diff --git a/server/api.go b/server/api.go index f777cf2..0c4c735 100644 --- a/server/api.go +++ b/server/api.go @@ -3,12 +3,16 @@ package server import ( "encoding/json" "net/http" + "time" + + "status/db" ) func writeJSON(w http.ResponseWriter, a interface{}) { bytes, err := json.Marshal(a) if err != nil { http.Error(w, "500 Internal Server Error", http.StatusInternalServerError) + return } w.Header().Set("Content-Type", "application/json") _, err = w.Write(bytes) @@ -21,3 +25,17 @@ func allStatus(w http.ResponseWriter, req *http.Request) { status := getStatus() writeJSON(w, status) } + +func dayHistory(w http.ResponseWriter, req *http.Request) { + keys, ok := req.URL.Query()["service_name"] + if !ok || len(keys) == 0 || len(keys[0]) < 1 { + http.Error(w, "`service_name` not provided", http.StatusBadRequest) + return + } + if len(keys) >= 2 { + http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest) + return + } + entries := db.GetFromDB(keys[0], time.Now().Add(time.Duration(-24)*time.Hour), time.Now()) + writeJSON(w, entries) +} diff --git a/server/server.go b/server/server.go index 9c1fb84..c08b938 100644 --- a/server/server.go +++ b/server/server.go @@ -8,6 +8,7 @@ import ( func apiEndpoints() { http.HandleFunc("/api/latest", allStatus) + http.HandleFunc("/api/history/day", dayHistory) } func StartServer(port int) {