|
|
@@ -12,30 +12,30 @@ import (
|
|
|
)
|
|
|
|
|
|
type (
|
|
|
- RoutesInterface interface {
|
|
|
- routesInterface
|
|
|
+ IRouter interface {
|
|
|
+ IRoutes
|
|
|
Group(string, ...HandlerFunc) *RouterGroup
|
|
|
}
|
|
|
|
|
|
- routesInterface interface {
|
|
|
- Use(...HandlerFunc) 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
|
|
|
+ IRoutes interface {
|
|
|
+ Use(...HandlerFunc) IRoutes
|
|
|
+
|
|
|
+ Handle(string, string, ...HandlerFunc) IRoutes
|
|
|
+ Any(string, ...HandlerFunc) IRoutes
|
|
|
+ GET(string, ...HandlerFunc) IRoutes
|
|
|
+ POST(string, ...HandlerFunc) IRoutes
|
|
|
+ DELETE(string, ...HandlerFunc) IRoutes
|
|
|
+ PATCH(string, ...HandlerFunc) IRoutes
|
|
|
+ PUT(string, ...HandlerFunc) IRoutes
|
|
|
+ OPTIONS(string, ...HandlerFunc) IRoutes
|
|
|
+ HEAD(string, ...HandlerFunc) IRoutes
|
|
|
+
|
|
|
+ StaticFile(string, string) IRoutes
|
|
|
+ Static(string, string) IRoutes
|
|
|
+ StaticFS(string, http.FileSystem) IRoutes
|
|
|
}
|
|
|
|
|
|
- // Used internally to configure router, a RouterGroup is associated with a prefix
|
|
|
+ // RouterGroup is used internally to configure router, a RouterGroup is associated with a prefix
|
|
|
// and an array of handlers (middlewares)
|
|
|
RouterGroup struct {
|
|
|
Handlers HandlersChain
|
|
|
@@ -45,15 +45,15 @@ type (
|
|
|
}
|
|
|
)
|
|
|
|
|
|
-var _ RoutesInterface = &RouterGroup{}
|
|
|
+var _ IRouter = &RouterGroup{}
|
|
|
|
|
|
-// Adds middlewares to the group, see example code in github.
|
|
|
-func (group *RouterGroup) Use(middlewares ...HandlerFunc) routesInterface {
|
|
|
+// Use adds middlewares to the group, see example code in github.
|
|
|
+func (group *RouterGroup) Use(middlewares ...HandlerFunc) IRoutes {
|
|
|
group.Handlers = append(group.Handlers, middlewares...)
|
|
|
return group.returnObj()
|
|
|
}
|
|
|
|
|
|
-// Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
|
|
+// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
|
|
|
// 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{
|
|
|
@@ -63,6 +63,13 @@ func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *R
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) IRoutes {
|
|
|
+ absolutePath := group.calculateAbsolutePath(relativePath)
|
|
|
+ handlers = group.combineHandlers(handlers)
|
|
|
+ group.engine.addRoute(httpMethod, absolutePath, handlers)
|
|
|
+ return group.returnObj()
|
|
|
+}
|
|
|
+
|
|
|
// Handle registers a new request handle and middlewares with the given path and method.
|
|
|
// The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes.
|
|
|
// See the example code in github.
|
|
|
@@ -73,14 +80,7 @@ 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 {
|
|
|
- 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) IRoutes {
|
|
|
if matches, err := regexp.MatchString("^[A-Z]+$", httpMethod); !matches || err != nil {
|
|
|
panic("http method " + httpMethod + " is not valid")
|
|
|
}
|
|
|
@@ -88,42 +88,43 @@ 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) IRoutes {
|
|
|
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) IRoutes {
|
|
|
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) IRoutes {
|
|
|
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) IRoutes {
|
|
|
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) IRoutes {
|
|
|
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) IRoutes {
|
|
|
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) IRoutes {
|
|
|
return group.handle("HEAD", relativePath, handlers)
|
|
|
}
|
|
|
|
|
|
-func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) routesInterface {
|
|
|
- // GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
|
|
|
+// Any registers a route that matches all the HTTP methods.
|
|
|
+// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
|
|
|
+func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {
|
|
|
group.handle("GET", relativePath, handlers)
|
|
|
group.handle("POST", relativePath, handlers)
|
|
|
group.handle("PUT", relativePath, handlers)
|
|
|
@@ -136,7 +137,9 @@ func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) rout
|
|
|
return group.returnObj()
|
|
|
}
|
|
|
|
|
|
-func (group *RouterGroup) StaticFile(relativePath, filepath string) routesInterface {
|
|
|
+// StaticFile registers a single route in order to server a single file of the local filesystem.
|
|
|
+// router.StaticFile("favicon.ico", "./resources/favicon.ico")
|
|
|
+func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes {
|
|
|
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
|
|
|
panic("URL parameters can not be used when serving a static file")
|
|
|
}
|
|
|
@@ -154,11 +157,13 @@ 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) IRoutes {
|
|
|
return group.StaticFS(relativePath, Dir(root, false))
|
|
|
}
|
|
|
|
|
|
-func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) routesInterface {
|
|
|
+// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.
|
|
|
+// Gin by default user: gin.Dir()
|
|
|
+func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes {
|
|
|
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
|
|
|
panic("URL parameters can not be used when serving a static folder")
|
|
|
}
|
|
|
@@ -185,7 +190,7 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
|
|
|
|
|
|
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
|
|
|
finalSize := len(group.Handlers) + len(handlers)
|
|
|
- if finalSize >= int(AbortIndex) {
|
|
|
+ if finalSize >= int(abortIndex) {
|
|
|
panic("too many handlers")
|
|
|
}
|
|
|
mergedHandlers := make(HandlersChain, finalSize)
|
|
|
@@ -198,10 +203,9 @@ func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
|
|
|
return joinPaths(group.BasePath, relativePath)
|
|
|
}
|
|
|
|
|
|
-func (group *RouterGroup) returnObj() routesInterface {
|
|
|
+func (group *RouterGroup) returnObj() IRoutes {
|
|
|
if group.root {
|
|
|
return group.engine
|
|
|
- } else {
|
|
|
- return group
|
|
|
}
|
|
|
+ return group
|
|
|
}
|