本文整理汇总了Golang中github.com/sdming/wk.HttpContext类的典型用法代码示例。如果您正苦于以下问题:Golang HttpContext类的具体用法?Golang HttpContext怎么用?Golang HttpContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HttpContext类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Exists
// 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
}
示例2: showQrCode
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
}
示例3: Add
// 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
}
示例4: RangeCount
// 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
}
示例5: Execute
// 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
}
示例6: Set
// 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
}
示例7: Post
// 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
}
示例8: EditPost
// 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
}
示例9: printTrace
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())
}
示例10: AddPost
// 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
}
示例11: Remove
// 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
}
示例12: Get
// 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
}
示例13: Add
// 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
}
示例14: Id
// url: /session/id
func (c *Session) Id(ctx *wk.HttpContext) (wk.HttpResult, error) {
id := ctx.SessionId()
return wk.Data(id), nil
}