init post handler
()
| 273 | |
| 274 | //init post handler |
| 275 | func (this *restServer) initPostHandler() { |
| 276 | for k, _ := range this.postMap { |
| 277 | this.router.Post(k, func(w http.ResponseWriter, r *http.Request) { |
| 278 | decoder := json.NewDecoder(io.LimitReader(r.Body, common.MAX_REQUEST_BODY_SIZE)) |
| 279 | defer r.Body.Close() |
| 280 | var req = make(map[string]interface{}) |
| 281 | var resp map[string]interface{} |
| 282 | |
| 283 | url := this.getPath(r.URL.Path) |
| 284 | if h, ok := this.postMap[url]; ok { |
| 285 | if err := decoder.Decode(&req); err == nil { |
| 286 | req = this.getParams(r, url, req) |
| 287 | resp = h.handler(req) |
| 288 | resp["Action"] = h.name |
| 289 | } else { |
| 290 | resp = rest.ResponsePack(berr.ILLEGAL_DATAFORMAT) |
| 291 | resp["Action"] = h.name |
| 292 | } |
| 293 | } else { |
| 294 | resp = rest.ResponsePack(berr.INVALID_METHOD) |
| 295 | } |
| 296 | this.response(w, resp) |
| 297 | }) |
| 298 | } |
| 299 | //Options |
| 300 | for k, _ := range this.postMap { |
| 301 | this.router.Options(k, func(w http.ResponseWriter, r *http.Request) { |
| 302 | this.write(w, []byte{}) |
| 303 | }) |
| 304 | } |
| 305 | |
| 306 | } |
| 307 | func (this *restServer) write(w http.ResponseWriter, data []byte) { |
| 308 | w.Header().Add("Access-Control-Allow-Headers", "Content-Type") |
| 309 | w.Header().Set("content-type", "application/json;charset=utf-8") |
no test coverage detected