2014-10-08 19:37:26 +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-05-21 14:59:58 +00:00
|
|
|
"strings"
|
2014-10-08 19:37:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Used internally to configure router, a RouterGroup is associated with a prefix
|
|
|
|
// and an array of handlers (middlewares)
|
|
|
|
type RouterGroup struct {
|
2015-05-16 16:08:19 +00:00
|
|
|
Handlers HandlersChain
|
|
|
|
BasePath string
|
|
|
|
engine *Engine
|
2014-10-08 19:37:26 +00: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{
|
2015-05-16 16:08:19 +00:00
|
|
|
Handlers: group.combineHandlers(handlers),
|
|
|
|
BasePath: group.calculateAbsolutePath(relativePath),
|
|
|
|
engine: group.engine,
|
2014-10-08 19:37:26 +00: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-19 21:22:35 +00:00
|
|
|
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) {
|
2014-10-08 19:37:26 +00:00
|
|
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
|
|
|
handlers = group.combineHandlers(handlers)
|
2015-05-19 21:22:35 +00:00
|
|
|
group.engine.addRoute(httpMethod, absolutePath, handlers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) {
|
|
|
|
group.handle(httpMethod, relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST is a shortcut for router.Handle("POST", path, handle)
|
|
|
|
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 21:22:35 +00:00
|
|
|
group.handle("POST", relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET is a shortcut for router.Handle("GET", path, handle)
|
|
|
|
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 21:22:35 +00:00
|
|
|
group.handle("GET", relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE is a shortcut for router.Handle("DELETE", path, handle)
|
|
|
|
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 21:22:35 +00:00
|
|
|
group.handle("DELETE", relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH is a shortcut for router.Handle("PATCH", path, handle)
|
|
|
|
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 21:22:35 +00:00
|
|
|
group.handle("PATCH", relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT is a shortcut for router.Handle("PUT", path, handle)
|
|
|
|
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 21:22:35 +00:00
|
|
|
group.handle("PUT", relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
|
|
|
|
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 21:22:35 +00:00
|
|
|
group.handle("OPTIONS", relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HEAD is a shortcut for router.Handle("HEAD", path, handle)
|
|
|
|
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 21:22:35 +00:00
|
|
|
group.handle("HEAD", relativePath, handlers)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 21:00:55 +00:00
|
|
|
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
|
2015-05-19 22:32:06 +00:00
|
|
|
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
|
2015-05-19 21:22:35 +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-05-19 21:00:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 14:59:58 +00:00
|
|
|
func (group *RouterGroup) StaticFile(relativePath, filepath string) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:37:26 +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")
|
|
|
|
func (group *RouterGroup) Static(relativePath, root string) {
|
2015-05-21 15:01:13 +00:00
|
|
|
group.StaticFS(relativePath, http.Dir(root), false)
|
2015-05-20 08:50:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 15:01:13 +00:00
|
|
|
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem, listDirectory bool) {
|
|
|
|
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
|
|
|
|
panic("URL parameters can not be used when serving a static folder")
|
|
|
|
}
|
|
|
|
handler := group.createStaticHandler(relativePath, fs, listDirectory)
|
2015-04-06 19:26:16 +00:00
|
|
|
relativePath = path.Join(relativePath, "/*filepath")
|
2014-10-08 23:40:42 +00:00
|
|
|
|
|
|
|
// Register GET and HEAD handlers
|
2015-04-06 19:26:16 +00:00
|
|
|
group.GET(relativePath, handler)
|
|
|
|
group.HEAD(relativePath, handler)
|
2014-10-08 19:49:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 15:01:13 +00:00
|
|
|
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem, listDirectory bool) HandlerFunc {
|
2015-05-16 16:08:19 +00:00
|
|
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
2015-05-20 08:50:59 +00:00
|
|
|
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
|
2015-05-21 15:01:13 +00:00
|
|
|
return func(c *Context) {
|
|
|
|
if !listDirectory && lastChar(c.Request.URL.Path) == '/' {
|
|
|
|
http.NotFound(c.Writer, c.Request)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileServer.ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 09:30:01 +00:00
|
|
|
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
|
2014-10-08 19:37:26 +00:00
|
|
|
finalSize := len(group.Handlers) + len(handlers)
|
2015-05-07 09:30:01 +00:00
|
|
|
mergedHandlers := make(HandlersChain, finalSize)
|
2015-04-09 10:15:02 +00:00
|
|
|
copy(mergedHandlers, group.Handlers)
|
|
|
|
copy(mergedHandlers[len(group.Handlers):], handlers)
|
|
|
|
return mergedHandlers
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
|
2015-05-16 16:08:19 +00:00
|
|
|
return joinPaths(group.BasePath, relativePath)
|
2014-10-08 19:37:26 +00:00
|
|
|
}
|