summaryrefslogtreecommitdiff
path: root/srv/src/http/api.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-21 10:04:50 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-21 10:04:50 -0600
commitfb905ad53caac427e16a3fe6bb56748070ea69ad (patch)
tree04c8dc7e66388deeb9f550ca83e8d89bd1a26d4d /srv/src/http/api.go
parent1de0ab3b720cf7b83a7e29de4dbe35c117ccea0e (diff)
Add in-memory cache to GET requests, purges on successful POSTs
Diffstat (limited to 'srv/src/http/api.go')
-rw-r--r--srv/src/http/api.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/srv/src/http/api.go b/srv/src/http/api.go
index 11d092d..ebc1de2 100644
--- a/srv/src/http/api.go
+++ b/srv/src/http/api.go
@@ -15,6 +15,7 @@ import (
"strings"
"time"
+ lru "github.com/hashicorp/golang-lru"
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/blog.mediocregopher.com/srv/chat"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
@@ -162,8 +163,11 @@ func (a *api) Shutdown(ctx context.Context) error {
func (a *api) handler() http.Handler {
- requirePow := func(h http.Handler) http.Handler {
- return a.requirePowMiddleware(h)
+ cache, err := lru.New(5000)
+
+ // instantiating the lru cache can't realistically fail
+ if err != nil {
+ panic(err)
}
mux := http.NewServeMux()
@@ -172,12 +176,12 @@ func (a *api) handler() http.Handler {
apiMux := http.NewServeMux()
apiMux.Handle("/pow/challenge", a.newPowChallengeHandler())
apiMux.Handle("/pow/check",
- requirePow(
+ a.requirePowMiddleware(
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {}),
),
)
- apiMux.Handle("/mailinglist/subscribe", requirePow(a.mailingListSubscribeHandler()))
+ apiMux.Handle("/mailinglist/subscribe", a.requirePowMiddleware(a.mailingListSubscribeHandler()))
apiMux.Handle("/mailinglist/finalize", a.mailingListFinalizeHandler())
apiMux.Handle("/mailinglist/unsubscribe", a.mailingListUnsubscribeHandler())
@@ -222,10 +226,12 @@ func (a *api) handler() http.Handler {
"GET": applyMiddlewares(
globalHandler,
logReqMiddleware,
+ cacheMiddleware(cache),
setCSRFMiddleware,
),
"*": applyMiddlewares(
globalHandler,
+ purgeCacheOnOKMiddleware(cache),
authMiddleware(a.auther),
checkCSRFMiddleware,
addResponseHeadersMiddleware(map[string]string{