2022-05-28 05:42:28 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2014-10-08 23:37:26 +04:00
|
|
|
// 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-06-04 05:50:40 +03:00
|
|
|
"regexp"
|
2015-05-21 17:59:58 +03:00
|
|
|
"strings"
|
2014-10-08 23:37:26 +04:00
|
|
|
)
|
|
|
|
|
2021-04-21 03:24:55 +03:00
|
|
|
var (
|
2022-02-14 09:39:57 +03:00
|
|
|
// regEnLetter matches english letters for http method name
|
2021-04-21 03:24:55 +03:00
|
|
|
regEnLetter = regexp.MustCompile("^[A-Z]+$")
|
2021-10-24 04:31:13 +03:00
|
|
|
|
|
|
|
// anyMethods for RouterGroup Any method
|
|
|
|
anyMethods = []string{
|
|
|
|
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
|
|
|
|
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,
|
|
|
|
http.MethodTrace,
|
|
|
|
}
|
2021-04-21 03:24:55 +03:00
|
|
|
)
|
|
|
|
|
2018-09-15 10:21:54 +03:00
|
|
|
// IRouter defines all router handle interface includes single and group router.
|
2017-07-05 10:47:36 +03:00
|
|
|
type IRouter interface {
|
|
|
|
IRoutes
|
|
|
|
Group(string, ...HandlerFunc) *RouterGroup
|
|
|
|
}
|
|
|
|
|
2018-09-20 06:53:58 +03:00
|
|
|
// IRoutes defines all router handle interface.
|
2017-07-05 10:47:36 +03:00
|
|
|
type 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
|
2022-03-18 04:52:23 +03:00
|
|
|
StaticFileFS(string, string, http.FileSystem) IRoutes
|
2017-07-05 10:47:36 +03:00
|
|
|
Static(string, string) IRoutes
|
|
|
|
StaticFS(string, http.FileSystem) IRoutes
|
|
|
|
}
|
|
|
|
|
2018-09-15 10:21:54 +03:00
|
|
|
// RouterGroup is used internally to configure router, a RouterGroup is associated with
|
|
|
|
// a prefix and an array of handlers (middleware).
|
2017-07-05 10:47:36 +03:00
|
|
|
type RouterGroup struct {
|
|
|
|
Handlers HandlersChain
|
|
|
|
basePath string
|
|
|
|
engine *Engine
|
|
|
|
root bool
|
|
|
|
}
|
2014-10-08 23:37:26 +04:00
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
var _ IRouter = &RouterGroup{}
|
2015-06-26 17:01:35 +03:00
|
|
|
|
2018-12-31 04:02:53 +03:00
|
|
|
// Use adds middleware to the group, see example code in GitHub.
|
2015-08-16 19:38:13 +03:00
|
|
|
func (group *RouterGroup) Use(middleware ...HandlerFunc) IRoutes {
|
2015-07-03 21:12:01 +03:00
|
|
|
group.Handlers = append(group.Handlers, middleware...)
|
2015-06-11 02:02:38 +03:00
|
|
|
return group.returnObj()
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2018-11-05 09:17:04 +03:00
|
|
|
// Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.
|
|
|
|
// For example, all the routes that use a common middleware for authorization could be grouped.
|
2014-10-08 23:37:26 +04:00
|
|
|
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
|
|
|
|
return &RouterGroup{
|
2015-05-16 19:08:19 +03:00
|
|
|
Handlers: group.combineHandlers(handlers),
|
2015-07-08 05:27:23 +03:00
|
|
|
basePath: group.calculateAbsolutePath(relativePath),
|
2015-05-16 19:08:19 +03:00
|
|
|
engine: group.engine,
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 10:21:54 +03:00
|
|
|
// BasePath returns the base path of router group.
|
|
|
|
// For example, if v := router.Group("/rest/n/v1/api"), v.BasePath() is "/rest/n/v1/api".
|
2015-07-08 05:27:23 +03:00
|
|
|
func (group *RouterGroup) BasePath() string {
|
|
|
|
return group.basePath
|
|
|
|
}
|
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:12:01 +03:00
|
|
|
// 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.
|
2018-12-31 04:02:53 +03:00
|
|
|
// See the example code in GitHub.
|
2014-10-08 23:37:26 +04:00
|
|
|
//
|
|
|
|
// 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-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {
|
2021-04-21 03:24:55 +03:00
|
|
|
if matched := regEnLetter.MatchString(httpMethod); !matched {
|
2015-06-04 05:50:40 +03:00
|
|
|
panic("http method " + httpMethod + " is not valid")
|
|
|
|
}
|
2015-06-11 02:02:38 +03:00
|
|
|
return group.handle(httpMethod, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:34:33 +03:00
|
|
|
// POST is a shortcut for router.Handle("POST", path, handlers).
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2019-11-29 02:50:49 +03:00
|
|
|
return group.handle(http.MethodPost, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:34:33 +03:00
|
|
|
// GET is a shortcut for router.Handle("GET", path, handlers).
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2019-11-29 02:50:49 +03:00
|
|
|
return group.handle(http.MethodGet, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:34:33 +03:00
|
|
|
// DELETE is a shortcut for router.Handle("DELETE", path, handlers).
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2019-11-29 02:50:49 +03:00
|
|
|
return group.handle(http.MethodDelete, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:34:33 +03:00
|
|
|
// PATCH is a shortcut for router.Handle("PATCH", path, handlers).
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2019-11-29 02:50:49 +03:00
|
|
|
return group.handle(http.MethodPatch, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:34:33 +03:00
|
|
|
// PUT is a shortcut for router.Handle("PUT", path, handlers).
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2019-11-29 02:50:49 +03:00
|
|
|
return group.handle(http.MethodPut, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:34:33 +03:00
|
|
|
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handlers).
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2019-11-29 02:50:49 +03:00
|
|
|
return group.handle(http.MethodOptions, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:34:33 +03:00
|
|
|
// HEAD is a shortcut for router.Handle("HEAD", path, handlers).
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2019-11-29 02:50:49 +03:00
|
|
|
return group.handle(http.MethodHead, relativePath, handlers)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
// Any registers a route that matches all the HTTP methods.
|
2017-08-16 06:55:50 +03:00
|
|
|
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {
|
2021-10-24 04:31:13 +03:00
|
|
|
for _, method := range anyMethods {
|
|
|
|
group.handle(method, relativePath, handlers)
|
|
|
|
}
|
|
|
|
|
2015-06-11 02:02:38 +03:00
|
|
|
return group.returnObj()
|
2015-05-20 00:00:55 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 06:41:45 +03:00
|
|
|
// StaticFile registers a single route in order to serve a single file of the local filesystem.
|
2015-07-02 21:24:54 +03:00
|
|
|
// router.StaticFile("favicon.ico", "./resources/favicon.ico")
|
|
|
|
func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes {
|
2022-03-18 04:52:23 +03:00
|
|
|
return group.staticFileHandler(relativePath, func(c *Context) {
|
|
|
|
c.File(filepath)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaticFileFS works just like `StaticFile` but a custom `http.FileSystem` can be used instead..
|
|
|
|
// router.StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})
|
2022-09-18 16:59:57 +03:00
|
|
|
// Gin by default uses: gin.Dir()
|
2022-03-18 04:52:23 +03:00
|
|
|
func (group *RouterGroup) StaticFileFS(relativePath, filepath string, fs http.FileSystem) IRoutes {
|
|
|
|
return group.staticFileHandler(relativePath, func(c *Context) {
|
|
|
|
c.FileFromFS(filepath, fs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (group *RouterGroup) staticFileHandler(relativePath string, handler HandlerFunc) IRoutes {
|
2015-05-21 17:59:58 +03:00
|
|
|
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
|
|
|
|
panic("URL parameters can not be used when serving a static file")
|
|
|
|
}
|
|
|
|
group.GET(relativePath, handler)
|
|
|
|
group.HEAD(relativePath, handler)
|
2015-06-11 02:02:38 +03:00
|
|
|
return group.returnObj()
|
2015-05-21 17:59:58 +03:00
|
|
|
}
|
|
|
|
|
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 :
|
2022-08-15 16:38:20 +03:00
|
|
|
//
|
|
|
|
// router.Static("/static", "/var/www")
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) Static(relativePath, root string) IRoutes {
|
2015-06-10 18:10:34 +03:00
|
|
|
return group.StaticFS(relativePath, Dir(root, false))
|
2015-05-20 11:50:59 +03:00
|
|
|
}
|
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.
|
2022-09-18 16:59:57 +03:00
|
|
|
// Gin by default uses: gin.Dir()
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes {
|
2015-05-21 18:01:13 +03:00
|
|
|
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
|
|
|
|
panic("URL parameters can not be used when serving a static folder")
|
|
|
|
}
|
2015-05-24 17:29:55 +03:00
|
|
|
handler := group.createStaticHandler(relativePath, fs)
|
2015-05-31 16:55:10 +03:00
|
|
|
urlPattern := path.Join(relativePath, "/*filepath")
|
2014-10-09 03:40:42 +04:00
|
|
|
|
|
|
|
// Register GET and HEAD handlers
|
2015-05-31 16:55:10 +03:00
|
|
|
group.GET(urlPattern, handler)
|
|
|
|
group.HEAD(urlPattern, handler)
|
2015-06-11 02:02:38 +03:00
|
|
|
return group.returnObj()
|
2014-10-08 23:49:08 +04:00
|
|
|
}
|
|
|
|
|
2015-05-24 17:29:55 +03:00
|
|
|
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
|
2015-05-16 19:08:19 +03:00
|
|
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
2015-05-20 11:50:59 +03:00
|
|
|
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
|
2018-11-26 18:01:51 +03:00
|
|
|
|
2015-05-24 18:03:44 +03:00
|
|
|
return func(c *Context) {
|
2020-05-25 15:13:09 +03:00
|
|
|
if _, noListing := fs.(*onlyFilesFS); noListing {
|
2018-12-05 08:36:49 +03:00
|
|
|
c.Writer.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
2018-11-26 18:01:51 +03:00
|
|
|
|
2018-12-05 08:36:49 +03:00
|
|
|
file := c.Param("filepath")
|
2018-11-26 18:01:51 +03:00
|
|
|
// Check if file exists and/or if we have permission to access it
|
2019-10-31 17:52:02 +03:00
|
|
|
f, err := fs.Open(file)
|
|
|
|
if err != nil {
|
2018-06-26 12:21:32 +03:00
|
|
|
c.Writer.WriteHeader(http.StatusNotFound)
|
2019-03-11 05:52:47 +03:00
|
|
|
c.handlers = group.engine.noRoute
|
2018-11-26 18:01:51 +03:00
|
|
|
// Reset index
|
|
|
|
c.index = -1
|
|
|
|
return
|
2015-05-24 18:03:44 +03:00
|
|
|
}
|
2019-10-31 17:52:02 +03:00
|
|
|
f.Close()
|
2018-11-26 18:01:51 +03:00
|
|
|
|
2015-05-24 18:03:44 +03:00
|
|
|
fileServer.ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
|
|
|
|
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)
|
2021-07-11 09:38:45 +03:00
|
|
|
assert1(finalSize < int(abortIndex), "too many 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 {
|
2015-07-08 05:27:23 +03:00
|
|
|
return joinPaths(group.basePath, relativePath)
|
2014-10-08 23:37:26 +04:00
|
|
|
}
|
2015-06-11 02:02:38 +03:00
|
|
|
|
2015-07-02 21:24:54 +03:00
|
|
|
func (group *RouterGroup) returnObj() IRoutes {
|
2015-06-11 02:02:38 +03:00
|
|
|
if group.root {
|
|
|
|
return group.engine
|
|
|
|
}
|
2015-07-02 21:24:54 +03:00
|
|
|
return group
|
2015-06-11 02:02:38 +03:00
|
|
|
}
|