set cors
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Elizabeth Hunt 2025-01-05 17:30:36 -08:00
parent cb61b8e349
commit 9b954ac062
Signed by: simponic
GPG Key ID: 2909B9A7FF6213EE

View File

@ -60,6 +60,13 @@ func SetJsonContinuation(context *types.RequestContext, req *http.Request, resp
}
}
func SetCorsContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain {
resp.Header().Set("Access-Control-Allow-Origin", "*")
return success(context, req, resp)
}
}
func CacheControlMiddleware(next http.Handler, maxAge int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := fmt.Sprintf("public, max-age=%d", maxAge)
@ -68,6 +75,15 @@ func CacheControlMiddleware(next http.Handler, maxAge int) http.Handler {
})
}
// basically b(a(message)) if and only if b is successful
func Compose(a types.Continuation, b types.Continuation) types.Continuation {
return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
return b(context, req, resp)(a, FailurePassingContinuation)(success, failure)
}
}
}
func MakeMux(argv *args.Arguments, dbConn *sql.DB) *http.ServeMux {
mux := http.NewServeMux()
@ -95,8 +111,8 @@ func MakeMux(argv *args.Arguments, dbConn *sql.DB) *http.ServeMux {
mux.HandleFunc("GET /updates", func(w http.ResponseWriter, r *http.Request) {
requestContext := makeRequestContext()
LogRequestContinuation(requestContext, r, w)(whois.ListUpdates, FailurePassingContinuation)(SetJsonContinuation, FailurePassingContinuation)(template.TemplateContinuation("updates.json.tmpl", false), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
continuation := Compose(template.TemplateContinuation("updates.json.tmpl", false), Compose(whois.ListUpdates, Compose(SetJsonContinuation, SetCorsContinuation)))
LogRequestContinuation(requestContext, r, w)(continuation, FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
})
return mux