fix CORS etc
s-ol
5 months ago
91 | 91 | |
92 | 92 | func processValue(typ string, value string) interface{} { |
93 | 93 | switch typ { |
94 | case "null": | |
95 | if value == "" { | |
96 | return nil | |
97 | } | |
98 | case "boolean": | |
99 | if value == "true" || value == "on" { | |
100 | return true | |
101 | } else if value == "false" || value == "off" { | |
102 | return false | |
103 | } | |
104 | case "number": | |
105 | val, err := strconv.ParseFloat(value, 64) | |
106 | if err == nil { | |
107 | return val | |
108 | } | |
94 | 109 | case "integer": |
95 | 110 | val, err := strconv.Atoi(value) |
96 | if err != nil { | |
97 | return value | |
98 | } | |
99 | return val | |
100 | case "number": | |
101 | val, err := strconv.ParseFloat(value, 64) | |
102 | if err != nil { | |
103 | return value | |
104 | } | |
105 | return val | |
106 | case "boolean": | |
107 | return value == "true" | |
108 | default: | |
109 | return value | |
110 | } | |
111 | if err == nil { | |
112 | return val | |
113 | } | |
114 | } | |
115 | return value | |
111 | 116 | } |
112 | 117 | |
113 | 118 | func (h *SiteHandler) collectData(r *http.Request) (map[string]interface{}, error) { |
154 | 159 | return data, nil |
155 | 160 | } |
156 | 161 | |
157 | func (h *SiteHandler) handlePUTPOST(w http.ResponseWriter, r *http.Request, meta map[string]interface{}) error { | |
162 | func (h *SiteHandler) handlePOST(w http.ResponseWriter, r *http.Request, meta map[string]interface{}) error { | |
158 | 163 | data, err := h.collectData(r) |
159 | 164 | if err != nil { |
160 | 165 | return err |
161 | 166 | } |
162 | 167 | |
163 | 168 | if err := h.schema.Validate(data); err != nil { |
169 | log.Printf("Validation Error %s", err) | |
164 | 170 | return &HTTPError{http.StatusNotAcceptable, nil} |
165 | 171 | } |
166 | 172 | |
188 | 194 | return err |
189 | 195 | } |
190 | 196 | |
191 | log.Printf("%s %s", r.Method, meta["id"]) | |
197 | log.Printf("POST %s", meta["id"]) | |
192 | 198 | |
193 | 199 | var redirect_buf bytes.Buffer |
194 | 200 | h.redirect_tpl.Execute(&redirect_buf, TemplateParams{id, key}) |
211 | 217 | return &HTTPError{http.StatusUnauthorized, nil} |
212 | 218 | } |
213 | 219 | |
220 | w.Header().Set("Access-Control-Allow-Origin", "*") | |
221 | w.Header().Set("Access-Control-Allow-Methods", "*") | |
222 | ||
214 | 223 | id := parts[2] |
215 | 224 | |
216 | 225 | if id == "" { |
218 | 227 | return &HTTPError{http.StatusMethodNotAllowed, nil} |
219 | 228 | } |
220 | 229 | |
221 | return h.handlePUTPOST(w, r, nil) | |
230 | return h.handlePOST(w, r, nil) | |
222 | 231 | } |
223 | 232 | |
224 | 233 | var data map[string]interface{} |
237 | 246 | return &HTTPError{http.StatusUnauthorized, nil} |
238 | 247 | } |
239 | 248 | |
240 | if r.Method == "PUT" { | |
241 | return h.handlePUTPOST(w, r, meta) | |
249 | if r.Method == "POST" { | |
250 | return h.handlePOST(w, r, meta) | |
242 | 251 | } else if r.Method == "GET" { |
243 | 252 | w.Header().Set("Content-Type", "application/json") |
244 | 253 | w.Write(raw_data) |