From 4e3ed5453cfa6af89243ed22b19976c68892068a Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Fri, 12 Jun 2020 19:13:23 +0200 Subject: [PATCH] Added template rendering, caching, started blog pages. --- dev.sh | 1 + main.go | 55 ++++++++++++++++++- static/index.html | 117 ----------------------------------------- template/about.html | 81 ++++++++++++++++++++++++++++ template/blog.html | 8 +++ template/end.html | 11 ++++ template/head.html | 34 ++++++++++++ template/menu.html | 7 +++ template/notfound.html | 11 ++++ 9 files changed, 207 insertions(+), 118 deletions(-) delete mode 100644 static/index.html create mode 100644 template/about.html create mode 100644 template/blog.html create mode 100644 template/end.html create mode 100644 template/head.html create mode 100644 template/menu.html create mode 100644 template/notfound.html diff --git a/dev.sh b/dev.sh index 41835b9..9fe65cb 100755 --- a/dev.sh +++ b/dev.sh @@ -6,5 +6,6 @@ export MB_LOGLEVEL=debug export MB_ALLOWED_ORIGINS=* export MB_HOST=localhost:8080 +export MB_HOT_RELOAD=true go run main.go diff --git a/main.go b/main.go index 3aaf2a6..12c5256 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,13 @@ package main import ( + "bytes" "github.com/NYTimes/gziphandler" "github.com/caddyserver/certmagic" "github.com/emvi/logbuch" "github.com/gorilla/mux" "github.com/rs/cors" + "html/template" "net/http" "os" "strings" @@ -13,11 +15,18 @@ import ( const ( staticDir = "static" - staticDirPrefix = "/" + staticDirPrefix = "/static/" + templateDir = "template/*" logTimeFormat = "2006-01-02_15:04:05" envPrefix = "MB_" ) +var ( + tpl *template.Template + tplCache = make(map[string][]byte) + hotReload bool +) + func configureLog() { logbuch.SetFormatter(logbuch.NewFieldFormatter(logTimeFormat, "\t\t")) logbuch.Info("Configure logging...") @@ -41,9 +50,52 @@ func logEnvConfig() { } } +func loadTemplate() { + logbuch.Debug("Loading templates") + var err error + tpl, err = template.ParseGlob(templateDir) + + if err != nil { + logbuch.Fatal("Error loading template", logbuch.Fields{"err": err}) + } + + hotReload = os.Getenv("MB_HOT_RELOAD") == "true" + logbuch.Debug("Templates loaded", logbuch.Fields{"hot_reload": hotReload}) +} + +func renderTemplate(name string) { + logbuch.Debug("Rendering template", logbuch.Fields{"name": name}) + var buffer bytes.Buffer + + if err := tpl.ExecuteTemplate(&buffer, name, nil); err != nil { + logbuch.Fatal("Error executing template", logbuch.Fields{"err": err, "name": name}) + } + + tplCache[name] = buffer.Bytes() +} + +func serveTemplate(name string) http.HandlerFunc { + // render once so we have it in cache + renderTemplate(name) + + return func(w http.ResponseWriter, r *http.Request) { + if hotReload { + loadTemplate() + renderTemplate(name) + } + + if _, err := w.Write(tplCache[name]); err != nil { + logbuch.Error("Error returning page to client", logbuch.Fields{"err": err, "name": name}) + } + } +} + func setupRouter() *mux.Router { router := mux.NewRouter() router.PathPrefix(staticDirPrefix).Handler(http.StripPrefix(staticDirPrefix, gziphandler.GzipHandler(http.FileServer(http.Dir(staticDir))))) + router.Handle("/blog", serveTemplate("blog.html")) + router.Handle("/", serveTemplate("about.html")) + router.NotFoundHandler = serveTemplate("notfound.html") return router } @@ -83,6 +135,7 @@ func start(handler http.Handler) { func main() { configureLog() logEnvConfig() + loadTemplate() router := setupRouter() corsConfig := configureCors(router) start(corsConfig) diff --git a/static/index.html b/static/index.html deleted file mode 100644 index 9561259..0000000 --- a/static/index.html +++ /dev/null @@ -1,117 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - marvin blum - - - - - - - - - - - - -
-
-

marvin blum

-

welcome to my website!

-
- Marvin Blum -
- -
-

who am I?

-

- I'm a full stack software engineer from Germany, open source and Linux enthusiast and co-founder of Emvi. - In love with Go, but fluent in a lot of programming languages. -

-
-
-

projects

- -
-
-

skills

- -
-
-

work

- -
-
-
-

- Like to see more? Read my blog articles on Emvi, my project page on GitHub or send me a mail. -

-

- This page uses concrete for styling. Check it out! -

-
- - diff --git a/template/about.html b/template/about.html new file mode 100644 index 0000000..15d0c6b --- /dev/null +++ b/template/about.html @@ -0,0 +1,81 @@ +{{template "head.html"}} + +
+
+

marvin blum

+

welcome to my website!

+
+ Marvin Blum +
+ +{{template "menu.html"}} + +
+

who am I?

+

+ I'm a full stack software engineer from Germany, open source and Linux enthusiast and co-founder of Emvi. + In love with Go, but fluent in a lot of programming languages. +

+
+
+

latest blog posts

+

+ TODO +

+

+ View all +

+
+
+

projects

+ +
+
+

skills

+ +
+
+

work

+ +
+ +{{template "end.html"}} diff --git a/template/blog.html b/template/blog.html new file mode 100644 index 0000000..d16ce71 --- /dev/null +++ b/template/blog.html @@ -0,0 +1,8 @@ +{{template "head.html"}} +{{template "menu.html"}} + +
+

TODO Blog

+
+ +{{template "end.html"}} diff --git a/template/end.html b/template/end.html new file mode 100644 index 0000000..a283083 --- /dev/null +++ b/template/end.html @@ -0,0 +1,11 @@ +
+
+

+ Like to see more? Read my blog articles on Emvi, my project page on GitHub or send me a mail. +

+

+ This page uses concrete for styling. Check it out! +

+
+ + diff --git a/template/head.html b/template/head.html new file mode 100644 index 0000000..6c96969 --- /dev/null +++ b/template/head.html @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + marvin blum + + + + + + + + + + + + diff --git a/template/menu.html b/template/menu.html new file mode 100644 index 0000000..b74b425 --- /dev/null +++ b/template/menu.html @@ -0,0 +1,7 @@ + diff --git a/template/notfound.html b/template/notfound.html new file mode 100644 index 0000000..3d064b5 --- /dev/null +++ b/template/notfound.html @@ -0,0 +1,11 @@ +{{template "head.html"}} + +
+

Page not found

+

+ Nothing to see here...
+ Go back Home +

+
+ +{{template "end.html"}}