serve templates from disk
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user