-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
76 lines (68 loc) · 2.75 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package hgin
import (
"net/http"
"path"
)
type RouterGroup struct {
prefix string
middlewares []HandlerFunc // support middleware
parent *RouterGroup // support nesting
engine *Engine // all groups share an Engine instance
}
// Group is defined to create a new RouterGroup
// remember all groups share the same Engine instance
func (group *RouterGroup) Group(prefix string) *RouterGroup {
engine := group.engine
nextGroup := &RouterGroup{
prefix: group.prefix + prefix,
parent: group,
engine: engine,
}
engine.groups = append(engine.groups, nextGroup)
return nextGroup
}
// Use is defined to add middleware to the group
func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
group.middlewares = append(group.middlewares, middlewares...)
}
func (group *RouterGroup) addRoute(method string, comp string, handler HandlerFunc) {
pattern := group.prefix + comp
group.engine.router.addRoute(method, pattern, handler)
}
// GET defines the method to add GET request
func (group *RouterGroup) GET(pattern string, handler HandlerFunc) {
group.addRoute("GET", pattern, handler)
}
// POST defines the method to add POST request
func (group *RouterGroup) POST(pattern string, handler HandlerFunc) {
group.addRoute("POST", pattern, handler)
}
// Static 方法用于注册静态文件路由。
// 该方法允许通过相对路径和根目录提供静态文件服务。
// 参数 relativePath 是静态资源的访问路径,root 是静态文件的根目录。
// 通过这个方法,可以方便地为特定路径下的所有静态文件提供服务。
func (group *RouterGroup) Static(relativePath string, root string) {
handler := group.createStaticHandler(relativePath, http.Dir(root))
urlPattern := path.Join(relativePath, "/*filepath")
group.GET(urlPattern, handler)
}
// createStaticHandler 创建一个静态文件处理的路由规则
// 该函数属于RouterGroup结构体的方法,用于在路由组中添加静态文件处理功能。
// relativePath: 相对于路由组前缀的路径;
// fs: http.FileSystem接口,用于访问文件系统;
// HandlerFunc: 一个处理HTTP请求的函数。
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
absolutePath := path.Join(group.prefix, relativePath)
// 假设 absolutePath 是 /static,那么如果客户端请求 /static/js/app.js,
// http.StripPrefix("/static", ...) 会去掉 /static 部分,只剩下 /js/app.js,
// 然后将其传递给 http.FileServer(fs) 进行处理。
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
return func(c *Context) {
file := c.Params["filepath"]
if _, err := fs.Open(file); err != nil {
c.Status(http.StatusNotFound)
return
}
fileServer.ServeHTTP(c.Writer, c.Req)
}
}