Files
checky/api.go

159 lines
3.6 KiB
Go
Raw Normal View History

2022-03-16 04:12:17 +00:00
package main
2022-03-17 04:08:08 +00:00
import "encoding/json"
2022-03-18 05:22:16 +00:00
import "fmt"
2022-03-16 04:12:17 +00:00
import "log"
import "net/http"
2022-03-17 04:08:08 +00:00
import "github.com/google/uuid"
2022-03-16 04:12:17 +00:00
import "github.com/gorilla/mux"
type API struct {
router *mux.Router
2022-03-17 04:08:08 +00:00
store *Store
2022-03-18 05:22:16 +00:00
bus *Bus
2022-03-16 04:12:17 +00:00
}
2022-03-17 04:08:08 +00:00
func NewAPI(storePath string) *API {
2022-03-16 04:12:17 +00:00
api := &API{
router: mux.NewRouter(),
2022-03-17 04:08:08 +00:00
store: NewStore(storePath),
2022-03-18 05:22:16 +00:00
bus: NewBus(),
2022-03-16 04:12:17 +00:00
}
2022-03-18 05:22:16 +00:00
api.router.HandleFunc("/template", jsonOutput(api.createTemplate)).Methods("POST").Headers("Content-Type", "application/json")
2022-03-17 04:08:08 +00:00
api.router.HandleFunc("/template/{id}", api.streamTemplate).Methods("GET").Headers("Accept", "text/event-stream")
2022-03-18 05:22:16 +00:00
api.router.HandleFunc("/template/{id}", jsonOutput(api.getTemplate)).Methods("GET")
api.router.HandleFunc("/template/{id}", jsonOutput(api.updateTemplate)).Methods("PATCH").Headers("Content-Type", "application/json")
2022-03-16 04:12:17 +00:00
return api
}
func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
api.router.ServeHTTP(w, r)
}
2022-03-18 05:22:16 +00:00
func (api *API) createTemplate(r *http.Request) (interface{}, string, int) {
2022-03-16 04:12:17 +00:00
log.Printf("createTemplate")
2022-03-17 04:08:08 +00:00
template := NewTemplate()
2022-03-18 05:22:16 +00:00
msg, code := readJson(r, template)
if code != 0 {
return nil, msg, code
2022-03-17 04:08:08 +00:00
}
template.Id = uuid.NewString()
if !template.IsValid() {
2022-03-18 05:22:16 +00:00
return nil, "Invalid template", http.StatusBadRequest
2022-03-17 04:08:08 +00:00
}
2022-03-18 05:22:16 +00:00
err := api.store.Write(template)
2022-03-17 04:08:08 +00:00
if err != nil {
2022-03-18 05:22:16 +00:00
return nil, "Failed to write template", http.StatusInternalServerError
2022-03-17 04:08:08 +00:00
}
2022-03-18 05:22:16 +00:00
return template, "", 0
2022-03-17 04:08:08 +00:00
}
func (api *API) streamTemplate(w http.ResponseWriter, r *http.Request) {
log.Printf("streamTemplate %s", mux.Vars(r))
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
closeChan := w.(http.CloseNotifier).CloseNotify()
flusher.Flush()
<-closeChan
log.Printf("streamTemplate %s end", mux.Vars(r))
2022-03-16 04:12:17 +00:00
}
2022-03-18 05:22:16 +00:00
func (api *API) getTemplate(r *http.Request) (interface{}, string, int) {
2022-03-16 04:12:17 +00:00
log.Printf("getTemplate %s", mux.Vars(r))
2022-03-18 05:22:16 +00:00
template := NewTemplate()
template.Id = mux.Vars(r)["id"]
err := api.store.Read(template)
if err != nil {
return nil, fmt.Sprintf("Template %s not found", template.Id), http.StatusNotFound
}
return template, "", 0
2022-03-16 04:12:17 +00:00
}
2022-03-18 05:22:16 +00:00
func (api *API) updateTemplate(r *http.Request) (interface{}, string, int) {
2022-03-16 04:12:17 +00:00
log.Printf("updateTemplate %s", mux.Vars(r))
2022-03-18 05:22:16 +00:00
patch := NewTemplate()
msg, code := readJson(r, patch)
if code != 0 {
return nil, msg, code
}
template := NewTemplate()
template.Id = mux.Vars(r)["id"]
err := api.store.Read(template)
if err != nil {
return nil, fmt.Sprintf("Template %s not found", template.Id), http.StatusNotFound
}
if patch.Title != "" {
template.Title = patch.Title
}
if !template.IsValid() {
return nil, "Invalid template", http.StatusBadRequest
}
err = api.store.Write(template)
if err != nil {
return nil, "Failed to write template", http.StatusInternalServerError
}
api.bus.Announce(template)
return template, "", 0
}
func readJson(r *http.Request, out interface{}) (string, int) {
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
err := dec.Decode(out)
if err != nil {
return fmt.Sprintf("Invalid JSON: %s", err), http.StatusBadRequest
}
return "", 0
}
func jsonOutput(wrapped func(*http.Request) (interface{}, string, int)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
out, msg, code := wrapped(r)
if code != 0 {
http.Error(w, msg, code)
return
}
enc := json.NewEncoder(w)
err := enc.Encode(out)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to encode JSON: %s", err), http.StatusInternalServerError)
}
}
2022-03-16 04:12:17 +00:00
}