func DataTopHandle(ctx *wk.HttpContext) (result wk.HttpResult, err error) { if count, ok := ctx.RouteData.Int("count"); !ok { err = errors.New("parameter invalid:" + "count") } else { data := DataTop(count) result = wk.Json(data) } return }
func FileAjax(ctx *wk.HttpContext) (result wk.HttpResult, err error) { ctx.Request.ParseMultipartForm(32 << 20) // 32 MB status := UploadStatus{make([]FileInfo, 0)} for _, headers := range ctx.Request.MultipartForm.File { for _, header := range headers { info := fileInfo(header) status.Files = append(status.Files, info) } } return wk.Json(status), nil }
func TestJsonResult(t *testing.T) { data := defaultUser() j := wk.Json(data) var result wk.HttpResult = j t.Log("JsonResult", result) header := make(http.Header) body := &bytes.Buffer{} var render wk.Render = j err := render.Write(header, body) success(t, "json write", err) equal(t, "json header", wk.ContentTypeJson, header.Get(wk.HeaderContentType)) equal(t, "json body", tojson(data), body.String()) }
// get: /user/delete/1 func (uc *UserController) Delete(ctx *wk.HttpContext) (result wk.HttpResult, err error) { var res ApiRes if id, ok := ctx.RouteData.Int("arg"); ok { res = ApiRes{ Code: "", Data: userRepo.Delete(id), } } else { res = ApiRes{ Code: "1", Message: " Invalid Argument", } } return wk.Json(res), nil }
// url: /basic/all/ func (c *BasicController) All(ctx *wk.HttpContext) (result wk.HttpResult, err error) { return wk.Json(c.data), nil }
// url: /basic/int/32 func (c *BasicController) Int(ctx *wk.HttpContext) (result wk.HttpResult, err error) { if id, ok := ctx.RouteData.Int("id"); ok { return wk.Json(c.getByInt(id)), nil } return wk.Data(""), nil }