2020-04-23 07:42:16 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2020-04-23 12:55:30 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"status/db"
|
2020-04-23 07:42:16 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func writeJSON(w http.ResponseWriter, a interface{}) {
|
|
|
|
bytes, err := json.Marshal(a)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
|
2020-04-23 12:55:30 +07:00
|
|
|
return
|
2020-04-23 07:42:16 +07:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-04-23 08:23:21 +07:00
|
|
|
_, err = w.Write(bytes)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
|
|
|
|
}
|
2020-04-23 07:42:16 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func allStatus(w http.ResponseWriter, req *http.Request) {
|
|
|
|
status := getStatus()
|
|
|
|
writeJSON(w, status)
|
|
|
|
}
|
2020-04-23 12:55:30 +07:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|