This commit is contained in:
parent
604d4981f2
commit
cb61b8e349
11
api/api.go
11
api/api.go
@ -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 {
|
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)
|
||||||
@ -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)
|
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()
|
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
|
return mux
|
||||||
|
@ -65,7 +65,9 @@ func TemplateContinuation(path string, showBase bool) types.Continuation {
|
|||||||
return failure(context, req, resp)
|
return failure(context, req, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.Header().Set("Content-Type", "text/html")
|
if showBase {
|
||||||
|
resp.Header().Set("Content-Type", "text/html")
|
||||||
|
}
|
||||||
resp.Write(html.Bytes())
|
resp.Write(html.Bytes())
|
||||||
return success(context, req, resp)
|
return success(context, req, resp)
|
||||||
}
|
}
|
||||||
|
@ -26,3 +26,17 @@ func FetchLatestName(context *types.RequestContext, req *http.Request, resp http
|
|||||||
return success(context, req, resp)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
{{ define "content" }}
|
|
||||||
{{ .Latest.Name }}
|
|
||||||
{{ end }}
|
|
3
templates/updates.json.tmpl
Normal file
3
templates/updates.json.tmpl
Normal 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 }}
|
Loading…
Reference in New Issue
Block a user