gin/routergroup.go

147 lines
5.5 KiB
Go
Raw Normal View History

2014-10-08 23:37:26 +04: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"
)
// Used internally to configure router, a RouterGroup is associated with a prefix
// and an array of handlers (middlewares)
type RouterGroup struct {
Handlers HandlersChain
BasePath string
engine *Engine
2014-10-08 23:37:26 +04:00
}
// Adds middlewares to the group, see example code in github.
func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
group.Handlers = append(group.Handlers, middlewares...)
}
// 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{
Handlers: group.combineHandlers(handlers),
BasePath: group.calculateAbsolutePath(relativePath),
engine: group.engine,
2014-10-08 23:37:26 +04:00
}
}
// 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.
//
// 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-05-07 12:30:01 +03:00
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers HandlersChain) {
2014-10-08 23:37:26 +04:00
absolutePath := group.calculateAbsolutePath(relativePath)
handlers = group.combineHandlers(handlers)
2015-05-09 04:34:43 +03:00
debugPrintRoute(httpMethod, absolutePath, handlers)
2015-03-31 22:39:06 +03:00
group.engine.handle(httpMethod, absolutePath, handlers)
2014-10-08 23:37:26 +04:00
}
// POST is a shortcut for router.Handle("POST", path, handle)
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) {
group.Handle("POST", relativePath, handlers)
}
// GET is a shortcut for router.Handle("GET", path, handle)
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) {
group.Handle("GET", relativePath, handlers)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) {
group.Handle("DELETE", relativePath, handlers)
}
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) {
group.Handle("PATCH", relativePath, handlers)
}
// PUT is a shortcut for router.Handle("PUT", path, handle)
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) {
group.Handle("PUT", relativePath, handlers)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) {
group.Handle("OPTIONS", relativePath, handlers)
}
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) {
group.Handle("HEAD", relativePath, handlers)
}
// LINK is a shortcut for router.Handle("LINK", path, handle)
func (group *RouterGroup) LINK(relativePath string, handlers ...HandlerFunc) {
group.Handle("LINK", relativePath, handlers)
}
// UNLINK is a shortcut for router.Handle("UNLINK", path, handle)
func (group *RouterGroup) UNLINK(relativePath string, handlers ...HandlerFunc) {
group.Handle("UNLINK", relativePath, handlers)
}
2015-05-20 00:00:55 +03:00
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, WS, LINK, UNLINK, TRACE
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("WS", relativePath, handlers)
group.Handle("LINK", relativePath, handlers)
group.Handle("UNLINK", relativePath, handlers)
group.Handle("TRACE", relativePath, handlers)
}
2014-10-08 23:37:26 +04: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")
func (group *RouterGroup) Static(relativePath, root string) {
handler := group.createStaticHandler(relativePath, root)
2015-04-06 22:26:16 +03:00
relativePath = path.Join(relativePath, "/*filepath")
2014-10-09 03:40:42 +04:00
// Register GET and HEAD handlers
2015-04-06 22:26:16 +03:00
group.GET(relativePath, handler)
group.HEAD(relativePath, handler)
2014-10-08 23:49:08 +04:00
}
func (group *RouterGroup) createStaticHandler(relativePath, root string) func(*Context) {
absolutePath := group.calculateAbsolutePath(relativePath)
2014-10-08 23:37:26 +04:00
fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root)))
return func(c *Context) {
fileServer.ServeHTTP(c.Writer, c.Request)
}
}
2015-05-07 12:30:01 +03:00
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
2014-10-08 23:37:26 +04:00
finalSize := len(group.Handlers) + len(handlers)
2015-05-07 12:30:01 +03:00
mergedHandlers := make(HandlersChain, finalSize)
2015-04-09 13:15:02 +03:00
copy(mergedHandlers, group.Handlers)
copy(mergedHandlers[len(group.Handlers):], handlers)
return mergedHandlers
2014-10-08 23:37:26 +04:00
}
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
return joinPaths(group.BasePath, relativePath)
2014-10-08 23:37:26 +04:00
}