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() debugPrintWARNING()
engine := &Engine{ engine := &Engine{
RouterGroup: RouterGroup{ RouterGroup: RouterGroup{
Handlers: nil, Handlers: nil,
absolutePath: "/", BasePath: "/",
}, },
RedirectTrailingSlash: true, RedirectTrailingSlash: true,
RedirectFixedPath: true, RedirectFixedPath: true,

View File

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

View File

@ -12,9 +12,9 @@ import (
// Used internally to configure router, a RouterGroup is associated with a prefix // Used internally to configure router, a RouterGroup is associated with a prefix
// and an array of handlers (middlewares) // and an array of handlers (middlewares)
type RouterGroup struct { type RouterGroup struct {
Handlers HandlersChain Handlers HandlersChain
absolutePath string BasePath string
engine *Engine engine *Engine
} }
// Adds middlewares to the group, see example code in github. // 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. // For example, all the routes that use a common middlware for authorization could be grouped.
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup { func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
return &RouterGroup{ return &RouterGroup{
Handlers: group.combineHandlers(handlers), Handlers: group.combineHandlers(handlers),
absolutePath: group.calculateAbsolutePath(relativePath), BasePath: group.calculateAbsolutePath(relativePath),
engine: group.engine, engine: group.engine,
} }
} }
@ -101,8 +101,7 @@ func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) {
// use : // use :
// router.Static("/static", "/var/www") // router.Static("/static", "/var/www")
func (group *RouterGroup) Static(relativePath, root string) { func (group *RouterGroup) Static(relativePath, root string) {
absolutePath := group.calculateAbsolutePath(relativePath) handler := group.createStaticHandler(relativePath, root)
handler := group.createStaticHandler(absolutePath, root)
relativePath = path.Join(relativePath, "/*filepath") relativePath = path.Join(relativePath, "/*filepath")
// Register GET and HEAD handlers // Register GET and HEAD handlers
@ -110,7 +109,8 @@ func (group *RouterGroup) Static(relativePath, root string) {
group.HEAD(relativePath, handler) 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))) fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root)))
return func(c *Context) { return func(c *Context) {
fileServer.ServeHTTP(c.Writer, c.Request) fileServer.ServeHTTP(c.Writer, c.Request)
@ -126,5 +126,5 @@ func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain
} }
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string { 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) {}) group.Use(func(c *Context) {})
assert.Len(t, group.Handlers, 2) assert.Len(t, group.Handlers, 2)
assert.Equal(t, group.absolutePath, "/hola") assert.Equal(t, group.BasePath, "/hola")
assert.Equal(t, group.engine, router) assert.Equal(t, group.engine, router)
group2 := group.Group("manu") group2 := group.Group("manu")
group2.Use(func(c *Context) {}, func(c *Context) {}) group2.Use(func(c *Context) {}, func(c *Context) {})
assert.Len(t, group2.Handlers, 4) 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) assert.Equal(t, group2.engine, router)
} }
@ -47,10 +47,10 @@ func TestRouterGroupBasicHandle(t *testing.T) {
func performRequestInGroup(t *testing.T, method string) { func performRequestInGroup(t *testing.T, method string) {
router := New() router := New()
v1 := router.Group("v1", func(c *Context) {}) 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) {}) 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) { handler := func(c *Context) {
c.String(400, "the method was %s and index %d", c.Request.Method, c.index) c.String(400, "the method was %s and index %d", c.Request.Method, c.index)