作者 yangfu

feat-1.0

@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 "github.com/zeromicro/go-zero/rest/httpx" 7 "github.com/zeromicro/go-zero/rest/httpx"
8 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db" 8 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db"
9 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
10 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" 11 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
11 "net/http" 12 "net/http"
12 13
@@ -49,6 +50,7 @@ func main() { @@ -49,6 +50,7 @@ func main() {
49 50
50 ctx := svc.NewServiceContext(c) 51 ctx := svc.NewServiceContext(c)
51 handler.RegisterHandlers(server, ctx) 52 handler.RegisterHandlers(server, ctx)
  53 + server.AddRoutes(RoutersOpenapi(ctx))
52 54
53 // 数据迁移 55 // 数据迁移
54 if c.Migrate { 56 if c.Migrate {
@@ -69,3 +71,18 @@ func systemSetup(c config.Config) { @@ -69,3 +71,18 @@ func systemSetup(c config.Config) {
69 // 系统成功应答包装 71 // 系统成功应答包装
70 httpx.SetOkHandler(xerr.OkHandlerCtx) 72 httpx.SetOkHandler(xerr.OkHandlerCtx)
71 } 73 }
  74 +
  75 +func RoutersOpenapi(svc *svc.ServiceContext) []rest.Route {
  76 + return []rest.Route{
  77 + {
  78 + Method: http.MethodGet,
  79 + Path: "/log/:module",
  80 + Handler: tool.LogHandler(svc.Config.Log),
  81 + },
  82 + {
  83 + Method: http.MethodGet,
  84 + Path: "/cache/clear/:app",
  85 + Handler: tool.ClearCacheHandler(svc.Redis),
  86 + },
  87 + }
  88 +}
@@ -8,7 +8,7 @@ Timeout: 30000 @@ -8,7 +8,7 @@ Timeout: 30000
8 LogRequest: true # 记录详细请求日志 8 LogRequest: true # 记录详细请求日志
9 9
10 Log: 10 Log:
11 - #Mode: file 11 + Mode: file
12 Encoding: plain 12 Encoding: plain
13 Level: debug # info 13 Level: debug # info
14 MaxSize: 1 # 2MB 14 MaxSize: 1 # 2MB
@@ -11,6 +11,7 @@ import ( @@ -11,6 +11,7 @@ import (
11 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/db" 11 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/db"
12 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain" 12 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain"
13 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/gateway/smslib" 13 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/gateway/smslib"
  14 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
14 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" 15 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
15 "net/http" 16 "net/http"
16 17
@@ -78,5 +79,15 @@ func RoutersOpenapi(svc *svc.ServiceContext) []rest.Route { @@ -78,5 +79,15 @@ func RoutersOpenapi(svc *svc.ServiceContext) []rest.Route {
78 Path: "/openapi/sms/send-sms-code", 79 Path: "/openapi/sms/send-sms-code",
79 Handler: smslib.SendSmsCodeHandler(svc.SmsService), 80 Handler: smslib.SendSmsCodeHandler(svc.SmsService),
80 }, 81 },
  82 + {
  83 + Method: http.MethodGet,
  84 + Path: "/log/:module",
  85 + Handler: tool.LogHandler(svc.Config.Log),
  86 + },
  87 + {
  88 + Method: http.MethodGet,
  89 + Path: "/cache/clear/:app",
  90 + Handler: tool.ClearCacheHandler(svc.Redis),
  91 + },
81 } 92 }
82 } 93 }
  1 +package tool
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/zeromicro/go-zero/core/logx"
  6 + "github.com/zeromicro/go-zero/core/stores/redis"
  7 + "github.com/zeromicro/go-zero/rest/httpx"
  8 + "net/http"
  9 + "path/filepath"
  10 + "strings"
  11 +)
  12 +
  13 +func LogHandler(conf logx.LogConf) http.HandlerFunc {
  14 + return func(w http.ResponseWriter, r *http.Request) {
  15 + var req struct {
  16 + Module string `path:"module"`
  17 + }
  18 + if err := httpx.Parse(r, &req); err != nil {
  19 + httpx.ErrorCtx(r.Context(), w, err)
  20 + return
  21 + }
  22 + path := conf.Path
  23 + if conf.Mode != "file" {
  24 + return
  25 + }
  26 + if path == "" {
  27 + path = "logs"
  28 + }
  29 + if !strings.HasSuffix(req.Module, ".log") {
  30 + req.Module += ".log"
  31 + }
  32 + handler := http.FileServer(http.Dir(path))
  33 + r.URL.Path = filepath.Join(req.Module)
  34 + handler.ServeHTTP(w, r)
  35 + }
  36 +}
  37 +
  38 +func ClearCacheHandler(redis *redis.Redis) http.HandlerFunc {
  39 + return func(w http.ResponseWriter, r *http.Request) {
  40 + var (
  41 + success int
  42 + appName string
  43 + )
  44 + var req struct {
  45 + App string `path:"app"`
  46 + }
  47 + if err := httpx.Parse(r, &req); err != nil {
  48 + httpx.ErrorCtx(r.Context(), w, err)
  49 + return
  50 + }
  51 + appName = req.App
  52 + if strings.TrimSpace(appName) == "" {
  53 + httpx.Ok(w)
  54 + return
  55 + }
  56 + keyPattern := fmt.Sprintf("%s*", appName)
  57 + list, err := redis.Keys(keyPattern)
  58 + if err != nil {
  59 + httpx.ErrorCtx(r.Context(), w, err)
  60 + }
  61 + for _, key := range list {
  62 + if _, err = redis.Del(key); err == nil {
  63 + success++
  64 + }
  65 + }
  66 + logx.Infof("清理缓存:%d/%d", success, len(list))
  67 +
  68 + if err != nil {
  69 + httpx.ErrorCtx(r.Context(), w, err)
  70 + } else {
  71 + httpx.OkJson(w, fmt.Sprintf("应用:%v 清理缓存:%d/%d", appName, success, len(list)))
  72 + }
  73 + }
  74 +}