This commit is contained in:
parent
cb61b8e349
commit
9b954ac062
20
api/api.go
20
api/api.go
@ -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 {
|
func CacheControlMiddleware(next http.Handler, maxAge int) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
header := fmt.Sprintf("public, max-age=%d", maxAge)
|
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 {
|
func MakeMux(argv *args.Arguments, dbConn *sql.DB) *http.ServeMux {
|
||||||
mux := http.NewServeMux()
|
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) {
|
mux.HandleFunc("GET /updates", func(w http.ResponseWriter, r *http.Request) {
|
||||||
requestContext := makeRequestContext()
|
requestContext := makeRequestContext()
|
||||||
|
continuation := Compose(template.TemplateContinuation("updates.json.tmpl", false), Compose(whois.ListUpdates, Compose(SetJsonContinuation, SetCorsContinuation)))
|
||||||
LogRequestContinuation(requestContext, r, w)(whois.ListUpdates, FailurePassingContinuation)(SetJsonContinuation, FailurePassingContinuation)(template.TemplateContinuation("updates.json.tmpl", false), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
|
LogRequestContinuation(requestContext, r, w)(continuation, FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
|
||||||
})
|
})
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
|
Loading…
Reference in New Issue
Block a user