woodpecker/vendor/github.com/gin-gonic/gin/routergroup.go

216 lines
7.5 KiB
Go
Raw Normal View History

2015-05-22 18:37:40 +00:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"net/http"
"path"
2015-09-30 01:21:17 +00:00
"regexp"
"strings"
2015-05-22 18:37:40 +00:00
)
2015-09-30 01:21:17 +00:00
type (
IRouter interface {
IRoutes
Group(string, ...HandlerFunc) *RouterGroup
}
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
}
2015-05-22 18:37:40 +00:00
2015-09-30 01:21:17 +00:00
// RouterGroup is used internally to configure router, a RouterGroup is associated with a prefix
// and an array of handlers (middleware)
RouterGroup struct {
Handlers HandlersChain
basePath string
engine *Engine
root bool
}
)
var _ IRouter = &RouterGroup{}
// Use adds middleware to the group, see example code in github.
func (group *RouterGroup) Use(middleware ...HandlerFunc) IRoutes {
group.Handlers = append(group.Handlers, middleware...)
return group.returnObj()
2015-05-22 18:37:40 +00:00
}
2015-09-30 01:21:17 +00:00
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
2015-05-22 18:37:40 +00:00
// 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),
2015-09-30 01:21:17 +00:00
basePath: group.calculateAbsolutePath(relativePath),
2015-05-22 18:37:40 +00:00
engine: group.engine,
}
}
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) BasePath() string {
return group.basePath
}
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 middleware with the given path and method.
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
2015-05-22 18:37:40 +00:00
// See the example code in github.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// 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).
2015-09-30 01:21:17 +00:00
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")
}
return group.handle(httpMethod, relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
// POST is a shortcut for router.Handle("POST", path, handle)
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("POST", relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
// GET is a shortcut for router.Handle("GET", path, handle)
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("GET", relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("DELETE", relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("PATCH", relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
// PUT is a shortcut for router.Handle("PUT", path, handle)
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("PUT", relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("OPTIONS", relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("HEAD", relativePath, handlers)
2015-05-22 18:37:40 +00:00
}
2015-09-30 01:21:17 +00:00
// 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 {
2015-05-22 18:37:40 +00:00
group.handle("GET", relativePath, handlers)
group.handle("POST", relativePath, handlers)
group.handle("PUT", relativePath, handlers)
group.handle("PATCH", relativePath, handlers)
group.handle("HEAD", relativePath, handlers)
group.handle("OPTIONS", relativePath, handlers)
group.handle("DELETE", relativePath, handlers)
group.handle("CONNECT", relativePath, handlers)
group.handle("TRACE", relativePath, handlers)
2015-09-30 01:21:17 +00:00
return group.returnObj()
}
// 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")
}
handler := func(c *Context) {
c.File(filepath)
}
group.GET(relativePath, handler)
group.HEAD(relativePath, handler)
return group.returnObj()
2015-05-22 18:37:40 +00:00
}
// Static serves files from the given file system root.
// Internally a http.FileServer is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// To use the operating system's file system implementation,
// use :
// router.Static("/static", "/var/www")
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) Static(relativePath, root string) IRoutes {
return group.StaticFS(relativePath, Dir(root, false))
2015-05-22 18:37:40 +00:00
}
2015-09-30 01:21:17 +00:00
// 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")
}
2015-05-22 18:37:40 +00:00
handler := group.createStaticHandler(relativePath, fs)
2015-09-30 01:21:17 +00:00
urlPattern := path.Join(relativePath, "/*filepath")
2015-05-22 18:37:40 +00:00
// Register GET and HEAD handlers
2015-09-30 01:21:17 +00:00
group.GET(urlPattern, handler)
group.HEAD(urlPattern, handler)
return group.returnObj()
2015-05-22 18:37:40 +00:00
}
2015-09-30 01:21:17 +00:00
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
2015-05-22 18:37:40 +00:00
absolutePath := group.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
2015-09-30 01:21:17 +00:00
_, nolisting := fs.(*onlyfilesFS)
return func(c *Context) {
if nolisting {
c.Writer.WriteHeader(404)
}
fileServer.ServeHTTP(c.Writer, c.Request)
}
2015-05-22 18:37:40 +00:00
}
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
finalSize := len(group.Handlers) + len(handlers)
2015-09-30 01:21:17 +00:00
if finalSize >= int(abortIndex) {
panic("too many handlers")
}
2015-05-22 18:37:40 +00:00
mergedHandlers := make(HandlersChain, finalSize)
copy(mergedHandlers, group.Handlers)
copy(mergedHandlers[len(group.Handlers):], handlers)
return mergedHandlers
}
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
2015-09-30 01:21:17 +00:00
return joinPaths(group.basePath, relativePath)
}
func (group *RouterGroup) returnObj() IRoutes {
if group.root {
return group.engine
}
return group
2015-05-22 18:37:40 +00:00
}