Started worrying about templates

I am trying to match what Jeff did in his https://github.com/zeebo/gostbook
example.
This commit is contained in:
Stephen McQuay 2013-02-20 21:11:44 -08:00
parent 562b0311c1
commit f9d2d247bc
14 changed files with 9805 additions and 2 deletions

28
main.go
View File

@ -4,19 +4,35 @@ import (
"code.google.com/p/go.crypto/bcrypt"
"flag"
"fmt"
"github.com/gorilla/sessions"
"github.com/kuroneko/gosqlite3"
"html/template"
"log"
"net/http"
"path/filepath"
)
var addr = flag.String("addr", ":8000", "address I'll listen on.")
var static_files = flag.String("static", "./static", "location of static files")
var db_file = flag.String("db", "./db.sqlite", "the database")
var template_dir = flag.String("templates", "templates", "template dir")
var add_pw = flag.String("passwd", "", "add this pass to the db")
var store = sessions.NewCookieStore([]byte("hello world"))
var templates *template.Template
func homeHandler(c http.ResponseWriter, req *http.Request) {
log.Printf("%v\n", req.URL)
t := T("index.html")
if t != nil {
t.Execute(c, req.Host)
} else {
log.Fatal("template index.html not found")
}
}
func main() {
flag.Parse()
if *add_pw != "" {
hpass, err := bcrypt.GenerateFromPassword([]byte(*add_pw), bcrypt.DefaultCost)
if err != nil {
@ -34,7 +50,15 @@ func main() {
log.Fatal(err)
}
} else {
fmt.Printf("%v\n", *add_pw)
pattern := filepath.Join(*template_dir, "*.html")
var err error
templates, err = template.ParseGlob(pattern)
if err != nil {
println(*template_dir)
println(pattern)
log.Fatal("problem parsing template dir:", *template_dir)
}
http.HandleFunc("/", homeHandler)
http.Handle("/s/", http.StripPrefix("/s/",
http.FileServer(http.Dir(*static_files))))
if err := http.ListenAndServe(*addr, nil); err != nil {

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

6039
static/bootstrap/css/bootstrap.css vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

2159
static/bootstrap/js/bootstrap.js vendored Normal file

File diff suppressed because it is too large Load Diff

6
static/bootstrap/js/bootstrap.min.js vendored Normal file

File diff suppressed because one or more lines are too long

5
static/jquery-latest.js vendored Normal file

File diff suppressed because one or more lines are too long

397
static/readme.html Normal file
View File

@ -0,0 +1,397 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.10: http://docutils.sourceforge.net/" />
<title>Allowances</title>
<style type="text/css">
/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 7514 2012-09-14 14:27:12Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/
/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
border: 0 }
table.borderless td, table.borderless th {
/* Override padding for "table.docutils td" with "! important".
The right padding separates the table cells. */
padding: 0 0.5em 0 0 ! important }
.first {
/* Override more specific margin styles with "! important". */
margin-top: 0 ! important }
.last, .with-subtitle {
margin-bottom: 0 ! important }
.hidden {
display: none }
a.toc-backref {
text-decoration: none ;
color: black }
blockquote.epigraph {
margin: 2em 5em ; }
dl.docutils dd {
margin-bottom: 0.5em }
object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
overflow: hidden;
}
/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
font-weight: bold }
*/
div.abstract {
margin: 2em 5em }
div.abstract p.topic-title {
font-weight: bold ;
text-align: center }
div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
margin: 2em ;
border: medium outset ;
padding: 1em }
div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
font-weight: bold ;
font-family: sans-serif }
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title, .code .error {
color: red ;
font-weight: bold ;
font-family: sans-serif }
/* Uncomment (and remove this text!) to get reduced vertical space in
compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
margin-bottom: 0.5em }
div.compound .compound-last, div.compound .compound-middle {
margin-top: 0.5em }
*/
div.dedication {
margin: 2em 5em ;
text-align: center ;
font-style: italic }
div.dedication p.topic-title {
font-weight: bold ;
font-style: normal }
div.figure {
margin-left: 2em ;
margin-right: 2em }
div.footer, div.header {
clear: both;
font-size: smaller }
div.line-block {
display: block ;
margin-top: 1em ;
margin-bottom: 1em }
div.line-block div.line-block {
margin-top: 0 ;
margin-bottom: 0 ;
margin-left: 1.5em }
div.sidebar {
margin: 0 0 0.5em 1em ;
border: medium outset ;
padding: 1em ;
background-color: #ffffee ;
width: 40% ;
float: right ;
clear: right }
div.sidebar p.rubric {
font-family: sans-serif ;
font-size: medium }
div.system-messages {
margin: 5em }
div.system-messages h1 {
color: red }
div.system-message {
border: medium outset ;
padding: 1em }
div.system-message p.system-message-title {
color: red ;
font-weight: bold }
div.topic {
margin: 2em }
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
margin-top: 0.4em }
h1.title {
text-align: center }
h2.subtitle {
text-align: center }
hr.docutils {
width: 75% }
img.align-left, .figure.align-left, object.align-left {
clear: left ;
float: left ;
margin-right: 1em }
img.align-right, .figure.align-right, object.align-right {
clear: right ;
float: right ;
margin-left: 1em }
img.align-center, .figure.align-center, object.align-center {
display: block;
margin-left: auto;
margin-right: auto;
}
.align-left {
text-align: left }
.align-center {
clear: both ;
text-align: center }
.align-right {
text-align: right }
/* reset inner alignment in figures */
div.align-right {
text-align: inherit }
/* div.align-center * { */
/* text-align: left } */
ol.simple, ul.simple {
margin-bottom: 1em }
ol.arabic {
list-style: decimal }
ol.loweralpha {
list-style: lower-alpha }
ol.upperalpha {
list-style: upper-alpha }
ol.lowerroman {
list-style: lower-roman }
ol.upperroman {
list-style: upper-roman }
p.attribution {
text-align: right ;
margin-left: 50% }
p.caption {
font-style: italic }
p.credits {
font-style: italic ;
font-size: smaller }
p.label {
white-space: nowrap }
p.rubric {
font-weight: bold ;
font-size: larger ;
color: maroon ;
text-align: center }
p.sidebar-title {
font-family: sans-serif ;
font-weight: bold ;
font-size: larger }
p.sidebar-subtitle {
font-family: sans-serif ;
font-weight: bold }
p.topic-title {
font-weight: bold }
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
font: inherit }
pre.literal-block, pre.doctest-block, pre.math, pre.code {
margin-left: 2em ;
margin-right: 2em }
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
pre.code .literal.string, code .literal.string { color: #0C5404 }
pre.code .name.builtin, code .name.builtin { color: #352B84 }
pre.code .deleted, code .deleted { background-color: #DEB0A1}
pre.code .inserted, code .inserted { background-color: #A3D289}
span.classifier {
font-family: sans-serif ;
font-style: oblique }
span.classifier-delimiter {
font-family: sans-serif ;
font-weight: bold }
span.interpreted {
font-family: sans-serif }
span.option {
white-space: nowrap }
span.pre {
white-space: pre }
span.problematic {
color: red }
span.section-subtitle {
/* font-size relative to parent (h1..h6 element) */
font-size: 80% }
table.citation {
border-left: solid 1px gray;
margin-left: 1px }
table.docinfo {
margin: 2em 4em }
table.docutils {
margin-top: 0.5em ;
margin-bottom: 0.5em }
table.footnote {
border-left: solid 1px black;
margin-left: 1px }
table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
padding-left: 0.5em ;
padding-right: 0.5em ;
vertical-align: top }
table.docutils th.field-name, table.docinfo th.docinfo-name {
font-weight: bold ;
text-align: left ;
white-space: nowrap ;
padding-left: 0 }
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
font-size: 100% }
ul.auto-toc {
list-style-type: none }
</style>
</head>
<body>
<div class="document" id="allowances">
<h1 class="title">Allowances</h1>
<p class="rubric">keeping track of my children's money since 2013</p>
<div class="section" id="why">
<h1>Why</h1>
<p>I want to write a from-scratch, non-tutorial web application in go. This is my
attempt at such an endeavor.</p>
</div>
<div class="section" id="what">
<h1>What</h1>
<dl class="docutils">
<dt>The main point of this app should be the following:</dt>
<dd><ul class="first last">
<li><dl class="first docutils">
<dt>All should be able to see:</dt>
<dd><ul class="first last simple">
<li>how much each child has earned</li>
<li>show results for a single child</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Authenticated users should be able to:</dt>
<dd><ul class="first last simple">
<li>add money to a given child by surfing to their page and clicking on
coins</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>The app should:</dt>
<dd><ul class="first last">
<li><dl class="first docutils">
<dt>be resilient to restart</dt>
<dd><ul class="first last simple">
<li>on write, store info in .json db</li>
<li>on start up deal with parsing and empty data.json files</li>
</ul>
</dd>
</dl>
</li>
<li><p class="first">look better than what I've written in the past</p>
</li>
</ul>
</dd>
</dl>
</li>
</ul>
</dd>
</dl>
</div>
<div class="section" id="dependencies">
<h1>Dependencies</h1>
<blockquote>
<ul>
<li><dl class="first docutils">
<dt><a class="reference external" href="http://twitter.github.com/bootstrap/assets/bootstrap.zip">bootstrap</a></dt>
<dd><ul class="first last simple">
<li>place in ./static/bootstrap</li>
</ul>
</dd>
</dl>
</li>
</ul>
</blockquote>
</div>
</div>
</body>
</html>

25
template.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"html/template"
"path/filepath"
"sync"
)
var cachedTemplates = map[string]*template.Template{}
var cachedMutex sync.Mutex
func T(name string) *template.Template {
cachedMutex.Lock()
defer cachedMutex.Unlock()
if t, ok := cachedTemplates[name]; ok {
return t
}
t := template.New("_base.html")
t = template.Must(t.ParseFiles(
"templates/_base.html",
filepath.Join(*template_dir, name),
))
cachedTemplates[name] = t
return t
}

21
templates/_base.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ template "title" . }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/s/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="/s/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<style>
.pw {
padding-top: 100px;
}
</style>
</head>
<body>
<section id="content">
{{ template "content" . }}
</section>
<script src="/s/jquery-latest.js"></script>
<script src="/s/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

17
templates/index.html Normal file
View File

@ -0,0 +1,17 @@
{{ define "title" }}Welcome{{ end }}
{{ define "content" }}
<div class="container pw">
<div class="row-fluid">
<form class="form-inline offset4 span3">
<div class="input-prepend input-append span2">
<span class="add-on">
<i class="icon-lock"></i>
</span>
<input type="password" class="" placeholder="password">
<button class="btn" type="button">Sign in</button>
</div>
</form>
</div>
</div>
{{ end }}