forked from andeya/faygo
/
param.go
62 lines (55 loc) · 1.73 KB
/
param.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package handler
import (
"mime/multipart"
"net/http"
"sync"
"github.com/henrylee2cn/faygo"
)
type Param struct {
Id int `param:"<in:path> <required> <desc:ID> <range: 0:10>"`
Title string `param:"<in:query> <nonzero>"`
Num float32 `param:"<in:formData> <required> <name:n> <range: 0.1:10> <err: formData param 'n' must be number in 0.1~10>"`
Paragraph []string `param:"<in:formData> <name:p> <len: 1:20> <regexp: ^[\\w&=]*$>"`
Picture *multipart.FileHeader `param:"<in:formData> <name:pic> <maxmb:30>"`
Cookie *http.Cookie `param:"<in:cookie> <name:faygo>"`
CookieString string `param:"<in:cookie> <name:faygo>"`
}
var once sync.Once
// Implement the handler interface
func (p *Param) Serve(ctx *faygo.Context) error {
ctx.Log().Info(ctx.R.Host)
once.Do(func() {
println("Set session...")
ctx.SetSession("name", "henry")
ctx.SetCookie("faygo", "henrylee")
})
ctx.Log().Infof("Get session name=%v", ctx.GetSession("name"))
info, err := ctx.SaveFile("pic", true)
if err == nil {
ctx.Log().Infof("ctx.SaveFile: filename %s url %s, size %d", p.Picture.Filename, info.Url, info.Size)
}
return ctx.JSON(200,
faygo.Map{
"Struct Params": p,
"Additional Param": ctx.PathParam("additional"),
}, true)
// return ctx.String(200, "name=%v", name)
}
// Doc returns the API's note, result or parameters information.
func (p *Param) Doc() faygo.Doc {
return faygo.Doc{
Note: "param desc",
Return: faygo.JSONMsg{
Code: 1,
Info: "success",
},
MoreParams: []faygo.ParamInfo{
{
Name: "additional",
In: "path",
Model: "a",
Desc: "defined by the `Doc()` method",
},
},
}
}