-
Notifications
You must be signed in to change notification settings - Fork 2
/
myhandler.go
41 lines (35 loc) · 1.04 KB
/
myhandler.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
package handler
import (
"io/fs"
"net/http"
"strings"
"github.com/zeromicro/go-zero/rest"
"github.com/jaronnie/jzero/daemon/internal/svc"
"github.com/jaronnie/jzero/embeded"
)
func HealthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("success"))
}
}
func registerStaticEmbedHandler(server *rest.Server, serverCtx *svc.ServiceContext) {
// related: https://blog.csdn.net/keytounix/article/details/108424389
dirLevel := []string{":1", ":2", ":3", ":4", ":5", ":6", ":7", ":8"}
pattern := "/"
staticFS, _ := embeded.RootWeb()
for i := 1; i < len(dirLevel); i++ {
path := "/" + strings.Join(dirLevel[:i], "/")
server.AddRoute(
rest.Route{
Method: http.MethodGet,
Path: path,
Handler: dirHandler(pattern, staticFS),
})
}
}
func dirHandler(pattern string, fs fs.FS) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
handler := http.StripPrefix(pattern, http.FileServer(http.FS(fs)))
handler.ServeHTTP(w, req)
}
}