Skip to content

Commit

Permalink
router.go: Add timeout a little less than AWS Lambda limits
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Apr 10, 2024
1 parent 90dbe94 commit 2910d9f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
19 changes: 19 additions & 0 deletions internal/httpx/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package httpx

import (
"context"
"net/http"
"time"

"github.com/earthboundkid/mid"
)

func WithTimeout(d time.Duration) mid.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, stop := context.WithTimeout(r.Context(), d)
defer stop()
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
51 changes: 30 additions & 21 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@ package api

import (
"net/http"
"time"

"github.com/earthboundkid/mid"
"github.com/go-chi/chi/v5/middleware"

"github.com/spotlightpa/almanack/internal/httpx"
"github.com/spotlightpa/almanack/pkg/almanack"
"github.com/spotlightpa/almanack/pkg/almlog"
)

func (app *appEnv) routes() http.Handler {
mux := http.NewServeMux()

var baseMW mid.Stack
baseMW.Push(middleware.RealIP)
baseMW.PushIf(!app.isLambda, middleware.Recoverer)
baseMW.Push(
almlog.Middleware,
app.versionMiddleware,
app.maxSizeMiddleware,
)
standardMW := mid.Stack{
httpx.WithTimeout(9 * time.Second),
}

// Start public endpoints
mux.Handle(`GET /api/arc-image`,
baseMW.Controller(app.getArcImage))
standardMW.Controller(app.getArcImage))
mux.Handle(`GET /api/bookmarklet/{slug}`,
baseMW.HandlerFunc(app.getBookmarklet))
standardMW.HandlerFunc(app.getBookmarklet))
mux.Handle(`GET /api/healthcheck`,
baseMW.HandlerFunc(app.ping))
standardMW.HandlerFunc(app.ping))
mux.Handle(`GET /api/healthcheck/{code}`,
baseMW.HandlerFunc(app.pingErr))
standardMW.HandlerFunc(app.pingErr))
mux.Handle(`POST /api/identity-hook`,
baseMW.HandlerFunc(app.postIdentityHook))
standardMW.HandlerFunc(app.postIdentityHook))
// End public endpoints

authMW := baseMW.With(app.authHeaderMiddleware)
authMW := standardMW.With(app.authHeaderMiddleware)

mux.Handle(`GET /api/user-info`,
authMW.Controller(app.userInfo))
Expand Down Expand Up @@ -131,7 +128,7 @@ func (app *appEnv) routes() http.Handler {
// Don't trust this middleware!
// Netlify should be verifying the role at the CDN level.
// This is just a fallback.
ssrMW := baseMW.With(app.authCookieMiddleware)
ssrMW := standardMW.With(app.authCookieMiddleware)

mux.Handle(`GET /ssr/user-info`,
ssrMW.Controller(app.userInfo))
Expand All @@ -153,16 +150,28 @@ func (app *appEnv) routes() http.Handler {
spotlightSSRMW.Controller(app.renderPage))

// Start background API endpoints
backgroundMW := mid.Stack{
httpx.WithTimeout(14 * time.Minute),
}
mux.Handle(`GET /api-background/cron`,
baseMW.Controller(app.backgroundCron))
backgroundMW.Controller(app.backgroundCron))
mux.Handle(`GET /api-background/images`,
baseMW.Controller(app.backgroundImages))
backgroundMW.Controller(app.backgroundImages))
mux.Handle(`GET /api-background/refresh-pages`,
baseMW.Controller(app.backgroundRefreshPages))
backgroundMW.Controller(app.backgroundRefreshPages))
mux.Handle(`GET /api-background/sleep/{duration}`,
baseMW.Controller(app.backgroundSleep))
backgroundMW.Controller(app.backgroundSleep))
// End background API endpoints

mux.Handle("/", baseMW.HandlerFunc(app.notFound))
return mux
mux.Handle("/", standardMW.HandlerFunc(app.notFound))

var baseMW mid.Stack
baseMW.Push(middleware.RealIP)
baseMW.PushIf(!app.isLambda, middleware.Recoverer)
baseMW.Push(
almlog.Middleware,
app.versionMiddleware,
app.maxSizeMiddleware,
)
return baseMW.Handler(mux)
}

0 comments on commit 2910d9f

Please sign in to comment.