// get: /user/exists/sheldon func (uc *UserController) Exists(ctx *wk.HttpContext) (result wk.HttpResult, err error) { valid := true if name := ctx.FV("name"); name != "" { valid = !userRepo.Exists(name) } return wk.Data(valid), nil }
func showQrCode(ctx *wk.HttpContext) (result wk.HttpResult, err error) { text := ctx.FV("text") if text == "" { err = errors.New("text is invalid") } else { result = &QrCodeResult{ Text: text, } } return }
// url: /basic/add/?int=32&str=string&uint=1024&float=1.1&byte=64 func (c *BasicController) Add(ctx *wk.HttpContext) (result wk.HttpResult, err error) { data := Data{ Int: ctx.FormIntOr("int", 0), Uint: uint64(ctx.FormIntOr("uint", 0)), Str: ctx.FormValue("str"), Float: float32(ctx.FormFloatOr("float", 0.0)), Byte: byte(ctx.FormIntOr("byte", 0)), } c.data = append(c.data, data) return wk.Data(data.String()), nil }
// url: /basic/rangecount/?start=1&end=99 func (c *BasicController) RangeCount(ctx *wk.HttpContext) (result wk.HttpResult, err error) { start := ctx.FormIntOr("start", 0) end := ctx.FormIntOr("end", 0) var count int = 0 for _, d := range c.data { if d.Int >= start && d.Int <= end { count++ } } return wk.Data(count), nil }
// Execute write qrcode image to response func (qrcode *QrCodeResult) Execute(ctx *wk.HttpContext) error { c, err := qr.Encode(qrcode.Text, qr.M) if err != nil { log.Println("QrCodeResult Execute Error", err) return err } png := c.PNG() //ioutil.WriteFile(qrcode.Text+"_demo.png", png, 0666) ctx.ContentType("image/png") ctx.Write(png) return nil }
// url: /basic/set/32?str=s&uint=64&float=3.14&byte=8 func (c *BasicController) Set(ctx *wk.HttpContext) (result wk.HttpResult, err error) { var id int if i, ok := ctx.RouteData.Int("id"); ok { id = i } else { return wk.Data(0), nil } var count int = 0 for i := 0; i < len(c.data); i++ { d := c.data[i] if d.Int != id { continue } if x := ctx.FormValue("str"); x != "" { d.Str = x } if x, ok := ctx.FormInt("uint"); ok { d.Uint = uint64(x) } if x, ok := ctx.FormFloat("float"); ok { d.Float = float32(x) } if x, ok := ctx.FormInt("byte"); ok { d.Byte = byte(x) } count++ } return wk.Data(count), nil }
// url: post /basic/post/ func (c *BasicController) Post(ctx *wk.HttpContext) (result wk.HttpResult, err error) { var body []byte if body, err = ctx.ReadBody(); err != nil { return nil, err } data := &Data{} err = json.Unmarshal(body, data) if err != nil { return nil, err } c.data = append(c.data, *data) return wk.Data(true), nil }
// post: /user/edit/1 func (uc *UserController) EditPost(ctx *wk.HttpContext) (result wk.HttpResult, err error) { var id int if id, err = parseId(ctx); err != nil { return } u := User{ Id: id, Location: ctx.FV("location"), Gender: ctx.FV("gender"), Skills: ctx.Request.Form["skill"], } if userRepo.Update(u) { return wk.Redirect(fmt.Sprintf("/user/view/%d", id), false), nil } return wk.View("user/edit.html"), nil }
func printTrace(ctx *wk.HttpContext, trace []EventTrace) { if len(trace) == 0 { return } var buffer bytes.Buffer indent := 0 s := " " offset := trace[0].Timestamp buffer.WriteString("\n") //buffer.WriteString(fmt.Sprintf("url:%v \n", ctx.Request.URL)) //buffer.WriteString(strings.Repeat("-", 10)) //buffer.WriteString("\n") buffer.WriteString("<script>\n") buffer.WriteString("var pageProfiler = \"") for _, t := range trace { if strings.HasPrefix(t.Name, "end_") && indent > 0 { indent-- } buffer.WriteString(strings.Repeat(s, indent)) //buffer.WriteString(fmt.Sprintf("%s\t %s\t %d \n", t.Module, t.Name, t.Timestamp-offset)) buffer.WriteString(fmt.Sprintf("%s\t %s\t %d ns \\n", t.Module, t.Name, t.Timestamp-offset)) if strings.HasPrefix(t.Name, "start_") { indent++ } } //buffer.WriteString(strings.Repeat("-", 10)) //buffer.WriteString("\n") buffer.WriteString("\";") buffer.WriteString("\n</script>\n") ctx.Write(buffer.Bytes()) }
// get/post: /user/add func (uc *UserController) AddPost(ctx *wk.HttpContext) (result wk.HttpResult, err error) { id := 0 if name := ctx.FV("name"); name == "" { ctx.ViewData["errmsg"] = "name is required" } else if name == "error" { ctx.ViewData["errmsg"] = "name is error" } else if userRepo.Exists(name) { ctx.ViewData["errmsg"] = "name exists" } else { u := User{ Name: ctx.FV("name"), Location: ctx.FV("location"), Gender: ctx.FV("gender"), Skills: ctx.Request.Form["skill"], } id = userRepo.Add(u) } if id <= 0 { ctx.ViewData["ctx"] = ctx return wk.View("user/add.html"), nil } return wk.Redirect(fmt.Sprintf("/user/view/%d", id), false), nil }
// url: /session/remove?k=test func (c *Session) Remove(ctx *wk.HttpContext) (wk.HttpResult, error) { err := ctx.Session.Remove(ctx.FV("k")) return wk.Data(err == nil), err }
// url: /session/get?k=test func (c *Session) Get(ctx *wk.HttpContext) (result wk.HttpResult, err error) { v, _, err := ctx.Session.Get(ctx.FV("k")) return wk.Data(v), err }
// url: /session/add?k=test&v=101 func (c *Session) Add(ctx *wk.HttpContext) (wk.HttpResult, error) { ok, err := ctx.Session.Add(ctx.FV("k"), ctx.FV("v")) return wk.Data(ok), err }
// url: /session/id func (c *Session) Id(ctx *wk.HttpContext) (wk.HttpResult, error) { id := ctx.SessionId() return wk.Data(id), nil }