/
group_middleware.go
76 lines (70 loc) · 1.94 KB
/
group_middleware.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
72
73
74
75
76
// Copyright 2017 King Qiu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
// https://github.com/qjw/kelly
package main
import (
"github.com/qjw/kelly"
"net/http"
)
func Middleware(title, ver string, ok bool) kelly.HandlerFunc {
return func(c *kelly.Context) {
if ok {
// 设置header
c.SetHeader(title,ver)
// 设置context参数
c.Set(title, ver)
// 调用下一个handle
c.InvokeNext()
} else {
// 不调用@InvokeNext 意味着中断执行流程
c.WriteIndentedJson(http.StatusForbidden, kelly.H{
"code": http.StatusForbidden,
})
}
}
}
func InitGroupMiddleware(r kelly.Router) {
// 新建一个子router,并注入一个middleware
ar := r.Group(
"/aaa",
Middleware("v2", "v2", true),
)
ar.GET("/", func(c *kelly.Context) {
c.WriteJson(http.StatusOK, kelly.H{ // 返回json(紧凑格式)
"code": "/aaa",
})
})
ar.GET("/a/b/c/d", func(c *kelly.Context) {
c.Redirect(http.StatusMovedPermanently, "/aaa/bbb/a") // 重定向
})
ar.GET("/b/*path", func(c *kelly.Context) {
c.WriteJson(http.StatusOK, kelly.H{ // 返回json(紧凑格式)
"code": c.MustGetPathVarible("path"),
})
})
ar.GET("/c/:path/d", func(c *kelly.Context) {
c.WriteJson(http.StatusOK, kelly.H{ // 返回json(紧凑格式)
"code": c.MustGetPathVarible("path"),
})
})
ar.GET("/d/:path/:path2", func(c *kelly.Context) {
c.WriteJson(http.StatusOK, kelly.H{ // 返回json(紧凑格式)
"code": c.MustGetPathVarible("path"),
"code2": c.MustGetPathVarible("path2"),
})
})
// 新建一个子router,并注入一个middleware
sar := ar.Group(
"/bbb",
Middleware("v3", "v3", true),
)
sar.GET("/", func(c *kelly.Context) {
c.WriteXml(http.StatusOK, kelly.H{ // 返回XML
"code": "/aaa/bbb",
})
})
sar.GET("/a", func(c *kelly.Context) {
c.WriteString(http.StatusOK, "test %d %d", 123, 456) // 返回普通文本
})
}