Renames RouterGroup.absolutePath to .BasePath

This commit is contained in:
Manu Mtz-Almeida 2015-05-16 18:08:19 +02:00
parent 05d01d2282
commit 3066c35754
4 changed files with 17 additions and 17 deletions

4
gin.go
View File

@ -65,8 +65,8 @@ func New() *Engine {
debugPrintWARNING()
engine := &Engine{
RouterGroup: RouterGroup{
Handlers: nil,
absolutePath: "/",
Handlers: nil,
BasePath: "/",
},
RedirectTrailingSlash: true,
RedirectFixedPath: true,

View File

@ -22,7 +22,7 @@ func init() {
func TestCreateEngine(t *testing.T) {
router := New()
assert.Equal(t, "/", router.absolutePath)
assert.Equal(t, "/", router.BasePath)
assert.Equal(t, router.engine, router)
assert.Empty(t, router.Handlers)
assert.True(t, router.RedirectTrailingSlash)

View File

@ -12,9 +12,9 @@ import (
// Used internally to configure router, a RouterGroup is associated with a prefix
// and an array of handlers (middlewares)
type RouterGroup struct {
Handlers HandlersChain
absolutePath string
engine *Engine
Handlers HandlersChain
BasePath string
engine *Engine
}
// Adds middlewares to the group, see example code in github.
@ -26,9 +26,9 @@ func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
// For example, all the routes that use a common middlware for authorization could be grouped.
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
return &RouterGroup{
Handlers: group.combineHandlers(handlers),
absolutePath: group.calculateAbsolutePath(relativePath),
engine: group.engine,
Handlers: group.combineHandlers(handlers),
BasePath: group.calculateAbsolutePath(relativePath),
engine: group.engine,
}
}
@ -101,8 +101,7 @@ func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) {
// use :
// router.Static("/static", "/var/www")
func (group *RouterGroup) Static(relativePath, root string) {
absolutePath := group.calculateAbsolutePath(relativePath)
handler := group.createStaticHandler(absolutePath, root)
handler := group.createStaticHandler(relativePath, root)
relativePath = path.Join(relativePath, "/*filepath")
// Register GET and HEAD handlers
@ -110,7 +109,8 @@ func (group *RouterGroup) Static(relativePath, root string) {
group.HEAD(relativePath, handler)
}
func (group *RouterGroup) createStaticHandler(absolutePath, root string) func(*Context) {
func (group *RouterGroup) createStaticHandler(relativePath, root string) func(*Context) {
absolutePath := group.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root)))
return func(c *Context) {
fileServer.ServeHTTP(c.Writer, c.Request)
@ -126,5 +126,5 @@ func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain
}
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
return joinPaths(group.absolutePath, relativePath)
return joinPaths(group.BasePath, relativePath)
}

View File

@ -20,14 +20,14 @@ func TestRouterGroupBasic(t *testing.T) {
group.Use(func(c *Context) {})
assert.Len(t, group.Handlers, 2)
assert.Equal(t, group.absolutePath, "/hola")
assert.Equal(t, group.BasePath, "/hola")
assert.Equal(t, group.engine, router)
group2 := group.Group("manu")
group2.Use(func(c *Context) {}, func(c *Context) {})
assert.Len(t, group2.Handlers, 4)
assert.Equal(t, group2.absolutePath, "/hola/manu")
assert.Equal(t, group2.BasePath, "/hola/manu")
assert.Equal(t, group2.engine, router)
}
@ -47,10 +47,10 @@ func TestRouterGroupBasicHandle(t *testing.T) {
func performRequestInGroup(t *testing.T, method string) {
router := New()
v1 := router.Group("v1", func(c *Context) {})
assert.Equal(t, v1.absolutePath, "/v1")
assert.Equal(t, v1.BasePath, "/v1")
login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {})
assert.Equal(t, login.absolutePath, "/v1/login/")
assert.Equal(t, login.BasePath, "/v1/login/")
handler := func(c *Context) {
c.String(400, "the method was %s and index %d", c.Request.Method, c.index)