diff --git a/api/api.go b/api/api.go index 22ea4a8..43dd476 100644 --- a/api/api.go +++ b/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 { 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 diff --git a/api/template/template.go b/api/template/template.go index 9b43a42..4e0d4f9 100644 --- a/api/template/template.go +++ b/api/template/template.go @@ -65,7 +65,9 @@ func TemplateContinuation(path string, showBase bool) types.Continuation { 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()) return success(context, req, resp) } diff --git a/api/whois/whois.go b/api/whois/whois.go index a30661d..cea7326 100644 --- a/api/whois/whois.go +++ b/api/whois/whois.go @@ -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) + } +} diff --git a/templates/name.tmpl b/templates/name.tmpl deleted file mode 100644 index 108b8a3..0000000 --- a/templates/name.tmpl +++ /dev/null @@ -1,3 +0,0 @@ -{{ define "content" }} -{{ .Latest.Name }} -{{ end }} \ No newline at end of file diff --git a/templates/updates.json.tmpl b/templates/updates.json.tmpl new file mode 100644 index 0000000..529a2e0 --- /dev/null +++ b/templates/updates.json.tmpl @@ -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 }} \ No newline at end of file