serve templates from disk

This commit is contained in:
Stephen McQuay 2017-01-26 19:44:31 -08:00
parent 85296b4e1b
commit 09f4fef613
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
6 changed files with 92 additions and 36 deletions

View File

@ -10,11 +10,6 @@ import (
)
func main() {
static := ""
if s := os.Getenv("STATIC"); s != "" {
static = s
}
port := 8000
if os.Getenv("PORT") != "" {
p, err := strconv.Atoi(os.Getenv("PORT"))
@ -25,9 +20,19 @@ func main() {
port = p
}
static := ""
if s := os.Getenv("STATIC"); s != "" {
static = s
}
tmpl := ""
if t := os.Getenv("TEMPLATES"); t != "" {
tmpl = t
}
sm := http.NewServeMux()
web.NewServer(sm, static)
web.NewSite(sm, static, tmpl)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), sm); err != nil {
fmt.Fprintf(os.Stderr, "problem serving: %v\n", err)
os.Exit(1)

View File

@ -1,26 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, initial-scale=1" name="viewport">
<title>smcquay</title>
<link rel="stylesheet" type="text/css" href="/css/sm.css">
</head>
<body>
<div id="content">
<div id="title">
<a href="https://s.mcquay.me">smcquay</a>
</div>
<div id="pic">
<a href="https://s.mcquay.me"><img src="/img/smcquay.png"></a>
</div>
<div id="info">
<p>
My name is <a href="http://stephen.mcquay.me">Stephen McQuay</a>,
<a href="https://twitter.com/smcquay">smcquay</a> on the internet.
I speak <a href="https://golang.org">gopher</a>
</p>
</div>
</div>
<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-2869147-4', 'auto'); ga('send', 'pageview'); </script>
</body>
</html>

13
templates/base.html Normal file
View File

@ -0,0 +1,13 @@
{{ define "base" }}
<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, initial-scale=1" name="viewport">
<title>ωτφ</title>
<link rel="stylesheet" type="text/css" href="/static/css/sm.css">
</head>
<body>
{{ template "body" . }}
</body>
</html>
{{ end }}

15
templates/home.html Normal file
View File

@ -0,0 +1,15 @@
{{ define "body" }}
<div id="content">
<div id="title">
<a href="https://s.mcquay.me">smcquay</a>
</div>
<div id="pic">
<a href="https://s.mcquay.me"><img src="/static/img/smcquay.png"></a>
</div>
<div id="info">
<p>
My name is <a href="http://stephen.mcquay.me">Stephen McQuay</a>.
</p>
</div>
</div>
{{ end }}

24
tmpl.go Normal file
View File

@ -0,0 +1,24 @@
package web
import (
"html/template"
"path/filepath"
)
// TemplateGetter defines what needs to be implemented to be able to fetch
// templates for use by a Site.
type TemplateGetter interface {
Get(name string) (*template.Template, error)
}
// Disk keeps track of the location of the root directory of the template
// directory.
type Disk struct {
Root string
}
// Get attempts to merge the requested name+.html with base.html found at root.
func (d Disk) Get(name string) (*template.Template, error) {
p := filepath.Join(d.Root, name+".html")
return template.New("").ParseFiles(p, filepath.Join(d.Root, "base.html"))
}

33
web.go
View File

@ -1,6 +1,7 @@
package web
import (
"fmt"
"net/http"
"path/filepath"
@ -10,9 +11,15 @@ import (
//go:generate go get github.com/jteeuwen/go-bindata/...
//go:generate go get github.com/elazarl/go-bindata-assetfs/...
//go:generate rm -f static.go
//go:generate go-bindata -o static.go -pkg=web static/...
//go:generate go-bindata -o static.go -pkg=web static/... templates/...
type Site struct {
tmpl TemplateGetter
}
func NewSite(sm *http.ServeMux, static, templates string) *Site {
s := &Site{}
func NewServer(sm *http.ServeMux, static string) {
var fs http.FileSystem
if p, d := filepath.Split(static); d == "static" {
static = p
@ -23,12 +30,30 @@ func NewServer(sm *http.ServeMux, static string) {
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo,
Prefix: "static",
}
}
if templates != "" {
s.tmpl = Disk{templates}
}
sm.HandleFunc("/", s.Home)
sm.Handle(
"/",
"/static/",
http.FileServer(fs),
)
return s
}
func (s *Site) Home(w http.ResponseWriter, req *http.Request) {
c := map[string]string{
"time": "asdf",
}
tmpl, err := s.tmpl.Get("home")
if err != nil {
http.Error(w, fmt.Sprintf("not found: %v", err), http.StatusNotFound)
return
}
tmpl.ExecuteTemplate(w, "base", c)
}