From 626000ab96ff007e88628a8965d1da270c60c660 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 5 Aug 2020 19:14:10 +0200 Subject: [PATCH] Add simple index (or /blog) site --- http.go | 23 +++++++++++++++++------ minify.go | 2 +- posts.go | 9 +++++++++ render.go | 9 ++++++++- templates/index.gohtml | 15 +++++++++++++++ templates/post.gohtml | 2 +- templates/summary.gohtml | 8 ++++++++ utils.go | 12 ++++++++++++ 8 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 templates/index.gohtml create mode 100644 templates/summary.gohtml create mode 100644 utils.go diff --git a/http.go b/http.go index 49fc382..4b0fc4c 100644 --- a/http.go +++ b/http.go @@ -10,7 +10,7 @@ import ( "sync" ) -const contentTypeHTML = "text/html" +const contentTypeHTML = "text/html; charset=utf-8" var d *dynamicHandler @@ -53,12 +53,9 @@ func buildHandler() (http.Handler, error) { r := chi.NewRouter() if appConfig.Server.Logging { - r.Use(middleware.RealIP) - r.Use(middleware.Logger) + r.Use(middleware.RealIP, middleware.Logger) } - r.Use(middleware.Recoverer) - r.Use(middleware.StripSlashes) - r.Use(middleware.GetHead) + r.Use(middleware.Recoverer, middleware.StripSlashes, middleware.GetHead) r.Route("/api", func(apiRouter chi.Router) { apiRouter.Use(middleware.BasicAuth("API", map[string]string{ @@ -88,11 +85,25 @@ func buildHandler() (http.Handler, error) { } } + routePatterns := routesToStringSlice(r.Routes()) + if !routePatterns.has("/") { + r.With(cacheMiddleware, minifier.Middleware).Get("/", serveIndex) + } else if !routePatterns.has("/blog") { + r.With(cacheMiddleware, minifier.Middleware).Get("/blog", serveIndex) + } + r.With(minifier.Middleware).NotFound(serve404) return r, nil } +func routesToStringSlice(routes []chi.Route) (ss stringSlice) { + for _, r := range routes { + ss = append(ss, r.Pattern) + } + return +} + type dynamicHandler struct { realHandler http.Handler changeMutex *sync.Mutex diff --git a/minify.go b/minify.go index 0d924e7..1a24297 100644 --- a/minify.go +++ b/minify.go @@ -12,7 +12,7 @@ var minifier *minify.M func initMinify() { minifier = minify.New() - minifier.AddFunc(contentTypeHTML, html.Minify) + minifier.AddFunc("text/html", html.Minify) minifier.AddFunc("text/css", css.Minify) minifier.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify) } diff --git a/posts.go b/posts.go index 510f271..74b69d1 100644 --- a/posts.go +++ b/posts.go @@ -31,6 +31,15 @@ func servePost(w http.ResponseWriter, r *http.Request) { render(w, templatePost, post) } +func serveIndex(w http.ResponseWriter, r *http.Request) { + posts, err := getAllPosts(r.Context()) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + render(w, templateIndex, posts) +} + func getPost(context context.Context, path string) (*Post, error) { posts, err := getPosts(context, path) if err != nil { diff --git a/render.go b/render.go index 9c63e59..df7ce15 100644 --- a/render.go +++ b/render.go @@ -11,6 +11,8 @@ import ( const templatePost = "post" const templateError = "error" const templateRedirect = "redirect" +const templateIndex = "index" +const templateSummary = "summary" var templates map[string]*template.Template var templateFunctions template.FuncMap @@ -31,10 +33,15 @@ func initRendering() { "p": func(post Post, parameter string) string { return post.Parameters[parameter] }, + "include": func(templateName string, data interface{}) (template.HTML, error) { + buf := new(bytes.Buffer) + err := templates[templateName].ExecuteTemplate(buf, templateName, data) + return template.HTML(buf.String()), err + }, } templates = make(map[string]*template.Template) - for _, name := range []string{templatePost, templateError, templateRedirect} { + for _, name := range []string{templatePost, templateError, templateRedirect, templateIndex, templateSummary} { templates[name] = loadTemplate(name) } } diff --git a/templates/index.gohtml b/templates/index.gohtml new file mode 100644 index 0000000..3eb1ae0 --- /dev/null +++ b/templates/index.gohtml @@ -0,0 +1,15 @@ +{{ define "title" }} + {{ blog.Title }} +{{ end }} + +{{ define "main" }} +
+ {{ range $i, $post := . }} + {{ include "summary" . }} + {{ end }} +
+{{ end }} + +{{ define "index" }} + {{ template "base" . }} +{{ end }} \ No newline at end of file diff --git a/templates/post.gohtml b/templates/post.gohtml index 572ce89..a626c4a 100644 --- a/templates/post.gohtml +++ b/templates/post.gohtml @@ -13,6 +13,6 @@ {{ end }} -{{ define "post"}} +{{ define "post" }} {{ template "base" . }} {{ end }} \ No newline at end of file diff --git a/templates/summary.gohtml b/templates/summary.gohtml new file mode 100644 index 0000000..cd290ea --- /dev/null +++ b/templates/summary.gohtml @@ -0,0 +1,8 @@ +{{ define "summary" }} +
+ {{ with p . "title" }}

{{ . }}

{{ end }} +

{{ md .Content }}

+ View +
+
+{{ end }} \ No newline at end of file diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..6e496b7 --- /dev/null +++ b/utils.go @@ -0,0 +1,12 @@ +package main + +type stringSlice []string + +func (s stringSlice) has(value string) bool { + for _, v := range s { + if v == value { + return true + } + } + return false +}