From 1f3ae665ed2e58ca572678ce7caf8b711f226392 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 29 Nov 2022 20:59:31 +0100 Subject: Introduce EDIT and MANAGE methods All admin "index" pages are moved under MANAGE, so that we can have (for example) and normal "GET /posts" page later which would replace the current index page, and potentially corresponding pages for the other categories. The EDIT method replaces the old `?edit` pattern, which normalizes how we differentiate page functionality generally. --- src/http/api.go | 72 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 30 deletions(-) (limited to 'src/http/api.go') diff --git a/src/http/api.go b/src/http/api.go index 01cad50..cbcd182 100644 --- a/src/http/api.go +++ b/src/http/api.go @@ -190,7 +190,9 @@ func (a *api) blogHandler() http.Handler { mux.Handle("/posts/", http.StripPrefix("/posts", apiutil.MethodMux(map[string]http.Handler{ - "GET": a.renderPostHandler(), + "GET": a.getPostHandler(), + "EDIT": a.editPostHandler(false), + "MANAGE": a.managePostsHandler(), "POST": a.postPostHandler(), "DELETE": a.deletePostHandler(false), "PREVIEW": a.previewPostHandler(), @@ -200,6 +202,7 @@ func (a *api) blogHandler() http.Handler { mux.Handle("/assets/", http.StripPrefix("/assets", apiutil.MethodMux(map[string]http.Handler{ "GET": a.getPostAssetHandler(), + "MANAGE": a.managePostAssetsHandler(), "POST": a.postPostAssetHandler(), "DELETE": a.deletePostAssetHandler(), }), @@ -211,7 +214,8 @@ func (a *api) blogHandler() http.Handler { authMiddleware(a.auther)( apiutil.MethodMux(map[string]http.Handler{ - "GET": a.renderDraftPostHandler(), + "EDIT": a.editPostHandler(true), + "MANAGE": a.manageDraftPostsHandler(), "POST": a.postDraftPostHandler(), "DELETE": a.deletePostHandler(true), "PREVIEW": a.previewPostHandler(), @@ -227,17 +231,21 @@ func (a *api) blogHandler() http.Handler { mux.Handle("/feed.xml", a.renderFeedHandler()) mux.Handle("/", a.renderIndexHandler()) + readOnlyMiddlewares := []middleware{ + logReqMiddleware, // only log GETs on cache miss + cacheMiddleware(cache), + } + + readWriteMiddlewares := []middleware{ + purgeCacheOnOKMiddleware(cache), + authMiddleware(a.auther), + } + h := apiutil.MethodMux(map[string]http.Handler{ - "GET": applyMiddlewares( - mux, - logReqMiddleware, // only log GETs on cache miss - cacheMiddleware(cache), - ), - "*": applyMiddlewares( - mux, - purgeCacheOnOKMiddleware(cache), - authMiddleware(a.auther), - ), + "GET": applyMiddlewares(mux, readOnlyMiddlewares...), + "MANAGE": applyMiddlewares(mux, readOnlyMiddlewares...), + "EDIT": applyMiddlewares(mux, readOnlyMiddlewares...), + "*": applyMiddlewares(mux, readWriteMiddlewares...), }) return h @@ -247,26 +255,30 @@ func (a *api) handler() http.Handler { mux := http.NewServeMux() - mux.Handle("/api/", http.StripPrefix("/api", a.apiHandler())) - mux.Handle("/", a.blogHandler()) + mux.Handle("/api/", applyMiddlewares( + http.StripPrefix("/api", a.apiHandler()), + logReqMiddleware, + )) - h := apiutil.MethodMux(map[string]http.Handler{ - "GET": applyMiddlewares( - mux, - ), - "*": applyMiddlewares( - mux, - a.checkCSRFMiddleware, - addResponseHeadersMiddleware(map[string]string{ - "Cache-Control": "no-store, max-age=0", - "Pragma": "no-cache", - "Expires": "0", - }), - logReqMiddleware, - ), - }) + mux.Handle("/", a.blogHandler()) - h = setLoggerMiddleware(a.params.Logger)(h) + h := applyMiddlewares( + apiutil.MethodMux(map[string]http.Handler{ + "GET": applyMiddlewares( + mux, + ), + "*": applyMiddlewares( + mux, + a.checkCSRFMiddleware, + addResponseHeadersMiddleware(map[string]string{ + "Cache-Control": "no-store, max-age=0", + "Pragma": "no-cache", + "Expires": "0", + }), + ), + }), + setLoggerMiddleware(a.params.Logger), + ) return h } -- cgit v1.2.3