From ccc26c3a0bb65ae2613e222c3ead7f6db377b483 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sat, 17 Sep 2022 11:41:20 -0400 Subject: More work on the editor and update the example site to utilize it. --- cmd/server/handlers.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'cmd/server/handlers.go') diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index 1711ae3..c44c19c 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "path/filepath" + "strings" "github.com/gorilla/mux" "riedstra.dev/mitch/go-website/page" @@ -22,7 +23,7 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { rtr.PathPrefix("/edit/").Handler( a.RequiresLogin(http.StripPrefix("/edit/", http.HandlerFunc(a.SaveEditPage)))).Methods("POST") - if a.redisPool != nil { + if a.redisPool != nil && !a.IsLoggedIn(r) { rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).Handler( rediscache.HandleWithParams(a.redisPool, a.RedisKey, http.HandlerFunc(a.FeedHandler))) @@ -45,9 +46,27 @@ func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) { u = "/index" } + loggedIn := a.IsLoggedIn(r) + + if u == "/dashboard" && loggedIn { + u = page.TemplateDirectory + "/dashboard" + } + + // Render nothing inside of the template directory if we're not logged in + if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) && + !loggedIn { + + page.Render4xx(w, r, map[string]interface{}{ + "LoggedIn": loggedIn, + }, 404) + return + } + u = filepath.Join(".", u) - page.RenderForPath(w, r, u) + page.RenderWithVars(w, r, u, map[string]interface{}{ + "LoggedIn": loggedIn, + }) } func (a *App) RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { -- cgit v1.2.3