Browse Source

serve templates from disk

templ
Stephen McQuay 5 years ago
parent
commit
09f4fef613
No known key found for this signature in database GPG Key ID: 1ABF428F71BAFC3D
  1. 17
      cmd/smweb/main.go
  2. 26
      static/index.html
  3. 13
      templates/base.html
  4. 15
      templates/home.html
  5. 24
      tmpl.go
  6. 33
      web.go

17
cmd/smweb/main.go

@ -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)

26
static/index.html

@ -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

@ -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

@ -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

@ -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

@ -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)
}
Loading…
Cancel
Save