Skip to content

Commit

Permalink
ver 0.8.3
Browse files Browse the repository at this point in the history
Remove Config#Done function
add Config#Groug function
add comment #3
  • Loading branch information
it512 committed Feb 12, 2019
1 parent cb6c822 commit 2cafdf3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
3 changes: 1 addition & 2 deletions middleware/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func main() {
web.Pre(middleware.NewRateLimiter(rate.NewLimiter(rate.Every(100), 1000)))
web.Config().
Get("/hello", twig.HelloTwig).
Done()
Get("/hello", twig.HelloTwig)
web.Start()
Expand Down
6 changes: 3 additions & 3 deletions radix.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ func (c *radixTreeCtx) Params() UrlParams {
return pms
}

func (c *radixTreeCtx) URL(name string, i ...interface{}) string {
func (c *radixTreeCtx) URL(name string, i ...interface{}) (url string) {
if route, ok := c.tree.routes[name]; ok {
return reverse(route.Path(), i...)
url = reverse(route.Path(), i...)
}
return ""
return
}

type RadixTree struct {
Expand Down
7 changes: 5 additions & 2 deletions twig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
)

const Version = "0.8.3-dev"
const Version = "0.8.3"

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

// Config 创建并返回Config工具
func (t *Twig) Config() *Cfg {
return Config(t).WithNamer(t)
return &Cfg{
N: t,
R: t,
}
}
27 changes: 19 additions & 8 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type Mounter interface {
Mount(Register)
}

// MountFunc Mount函数用于简化配置
type MountFunc func(Register)

func (m MountFunc) Mount(r Register) {
m(r)
}

// 获取当前请求路径
func GetReqPath(r *http.Request) string {
path := r.URL.RawPath
Expand All @@ -30,33 +37,34 @@ func Attach(i interface{}, t *Twig) {
}
}

// Cfg Twig路由配置工具
// 对当前的Register和Namer进行配置
type Cfg struct {
R Register
N Namer
}

// Config 指定Register创建Config
func Config(r Register) *Cfg {
return &Cfg{
R: r,
N: nil,
}
}

func (c *Cfg) WithNamer(n Namer) *Cfg {
c.N = n
return c
}

// SetName 设置当前Namer的名称
func (c *Cfg) SetName(name string) *Cfg {
c.N.SetName(name)
return c
}

// Use 当前Register增加中间件
func (c *Cfg) Use(m ...MiddlewareFunc) *Cfg {
c.R.Use(m...)
return c
}

// AddHandler 增加Handler
func (c *Cfg) AddHandler(method, path string, handler HandlerFunc, m ...MiddlewareFunc) *Cfg {
c.N = c.R.AddHandler(method, path, handler, m...)
return c
Expand Down Expand Up @@ -94,19 +102,22 @@ func (c *Cfg) Trace(path string, handler HandlerFunc, m ...MiddlewareFunc) *Cfg
return c.AddHandler(TRACE, path, handler, m...)
}

// Mount 挂载Mounter到当前Register
func (c *Cfg) Mount(mount Mounter) *Cfg {
mount.Mount(c.R)
c.N = nil
return c
}

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

func (c *Cfg) Done() {
c.R = nil
c.N = nil
// Group 配置路由组
func (c *Cfg) Group(path string, m MountFunc) *Cfg {
m(NewGroup(c.R, path))
return c
}

// Group 提供理由分组支持
Expand Down

0 comments on commit 2cafdf3

Please sign in to comment.