RoutesInterface includes Group()

This commit is contained in:
Manu Mtz-Almeida 2015-06-26 16:01:35 +02:00
parent 88a65eacff
commit 9e44079756
2 changed files with 38 additions and 29 deletions

4
gin.go
View File

@ -70,6 +70,8 @@ type (
}
)
var _ RoutesInterface = &Engine{}
func (c HandlersChain) Last() HandlerFunc {
length := len(c)
if length > 0 {
@ -157,7 +159,7 @@ func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
// Attachs a global middleware to the router. ie. the middlewares attached though Use() will be
// included in the handlers chain for every single request. Even 404, 405, static files...
// For example, this is the right place for a logger or error management middleware.
func (engine *Engine) Use(middlewares ...HandlerFunc) RoutesInterface {
func (engine *Engine) Use(middlewares ...HandlerFunc) routesInterface {
engine.RouterGroup.Use(middlewares...)
engine.rebuild404Handlers()
engine.rebuild405Handlers()

View File

@ -12,21 +12,26 @@ import (
)
type RoutesInterface interface {
Use(...HandlerFunc) RoutesInterface
routesInterface
Group(string, ...HandlerFunc) *RouterGroup
}
Handle(string, string, ...HandlerFunc) RoutesInterface
Any(string, ...HandlerFunc) RoutesInterface
GET(string, ...HandlerFunc) RoutesInterface
POST(string, ...HandlerFunc) RoutesInterface
DELETE(string, ...HandlerFunc) RoutesInterface
PATCH(string, ...HandlerFunc) RoutesInterface
PUT(string, ...HandlerFunc) RoutesInterface
OPTIONS(string, ...HandlerFunc) RoutesInterface
HEAD(string, ...HandlerFunc) RoutesInterface
type routesInterface interface {
Use(...HandlerFunc) routesInterface
StaticFile(string, string) RoutesInterface
Static(string, string) RoutesInterface
StaticFS(string, http.FileSystem) RoutesInterface
Handle(string, string, ...HandlerFunc) routesInterface
Any(string, ...HandlerFunc) routesInterface
GET(string, ...HandlerFunc) routesInterface
POST(string, ...HandlerFunc) routesInterface
DELETE(string, ...HandlerFunc) routesInterface
PATCH(string, ...HandlerFunc) routesInterface
PUT(string, ...HandlerFunc) routesInterface
OPTIONS(string, ...HandlerFunc) routesInterface
HEAD(string, ...HandlerFunc) routesInterface
StaticFile(string, string) routesInterface
Static(string, string) routesInterface
StaticFS(string, http.FileSystem) routesInterface
}
// Used internally to configure router, a RouterGroup is associated with a prefix
@ -38,8 +43,10 @@ type RouterGroup struct {
root bool
}
var _ RoutesInterface = &RouterGroup{}
// Adds middlewares to the group, see example code in github.
func (group *RouterGroup) Use(middlewares ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) Use(middlewares ...HandlerFunc) routesInterface {
group.Handlers = append(group.Handlers, middlewares...)
return group.returnObj()
}
@ -64,14 +71,14 @@ func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *R
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) RoutesInterface {
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) routesInterface {
absolutePath := group.calculateAbsolutePath(relativePath)
handlers = group.combineHandlers(handlers)
group.engine.addRoute(httpMethod, absolutePath, handlers)
return group.returnObj()
}
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) routesInterface {
if matches, err := regexp.MatchString("^[A-Z]+$", httpMethod); !matches || err != nil {
panic("http method " + httpMethod + " is not valid")
}
@ -79,41 +86,41 @@ func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...Ha
}
// POST is a shortcut for router.Handle("POST", path, handle)
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) routesInterface {
return group.handle("POST", relativePath, handlers)
}
// GET is a shortcut for router.Handle("GET", path, handle)
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) routesInterface {
return group.handle("GET", relativePath, handlers)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) routesInterface {
return group.handle("DELETE", relativePath, handlers)
}
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) routesInterface {
return group.handle("PATCH", relativePath, handlers)
}
// PUT is a shortcut for router.Handle("PUT", path, handle)
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) routesInterface {
return group.handle("PUT", relativePath, handlers)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) routesInterface {
return group.handle("OPTIONS", relativePath, handlers)
}
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) routesInterface {
return group.handle("HEAD", relativePath, handlers)
}
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) RoutesInterface {
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) routesInterface {
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
group.handle("GET", relativePath, handlers)
group.handle("POST", relativePath, handlers)
@ -127,7 +134,7 @@ func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) Rout
return group.returnObj()
}
func (group *RouterGroup) StaticFile(relativePath, filepath string) RoutesInterface {
func (group *RouterGroup) StaticFile(relativePath, filepath string) routesInterface {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static file")
}
@ -145,11 +152,11 @@ func (group *RouterGroup) StaticFile(relativePath, filepath string) RoutesInterf
// To use the operating system's file system implementation,
// use :
// router.Static("/static", "/var/www")
func (group *RouterGroup) Static(relativePath, root string) RoutesInterface {
func (group *RouterGroup) Static(relativePath, root string) routesInterface {
return group.StaticFS(relativePath, Dir(root, false))
}
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) RoutesInterface {
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) routesInterface {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static folder")
}
@ -189,7 +196,7 @@ func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
return joinPaths(group.BasePath, relativePath)
}
func (group *RouterGroup) returnObj() RoutesInterface {
func (group *RouterGroup) returnObj() routesInterface {
if group.root {
return group.engine
} else {