add json route
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Elizabeth Hunt 2025-01-05 17:17:52 -08:00
parent 604d4981f2
commit cb61b8e349
Signed by: simponic
GPG Key ID: 2909B9A7FF6213EE
5 changed files with 29 additions and 6 deletions

View File

@ -53,6 +53,13 @@ func IdContinuation(context *types.RequestContext, req *http.Request, resp http.
}
}
func SetJsonContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain {
resp.Header().Set("Content-Type", "application/json")
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)
@ -86,10 +93,10 @@ func MakeMux(argv *args.Arguments, dbConn *sql.DB) *http.ServeMux {
LogRequestContinuation(requestContext, r, w)(whois.FetchLatestName, FailurePassingContinuation)(template.TemplateContinuation("home.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
})
mux.HandleFunc("GET /name", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /updates", func(w http.ResponseWriter, r *http.Request) {
requestContext := makeRequestContext()
LogRequestContinuation(requestContext, r, w)(whois.FetchLatestName, FailurePassingContinuation)(template.TemplateContinuation("name.tmpl", false), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
LogRequestContinuation(requestContext, r, w)(whois.ListUpdates, FailurePassingContinuation)(SetJsonContinuation, FailurePassingContinuation)(template.TemplateContinuation("updates.json.tmpl", false), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation)
})
return mux

View File

@ -65,7 +65,9 @@ func TemplateContinuation(path string, showBase bool) types.Continuation {
return failure(context, req, resp)
}
if showBase {
resp.Header().Set("Content-Type", "text/html")
}
resp.Write(html.Bytes())
return success(context, req, resp)
}

View File

@ -26,3 +26,17 @@ func FetchLatestName(context *types.RequestContext, req *http.Request, resp http
return success(context, req, resp)
}
}
func ListUpdates(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
updates, err := database.ListUpdates(context.DBConn, database.ListUpdatesQuery{Limit: 25})
if err != nil {
log.Printf("err fetching updates %s", err)
resp.WriteHeader(http.StatusInternalServerError)
return failure(context, req, resp)
}
(*context.TemplateData)["Updates"] = updates
return success(context, req, resp)
}
}

View File

@ -1,3 +0,0 @@
{{ define "content" }}
{{ .Latest.Name }}
{{ end }}

View File

@ -0,0 +1,3 @@
{{ define "content" }}
[{{ range $i, $update := .Updates }}{{ if $i }},{{ end }}{"name": "{{$update.Name}}", "time": "{{$update.Time.Format "2006-01-02T15:04:05.000Z"}}"}{{ end }}]
{{ end }}