Re-arrange routes so that cache only applies to blog routes

This commit is contained in:
Brian Picciano 2022-05-21 10:18:00 -06:00
parent fb905ad53c
commit 79452e7472
2 changed files with 54 additions and 37 deletions

View File

@ -161,7 +161,32 @@ func (a *api) Shutdown(ctx context.Context) error {
return nil return nil
} }
func (a *api) handler() http.Handler { func (a *api) apiHandler() http.Handler {
mux := http.NewServeMux()
mux.Handle("/pow/challenge", a.newPowChallengeHandler())
mux.Handle("/pow/check",
a.requirePowMiddleware(
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {}),
),
)
mux.Handle("/mailinglist/subscribe", a.requirePowMiddleware(a.mailingListSubscribeHandler()))
mux.Handle("/mailinglist/finalize", a.mailingListFinalizeHandler())
mux.Handle("/mailinglist/unsubscribe", a.mailingListUnsubscribeHandler())
mux.Handle("/chat/global/", http.StripPrefix("/chat/global", newChatHandler(
a.params.GlobalRoom,
a.params.UserIDCalculator,
a.requirePowMiddleware,
)))
// disallowGetMiddleware is used rather than a MethodMux because it has an
// exception for websockets, which is needed for chat.
return disallowGetMiddleware(mux)
}
func (a *api) blogHandler() http.Handler {
cache, err := lru.New(5000) cache, err := lru.New(5000)
@ -172,32 +197,6 @@ func (a *api) handler() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
{
apiMux := http.NewServeMux()
apiMux.Handle("/pow/challenge", a.newPowChallengeHandler())
apiMux.Handle("/pow/check",
a.requirePowMiddleware(
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {}),
),
)
apiMux.Handle("/mailinglist/subscribe", a.requirePowMiddleware(a.mailingListSubscribeHandler()))
apiMux.Handle("/mailinglist/finalize", a.mailingListFinalizeHandler())
apiMux.Handle("/mailinglist/unsubscribe", a.mailingListUnsubscribeHandler())
apiMux.Handle("/chat/global/", http.StripPrefix("/chat/global", newChatHandler(
a.params.GlobalRoom,
a.params.UserIDCalculator,
a.requirePowMiddleware,
)))
mux.Handle("/api/", http.StripPrefix("/api",
// disallowGetMiddleware is used rather than a MethodMux because it
// has an exception for websockets, which is needed for chat.
disallowGetMiddleware(apiMux),
))
}
mux.Handle("/posts/", http.StripPrefix("/posts", mux.Handle("/posts/", http.StripPrefix("/posts",
apiutil.MethodMux(map[string]http.Handler{ apiutil.MethodMux(map[string]http.Handler{
"GET": a.renderPostHandler(), "GET": a.renderPostHandler(),
@ -220,19 +219,36 @@ func (a *api) handler() http.Handler {
mux.Handle("/feed.xml", a.renderFeedHandler()) mux.Handle("/feed.xml", a.renderFeedHandler())
mux.Handle("/", a.renderIndexHandler()) mux.Handle("/", a.renderIndexHandler())
globalHandler := http.Handler(mux) h := apiutil.MethodMux(map[string]http.Handler{
globalHandler = apiutil.MethodMux(map[string]http.Handler{
"GET": applyMiddlewares( "GET": applyMiddlewares(
globalHandler, mux,
logReqMiddleware, logReqMiddleware, // only log GETs on cache miss
cacheMiddleware(cache), cacheMiddleware(cache),
),
"*": applyMiddlewares(
mux,
purgeCacheOnOKMiddleware(cache),
authMiddleware(a.auther),
),
})
return h
}
func (a *api) handler() http.Handler {
mux := http.NewServeMux()
mux.Handle("/api/", http.StripPrefix("/api", a.apiHandler()))
mux.Handle("/", a.blogHandler())
h := apiutil.MethodMux(map[string]http.Handler{
"GET": applyMiddlewares(
mux,
setCSRFMiddleware, setCSRFMiddleware,
), ),
"*": applyMiddlewares( "*": applyMiddlewares(
globalHandler, mux,
purgeCacheOnOKMiddleware(cache),
authMiddleware(a.auther),
checkCSRFMiddleware, checkCSRFMiddleware,
addResponseHeadersMiddleware(map[string]string{ addResponseHeadersMiddleware(map[string]string{
"Cache-Control": "no-store, max-age=0", "Cache-Control": "no-store, max-age=0",
@ -243,7 +259,7 @@ func (a *api) handler() http.Handler {
), ),
}) })
globalHandler = setLoggerMiddleware(a.params.Logger)(globalHandler) h = setLoggerMiddleware(a.params.Logger)(h)
return globalHandler return h
} }

View File

@ -182,6 +182,7 @@ func purgeCacheOnOKMiddleware(cache *lru.Cache) middleware {
h.ServeHTTP(wrw, r) h.ServeHTTP(wrw, r)
if wrw.statusCode == 200 { if wrw.statusCode == 200 {
apiutil.GetRequestLogger(r).Info(r.Context(), "purging cache!")
cache.Purge() cache.Purge()
} }
}) })