handlers.go
1.6 KB
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
package tool
import (
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
"path/filepath"
"strings"
)
func LogHandler(conf logx.LogConf) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Module string `path:"module"`
}
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
path := conf.Path
if conf.Mode != "file" {
return
}
if path == "" {
path = "logs"
}
if !strings.HasSuffix(req.Module, ".log") {
req.Module += ".log"
}
handler := http.FileServer(http.Dir(path))
r.URL.Path = filepath.Join(req.Module)
handler.ServeHTTP(w, r)
}
}
func ClearCacheHandler(redis *redis.Redis) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
success int
appName string
)
var req struct {
App string `path:"app"`
}
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
appName = req.App
if strings.TrimSpace(appName) == "" {
httpx.Ok(w)
return
}
keyPattern := fmt.Sprintf("%s*", appName)
list, err := redis.Keys(keyPattern)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
}
for _, key := range list {
if _, err = redis.Del(key); err == nil {
success++
}
}
logx.Infof("清理缓存:%d/%d", success, len(list))
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJson(w, fmt.Sprintf("应用:%v 清理缓存:%d/%d", appName, success, len(list)))
}
}
}