Skip to content

Commit 2cafdf3

Browse files
committed
ver 0.8.3
Remove Config#Done function add Config#Groug function add comment #3
1 parent cb6c822 commit 2cafdf3

4 files changed

Lines changed: 28 additions & 15 deletions

File tree

middleware/ratelimiter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ func main() {
1818
web.Pre(middleware.NewRateLimiter(rate.NewLimiter(rate.Every(100), 1000)))
1919
2020
web.Config().
21-
Get("/hello", twig.HelloTwig).
22-
Done()
21+
Get("/hello", twig.HelloTwig)
2322
2423
web.Start()
2524

radix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ func (c *radixTreeCtx) Params() UrlParams {
214214
return pms
215215
}
216216

217-
func (c *radixTreeCtx) URL(name string, i ...interface{}) string {
217+
func (c *radixTreeCtx) URL(name string, i ...interface{}) (url string) {
218218
if route, ok := c.tree.routes[name]; ok {
219-
return reverse(route.Path(), i...)
219+
url = reverse(route.Path(), i...)
220220
}
221-
return ""
221+
return
222222
}
223223

224224
type RadixTree struct {

twig.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sync"
88
)
99

10-
const Version = "0.8.3-dev"
10+
const Version = "0.8.3"
1111

1212
// Identifier 标识符接口
1313
type Identifier interface {
@@ -218,5 +218,8 @@ func (t *Twig) SetType(typ string) {
218218

219219
// Config 创建并返回Config工具
220220
func (t *Twig) Config() *Cfg {
221-
return Config(t).WithNamer(t)
221+
return &Cfg{
222+
N: t,
223+
R: t,
224+
}
222225
}

util.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ type Mounter interface {
1212
Mount(Register)
1313
}
1414

15+
// MountFunc Mount函数用于简化配置
16+
type MountFunc func(Register)
17+
18+
func (m MountFunc) Mount(r Register) {
19+
m(r)
20+
}
21+
1522
// 获取当前请求路径
1623
func GetReqPath(r *http.Request) string {
1724
path := r.URL.RawPath
@@ -30,33 +37,34 @@ func Attach(i interface{}, t *Twig) {
3037
}
3138
}
3239

40+
// Cfg Twig路由配置工具
41+
// 对当前的Register和Namer进行配置
3342
type Cfg struct {
3443
R Register
3544
N Namer
3645
}
3746

47+
// Config 指定Register创建Config
3848
func Config(r Register) *Cfg {
3949
return &Cfg{
4050
R: r,
4151
N: nil,
4252
}
4353
}
4454

45-
func (c *Cfg) WithNamer(n Namer) *Cfg {
46-
c.N = n
47-
return c
48-
}
49-
55+
// SetName 设置当前Namer的名称
5056
func (c *Cfg) SetName(name string) *Cfg {
5157
c.N.SetName(name)
5258
return c
5359
}
5460

61+
// Use 当前Register增加中间件
5562
func (c *Cfg) Use(m ...MiddlewareFunc) *Cfg {
5663
c.R.Use(m...)
5764
return c
5865
}
5966

67+
// AddHandler 增加Handler
6068
func (c *Cfg) AddHandler(method, path string, handler HandlerFunc, m ...MiddlewareFunc) *Cfg {
6169
c.N = c.R.AddHandler(method, path, handler, m...)
6270
return c
@@ -94,19 +102,22 @@ func (c *Cfg) Trace(path string, handler HandlerFunc, m ...MiddlewareFunc) *Cfg
94102
return c.AddHandler(TRACE, path, handler, m...)
95103
}
96104

105+
// Mount 挂载Mounter到当前Register
97106
func (c *Cfg) Mount(mount Mounter) *Cfg {
98107
mount.Mount(c.R)
99108
c.N = nil
100109
return c
101110
}
102111

112+
// Static 增加静态路由
103113
func (c *Cfg) Static(path, file string, m ...MiddlewareFunc) *Cfg {
104114
return c.Get(path, Static(file), m...)
105115
}
106116

107-
func (c *Cfg) Done() {
108-
c.R = nil
109-
c.N = nil
117+
// Group 配置路由组
118+
func (c *Cfg) Group(path string, m MountFunc) *Cfg {
119+
m(NewGroup(c.R, path))
120+
return c
110121
}
111122

112123
// Group 提供理由分组支持

0 commit comments

Comments
 (0)