From 10c60b3c9ba2c17419534cf4089328a66568e4f1 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sat, 3 Dec 2022 13:38:05 -0500 Subject: Add a couple handlers to serve up JSON and markdown --- cmd/server/handlers.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/server/middleware.go | 32 +------------------------------- 2 files changed, 48 insertions(+), 31 deletions(-) (limited to 'cmd') diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index c44c19c..d4b7140 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -28,9 +28,23 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { rediscache.HandleWithParams(a.redisPool, a.RedisKey, http.HandlerFunc(a.FeedHandler))) + rtr.PathPrefix("/_json/").Handler( + rediscache.HandleWithParams(a.redisPool, a.RedisKey, + http.StripPrefix("/_json", http.HandlerFunc(a.PageJsonHandler)))) + + rtr.PathPrefix("/_md/").Handler( + rediscache.HandleWithParams(a.redisPool, a.RedisKey, + http.StripPrefix("/_md", http.HandlerFunc(a.PageMarkdownHandler)))) + rtr.PathPrefix("/").Handler(rediscache.Handle( a.redisPool, a.RedisKey, http.HandlerFunc(a.PageHandler))) } else { + rtr.PathPrefix("/_md/").Handler(http.StripPrefix("/_md", + http.HandlerFunc(a.PageMarkdownHandler))) + + rtr.PathPrefix("/_json/").Handler(http.StripPrefix("/_json", + http.HandlerFunc(a.PageJsonHandler))) + rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc( a.FeedHandler) @@ -68,6 +82,39 @@ func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) { "LoggedIn": loggedIn, }) } +func (a *App) PageJsonHandler(w http.ResponseWriter, r *http.Request) { + u := r.URL.Path + if u == "/" { + u = "/index" + } + + // Skip template directory + if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) { + page.Render4xx(w, r, map[string]interface{}{}, 404) + return + } + + u = filepath.Join(".", u) + + page.RenderJson(w, r, u, map[string]interface{}{}, 200) +} + +func (a *App) PageMarkdownHandler(w http.ResponseWriter, r *http.Request) { + u := r.URL.Path + if u == "/" { + u = "/index" + } + + // Skip template directory + if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) { + page.Render4xx(w, r, map[string]interface{}{}, 404) + return + } + + u = filepath.Join(".", u) + + page.RenderMarkdown(w, r, u, map[string]interface{}{}, 200) +} func (a *App) RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { u := r.URL.Path diff --git a/cmd/server/middleware.go b/cmd/server/middleware.go index 0d332cd..7ba34cf 100644 --- a/cmd/server/middleware.go +++ b/cmd/server/middleware.go @@ -7,7 +7,7 @@ import ( "net/url" "time" - jwt "github.com/dgrijalva/jwt-go" + jwt "github.com/golang-jwt/jwt/v4" "riedstra.dev/mitch/go-website/page" "riedstra.dev/mitch/go-website/users" ) @@ -166,33 +166,3 @@ func (a *App) RequiresLogin(next http.Handler) http.Handler { }) } - -/* -// ConditionalMiddleware is used to select one handler or another based on -// a test function. If the test function returns true, use handler A, otherwise B. -// This allows the test condition to only be run when the handler is selected -// rather than trying to do this as a top level and ending up with a condition -// that is tested on every single request, regardless of whether or not -// the specific handler is selected -type ConditionalMiddleware struct { - A, B http.Handler - Test func(r *http.Request) bool -} - -func NewConditionalMiddleware(test func(r *http.Request) bool, - A, B http.Handler) http.Handler { - return &ConditionalMiddleware{ - Test: test, - A: A, - B: B, - } -} - -func (cm *ConditionalMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if cm.Test(r) { - cm.A.ServeHTTP(w, r) - } else { - cm.B.ServeHTTP(w, r) - } -} -*/ -- cgit v1.2.3