2022-03-16 04:12:17 +00:00
|
|
|
package main
|
|
|
|
|
|
2022-03-17 04:08:08 +00:00
|
|
|
import "encoding/json"
|
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-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-16 04:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-17 04:08:08 +00:00
|
|
|
api.router.HandleFunc("/template", api.createTemplate).Methods("POST").Headers("Content-Type", "application/json")
|
|
|
|
|
api.router.HandleFunc("/template/{id}", api.streamTemplate).Methods("GET").Headers("Accept", "text/event-stream")
|
2022-03-16 04:12:17 +00:00
|
|
|
api.router.HandleFunc("/template/{id}", api.getTemplate).Methods("GET")
|
2022-03-17 04:08:08 +00:00
|
|
|
api.router.HandleFunc("/template/{id}", 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (api *API) createTemplate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
log.Printf("createTemplate")
|
2022-03-17 04:08:08 +00:00
|
|
|
|
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
|
dec.DisallowUnknownFields()
|
|
|
|
|
|
|
|
|
|
template := NewTemplate()
|
|
|
|
|
err := dec.Decode(template)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template.Id = uuid.NewString()
|
|
|
|
|
|
|
|
|
|
if !template.IsValid() {
|
|
|
|
|
http.Error(w, "invalid template", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = api.store.Write(template)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "failed to write template", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
|
err = enc.Encode(template)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "failed to encode json", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (api *API) getTemplate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
log.Printf("getTemplate %s", mux.Vars(r))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (api *API) updateTemplate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
log.Printf("updateTemplate %s", mux.Vars(r))
|
|
|
|
|
}
|