From 8dbf795902545e421c1f1bbd6061acf1ca5fb03b Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 29 Jul 2020 17:26:33 +0200 Subject: [PATCH] Allow root post --- http.go | 8 +------- posts.go | 6 +++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/http.go b/http.go index 5a2e8e4..35202d0 100644 --- a/http.go +++ b/http.go @@ -56,15 +56,13 @@ func buildHandler() (http.Handler, error) { r.Use(middleware.Recoverer) r.Use(middleware.StripSlashes) - r.Get("/", hello) - allPostPaths, err := allPostPaths() if err != nil { return nil, err } else { for _, path := range allPostPaths { if path != "" { - r.Get("/"+path, servePost) + r.Get(path, servePost) } } } @@ -72,10 +70,6 @@ func buildHandler() (http.Handler, error) { return r, nil } -func hello(w http.ResponseWriter, _ *http.Request) { - _, _ = w.Write([]byte("Hello World!")) -} - type dynamicHandler struct { realHandler http.Handler changeMutex *sync.Mutex diff --git a/posts.go b/posts.go index 817837b..20cdbad 100644 --- a/posts.go +++ b/posts.go @@ -18,7 +18,11 @@ type post struct { } func servePost(w http.ResponseWriter, r *http.Request) { - post, err := getPost(strings.TrimSuffix(strings.TrimPrefix(r.RequestURI, "/"), "/"), r.Context()) + path := r.RequestURI + if len(path) > 1 { + path = strings.TrimSuffix(path, "/") + } + post, err := getPost(path, r.Context()) if err == postNotFound { http.NotFound(w, r) return