代码拉取完成,页面将自动刷新
package yasrpc
import (
"net/http"
"path"
"strings"
)
// IRouter defines all router handle interface includes single and group router.
type IRouter interface {
IRoutes
Group(string, ...HandlerFunc) *RouterGroup
}
// IRoutes defines all router handle interface.
type IRoutes interface {
Use(...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
Match([]string, string, ...HandlerFunc) IRoutes
StaticFile(string, string) IRoutes
Static(string, string) IRoutes
StaticFS(string, http.FileSystem) IRoutes
}
// RouterGroup is used internally to configure router, a RouterGroup is associated with
// a prefix and an array of handlers (middleware).
type RouterGroup struct {
Middlewares HandlersChain
basePath string
engine *Engine
root bool
}
var _ IRouter = (*RouterGroup)(nil)
// Use adds middleware to the group.
func (g *RouterGroup) Use(middleware ...HandlerFunc) IRoutes {
g.Middlewares = append(g.Middlewares, middleware...)
return g.returnObj()
}
// 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.
func (g *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
return &RouterGroup{
Middlewares: g.combineHandlers(handlers),
basePath: g.calculateAbsolutePath(relativePath),
engine: g.engine,
}
}
// BasePath returns the base path of router group.
func (g *RouterGroup) BasePath() string {
return g.basePath
}
func (g *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) IRoutes {
absolutePath := g.calculateAbsolutePath(relativePath)
if absolutePath == DefaultRPCPath || absolutePath == DefaultDebugPath {
return nil
}
handlers = g.combineHandlers(handlers)
g.engine.addRoute(httpMethod, absolutePath, handlers)
return g.returnObj()
}
// GET is a shortcut for router.Handle("GET", path, handlers).
func (g *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
return g.handle(http.MethodGet, relativePath, handlers)
}
// POST is a shortcut for router.Handle("POST", path, handlers).
func (g *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
return g.handle(http.MethodPost, relativePath, handlers)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handlers).
func (g *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
return g.handle(http.MethodDelete, relativePath, handlers)
}
// PATCH is a shortcut for router.Handle("PATCH", path, handlers).
func (g *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
return g.handle(http.MethodPatch, relativePath, handlers)
}
// PUT is a shortcut for router.Handle("PUT", path, handlers).
func (g *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
return g.handle(http.MethodPut, relativePath, handlers)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handlers).
func (g *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
return g.handle(http.MethodOptions, relativePath, handlers)
}
// HEAD is a shortcut for router.Handle("HEAD", path, handlers).
func (g *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
return g.handle(http.MethodHead, relativePath, handlers)
}
// Match registers a route that matches the specified methods that you declared.
func (g *RouterGroup) Match(methods []string, relativePath string, handlers ...HandlerFunc) IRoutes {
for _, method := range methods {
g.handle(method, relativePath, handlers)
}
return g.returnObj()
}
// StaticFile registers a single route in order to serve a single file of the local filesystem.
func (g *RouterGroup) StaticFile(relativePath, filepath string) IRoutes {
return g.staticFileHandler(relativePath, func(c *Context) {
c.File(filepath)
})
}
func (g *RouterGroup) staticFileHandler(relativePath string, handler HandlerFunc) IRoutes {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static file")
}
g.GET(relativePath, handler)
g.HEAD(relativePath, handler)
return g.returnObj()
}
// 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.
func (group *RouterGroup) Static(relativePath, root string) IRoutes {
return group.StaticFS(relativePath, Dir(root, false))
}
// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.
// Gin by default uses: 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")
}
handler := group.createStaticHandler(relativePath, fs)
urlPattern := path.Join(relativePath, "/*filepath")
// Register GET and HEAD handlers
group.GET(urlPattern, handler)
group.HEAD(urlPattern, handler)
return group.returnObj()
}
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
absolutePath := group.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
return func(c *Context) {
if _, noListing := fs.(*onlyFilesFS); noListing {
c.Writer.WriteHeader(http.StatusNotFound)
}
file := c.Param("filepath")
// Check if file exists and/or if we have permission to access it
f, err := fs.Open(file)
if err != nil {
c.Writer.WriteHeader(http.StatusNotFound)
// c.handlers = group.engine.noRoute
// Reset index
c.index = -1
return
}
f.Close()
fileServer.ServeHTTP(c.Writer, c.Request)
}
}
func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
finalSize := len(group.Middlewares) + len(handlers)
mustGuard(finalSize < int(abortIndex), "too many handlers")
mergedHandlers := make(HandlersChain, finalSize)
copy(mergedHandlers, group.Middlewares)
copy(mergedHandlers[len(group.Middlewares):], handlers)
return mergedHandlers
}
func (g *RouterGroup) calculateAbsolutePath(relativePath string) string {
return joinPaths(g.basePath, relativePath)
}
func (g *RouterGroup) returnObj() IRoutes {
if g.root {
return g.engine
}
return g
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。