diff --git a/cmd/smweb/main.go b/cmd/smweb/main.go index f483486..6ac9aa0 100644 --- a/cmd/smweb/main.go +++ b/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) diff --git a/static/index.html b/static/index.html deleted file mode 100644 index 19e4bf9..0000000 --- a/static/index.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - - smcquay - - - -
-
- smcquay -
-
- -
-
-

- My name is Stephen McQuay, - smcquay on the internet. - I speak gopher -

-
-
- - - diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..bb93aeb --- /dev/null +++ b/templates/base.html @@ -0,0 +1,13 @@ +{{ define "base" }} + + + + + ωτφ + + + + {{ template "body" . }} + + +{{ end }} diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..8a24e34 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,15 @@ +{{ define "body" }} +
+
+ smcquay +
+
+ +
+
+

+ My name is Stephen McQuay. +

+
+
+{{ end }} diff --git a/tmpl.go b/tmpl.go new file mode 100644 index 0000000..5e9987b --- /dev/null +++ b/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")) +} diff --git a/web.go b/web.go index 6fcd4d9..983b2ec 100644 --- a/web.go +++ b/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) }