From 50901e2fcea76086bbf32e4b7fcc216c607d1fd7 Mon Sep 17 00:00:00 2001 From: s-ol Date: Fri, 30 Sep 2022 12:30:56 +0200 Subject: fix CORS etc --- main.go | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index b762245..7e815fb 100644 --- a/main.go +++ b/main.go @@ -92,23 +92,28 @@ func NewMeta() (map[string]interface{}, error) { func processValue(typ string, value string) interface{} { switch typ { - case "integer": - val, err := strconv.Atoi(value) - if err != nil { - return value + case "null": + if value == "" { + return nil + } + case "boolean": + if value == "true" || value == "on" { + return true + } else if value == "false" || value == "off" { + return false } - return val case "number": val, err := strconv.ParseFloat(value, 64) - if err != nil { - return value + if err == nil { + return val + } + case "integer": + val, err := strconv.Atoi(value) + if err == nil { + return val } - return val - case "boolean": - return value == "true" - default: - return value } + return value } func (h *SiteHandler) collectData(r *http.Request) (map[string]interface{}, error) { @@ -155,13 +160,14 @@ func (h *SiteHandler) collectData(r *http.Request) (map[string]interface{}, erro return data, nil } -func (h *SiteHandler) handlePUTPOST(w http.ResponseWriter, r *http.Request, meta map[string]interface{}) error { +func (h *SiteHandler) handlePOST(w http.ResponseWriter, r *http.Request, meta map[string]interface{}) error { data, err := h.collectData(r) if err != nil { return err } if err := h.schema.Validate(data); err != nil { + log.Printf("Validation Error %s", err) return &HTTPError{http.StatusNotAcceptable, nil} } @@ -189,7 +195,7 @@ func (h *SiteHandler) handlePUTPOST(w http.ResponseWriter, r *http.Request, meta return err } - log.Printf("%s %s", r.Method, meta["id"]) + log.Printf("POST %s", meta["id"]) var redirect_buf bytes.Buffer h.redirect_tpl.Execute(&redirect_buf, TemplateParams{id, key}) @@ -212,6 +218,9 @@ func (h *SiteHandler) handle(w http.ResponseWriter, r *http.Request) error { return &HTTPError{http.StatusUnauthorized, nil} } + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "*") + id := parts[2] if id == "" { @@ -219,7 +228,7 @@ func (h *SiteHandler) handle(w http.ResponseWriter, r *http.Request) error { return &HTTPError{http.StatusMethodNotAllowed, nil} } - return h.handlePUTPOST(w, r, nil) + return h.handlePOST(w, r, nil) } var data map[string]interface{} @@ -238,8 +247,8 @@ func (h *SiteHandler) handle(w http.ResponseWriter, r *http.Request) error { return &HTTPError{http.StatusUnauthorized, nil} } - if r.Method == "PUT" { - return h.handlePUTPOST(w, r, meta) + if r.Method == "POST" { + return h.handlePOST(w, r, meta) } else if r.Method == "GET" { w.Header().Set("Content-Type", "application/json") w.Write(raw_data) -- cgit v1.2.3