Re-arrange how api endpoints are defined

This commit is contained in:
Brian Picciano 2022-05-17 15:25:13 -06:00
parent 7b7bdcf57a
commit e406ad6e7c
3 changed files with 43 additions and 37 deletions

View File

@ -152,57 +152,63 @@ func (a *api) handler() http.Handler {
staticHandler = httputil.NewSingleHostReverseProxy(a.params.StaticProxy) staticHandler = httputil.NewSingleHostReverseProxy(a.params.StaticProxy)
} }
staticHandler = setCSRFMiddleware(staticHandler)
// sugar // sugar
requirePow := func(h http.Handler) http.Handler { requirePow := func(h http.Handler) http.Handler {
return a.requirePowMiddleware(h) return a.requirePowMiddleware(h)
} }
postFormMiddleware := func(h http.Handler) http.Handler {
h = checkCSRFMiddleware(h)
h = postOnlyMiddleware(h)
h = logReqMiddleware(h)
h = addResponseHeaders(map[string]string{
"Cache-Control": "no-store, max-age=0",
"Pragma": "no-cache",
"Expires": "0",
}, h)
return h
}
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/", staticHandler) mux.Handle("/", staticHandler)
apiMux := http.NewServeMux() {
apiMux.Handle("/pow/challenge", a.newPowChallengeHandler()) apiMux := http.NewServeMux()
apiMux.Handle("/pow/check", apiMux.Handle("/pow/challenge", a.newPowChallengeHandler())
requirePow( apiMux.Handle("/pow/check",
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {}), requirePow(
), http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {}),
) ),
)
apiMux.Handle("/mailinglist/subscribe", requirePow(a.mailingListSubscribeHandler())) apiMux.Handle("/mailinglist/subscribe", requirePow(a.mailingListSubscribeHandler()))
apiMux.Handle("/mailinglist/finalize", a.mailingListFinalizeHandler()) apiMux.Handle("/mailinglist/finalize", a.mailingListFinalizeHandler())
apiMux.Handle("/mailinglist/unsubscribe", a.mailingListUnsubscribeHandler()) apiMux.Handle("/mailinglist/unsubscribe", a.mailingListUnsubscribeHandler())
apiMux.Handle("/chat/global/", http.StripPrefix("/chat/global", newChatHandler( apiMux.Handle("/chat/global/", http.StripPrefix("/chat/global", newChatHandler(
a.params.GlobalRoom, a.params.GlobalRoom,
a.params.UserIDCalculator, a.params.UserIDCalculator,
a.requirePowMiddleware, a.requirePowMiddleware,
))) )))
var apiHandler http.Handler = apiMux mux.Handle("/api/", http.StripPrefix("/api", postFormMiddleware(apiMux)))
apiHandler = checkCSRFMiddleware(apiHandler) }
apiHandler = postOnlyMiddleware(apiHandler)
apiHandler = logReqMiddleware(apiHandler)
apiHandler = addResponseHeaders(map[string]string{
"Cache-Control": "no-store, max-age=0",
"Pragma": "no-cache",
"Expires": "0",
}, apiHandler)
mux.Handle("/api/", http.StripPrefix("/api", apiHandler)) {
v2Mux := http.NewServeMux()
v2Mux.Handle("/follow.html", a.renderDumbHandler("follow.html"))
v2Mux.Handle("/posts/", a.renderPostHandler())
v2Mux.Handle("/assets", a.renderPostAssetsIndexHandler())
v2Mux.Handle("/assets/", a.servePostAssetHandler())
v2Mux.Handle("/", a.renderIndexHandler())
// TODO need to setCSRFMiddleware on all these rendering endpoints mux.Handle("/v2/", http.StripPrefix("/v2", v2Mux))
mux.Handle("/v2/follow.html", a.renderDumbHandler("follow.html")) }
mux.Handle("/v2/posts/", a.renderPostHandler())
mux.Handle("/v2/", a.renderIndexHandler())
mux.Handle("/v2/assets/", a.servePostAssetHandler())
mux.Handle("/v2/admin/assets.html", a.renderAdminAssets())
var globalHandler http.Handler = mux var globalHandler http.Handler = mux
globalHandler = setCSRFMiddleware(globalHandler)
globalHandler = setLoggerMiddleware(a.params.Logger, globalHandler) globalHandler = setLoggerMiddleware(a.params.Logger, globalHandler)
return globalHandler return globalHandler

View File

@ -197,9 +197,9 @@ func (a *api) renderDumbHandler(tplName string) http.Handler {
}) })
} }
func (a *api) renderAdminAssets() http.Handler { func (a *api) renderPostAssetsIndexHandler() http.Handler {
tpl := a.mustParseTpl("admin/assets.html") tpl := a.mustParseTpl("admin-assets.html")
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {