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-18 05:51:09 +00:00
|
|
|
import "time"
|
2022-03-16 04:12:17 +00:00
|
|
|
|
2022-03-21 04:04:20 +00:00
|
|
|
import "github.com/firestuff/storebus"
|
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-21 04:04:20 +00:00
|
|
|
store *storebus.Store
|
|
|
|
|
bus *storebus.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-21 04:04:20 +00:00
|
|
|
store: storebus.NewStore(storePath),
|
|
|
|
|
bus: storebus.NewBus(),
|
2022-03-16 04:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 05:28:32 +00:00
|
|
|
api.router.HandleFunc("/template", returnError(jsonOutput(api.createTemplate))).Methods("POST").Headers("Content-Type", "application/json")
|
2022-03-18 05:51:09 +00:00
|
|
|
api.router.HandleFunc("/template/{id}", returnError(api.streamTemplate)).Methods("GET").Headers("Accept", "text/event-stream")
|
2022-03-18 05:28:32 +00:00
|
|
|
api.router.HandleFunc("/template/{id}", returnError(jsonOutput(api.getTemplate))).Methods("GET")
|
|
|
|
|
api.router.HandleFunc("/template/{id}", returnError(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-21 04:04:20 +00:00
|
|
|
func (api *API) createTemplate(r *http.Request) (storebus.Object, 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
|
|
|
}
|
|
|
|
|
|
2022-03-18 05:51:09 +00:00
|
|
|
func (api *API) streamTemplate(w http.ResponseWriter, r *http.Request) (string, int) {
|
2022-03-17 04:08:08 +00:00
|
|
|
log.Printf("streamTemplate %s", mux.Vars(r))
|
|
|
|
|
|
2022-03-18 05:51:09 +00:00
|
|
|
_, ok := w.(http.Flusher)
|
2022-03-17 04:08:08 +00:00
|
|
|
if !ok {
|
2022-03-18 05:51:09 +00:00
|
|
|
return "Streaming unsupported", http.StatusBadRequest
|
2022-03-17 04:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
|
|
2022-03-18 05:51:09 +00:00
|
|
|
template := NewTemplate()
|
|
|
|
|
template.Id = mux.Vars(r)["id"]
|
|
|
|
|
|
|
|
|
|
err := api.store.Read(template)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Sprintf("Template %s not found", template.Id), http.StatusNotFound
|
|
|
|
|
}
|
2022-03-17 04:08:08 +00:00
|
|
|
|
2022-03-18 05:51:09 +00:00
|
|
|
writeEvent(w, template)
|
2022-03-17 04:08:08 +00:00
|
|
|
|
2022-03-18 05:51:09 +00:00
|
|
|
closeChan := w.(http.CloseNotifier).CloseNotify()
|
|
|
|
|
msgChan := api.bus.Subscribe(template)
|
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
|
|
|
|
|
|
connected := true
|
|
|
|
|
for connected {
|
|
|
|
|
select {
|
|
|
|
|
case <-closeChan:
|
|
|
|
|
connected = false
|
|
|
|
|
case msg := <-msgChan:
|
|
|
|
|
writeEvent(w, msg)
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
writeEvent(w, NewHeartbeat())
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-17 04:08:08 +00:00
|
|
|
|
|
|
|
|
log.Printf("streamTemplate %s end", mux.Vars(r))
|
2022-03-18 05:51:09 +00:00
|
|
|
|
|
|
|
|
return "", 0
|
2022-03-16 04:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-21 04:04:20 +00:00
|
|
|
func (api *API) getTemplate(r *http.Request) (storebus.Object, 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-21 04:04:20 +00:00
|
|
|
func (api *API) updateTemplate(r *http.Request) (storebus.Object, 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
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 04:04:20 +00:00
|
|
|
func readJson(r *http.Request, out storebus.Object) (string, int) {
|
2022-03-18 05:22:16 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 05:28:32 +00:00
|
|
|
func returnError(wrapped func(http.ResponseWriter, *http.Request) (string, int)) func(http.ResponseWriter, *http.Request) {
|
2022-03-18 05:22:16 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-03-18 05:28:32 +00:00
|
|
|
msg, code := wrapped(w, r)
|
2022-03-18 05:22:16 +00:00
|
|
|
if code != 0 {
|
|
|
|
|
http.Error(w, msg, code)
|
2022-03-18 05:28:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 04:04:20 +00:00
|
|
|
func jsonOutput(wrapped func(*http.Request) (storebus.Object, string, int)) func(http.ResponseWriter, *http.Request) (string, int) {
|
2022-03-18 05:28:32 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) (string, int) {
|
|
|
|
|
out, msg, code := wrapped(r)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
return msg, code
|
2022-03-18 05:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
|
err := enc.Encode(out)
|
|
|
|
|
if err != nil {
|
2022-03-18 05:28:32 +00:00
|
|
|
return fmt.Sprintf("Failed to encode JSON: %s", err), http.StatusInternalServerError
|
2022-03-18 05:22:16 +00:00
|
|
|
}
|
2022-03-18 05:28:32 +00:00
|
|
|
|
|
|
|
|
return "", 0
|
2022-03-18 05:22:16 +00:00
|
|
|
}
|
2022-03-16 04:12:17 +00:00
|
|
|
}
|
2022-03-18 05:51:09 +00:00
|
|
|
|
2022-03-21 04:04:20 +00:00
|
|
|
func writeEvent(w http.ResponseWriter, in storebus.Object) (string, int) {
|
2022-03-18 05:51:09 +00:00
|
|
|
data, err := json.Marshal(in)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Sprintf("Failed to encode JSON: %s", err), http.StatusInternalServerError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", in.GetType(), data)
|
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
|
|
|
|
|
|
return "", 0
|
|
|
|
|
}
|