-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (68 loc) · 1.9 KB
/
main.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
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"net/http"
"github.com/tonny-zhang/cotton"
)
func main() {
router := cotton.NewRouter()
router.Use(cotton.Recover())
{
d1 := router.Domain("www.pilin.com", func(ctx *cotton.Context) {
fmt.Println("middleware d1", ctx.Request.Host)
}, cotton.LoggerWidthConf(cotton.LoggerConf{
Formatter: func(param cotton.LoggerFormatterParam, ctx *cotton.Context) string {
return fmt.Sprintf("[www-INFO] %v\t%13s %6s %3d %10v %s \n",
param.TimeStamp.Format("2006/01/02 15:04:05"),
param.ClientIP,
param.Method,
param.StatusCode,
param.Latency,
param.Path,
)
},
}))
d1.NotFound(func(ctx *cotton.Context) {
ctx.String(http.StatusNotFound, "d1 page not found")
})
d1.Get("/test", func(ctx *cotton.Context) {
ctx.String(http.StatusOK, "www test")
})
g1 := d1.Group("/v1")
g1.NotFound(func(ctx *cotton.Context) {
ctx.String(http.StatusOK, "d1 v1 page not found")
})
g1.Get("/test", func(ctx *cotton.Context) {
ctx.String(http.StatusOK, "www v1 test")
})
}
{
d2 := router.Domain("a.pilin.com", cotton.RecoverWithWriter(nil, func(ctx *cotton.Context, err interface{}) {
fmt.Println("[a-recover]", err)
}), cotton.LoggerWidthConf(cotton.LoggerConf{
Formatter: func(param cotton.LoggerFormatterParam, ctx *cotton.Context) string {
return fmt.Sprintf("[a-INFO] %v\t%13s %6s %3d %10v %s \n",
param.TimeStamp.Format("2006/01/02 15:04:05"),
param.ClientIP,
param.Method,
param.StatusCode,
param.Latency,
param.Path,
)
},
}))
d2.NotFound(func(ctx *cotton.Context) {
ctx.String(http.StatusNotFound, "d2 page not found")
})
d2.Get("/test", func(ctx *cotton.Context) {
ctx.String(http.StatusOK, "a test")
})
d2.Get("/panic", func(ctx *cotton.Context) {
panic("test")
})
}
router.Get("/test", func(ctx *cotton.Context) {
ctx.String(http.StatusOK, "test")
})
router.Run("")
}