diff options
| author | s-ol <s+removethis@s-ol.nu> | 2022-09-30 10:30:56 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2022-09-30 10:30:56 +0000 |
| commit | 50901e2fcea76086bbf32e4b7fcc216c607d1fd7 (patch) | |
| tree | 618ceba2552379bac30b60e53c2c7c991ebb9707 | |
| parent | add README (diff) | |
| download | json-hopper-50901e2fcea76086bbf32e4b7fcc216c607d1fd7.tar.gz json-hopper-50901e2fcea76086bbf32e4b7fcc216c607d1fd7.zip | |
fix CORS etc
| -rw-r--r-- | main.go | 43 |
1 files changed, 26 insertions, 17 deletions
@@ -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) |
