作者 yangfu

feat-1.0

... ... @@ -7,6 +7,7 @@ import (
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"net/http"
... ... @@ -49,6 +50,7 @@ func main() {
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
server.AddRoutes(RoutersOpenapi(ctx))
// 数据迁移
if c.Migrate {
... ... @@ -69,3 +71,18 @@ func systemSetup(c config.Config) {
// 系统成功应答包装
httpx.SetOkHandler(xerr.OkHandlerCtx)
}
func RoutersOpenapi(svc *svc.ServiceContext) []rest.Route {
return []rest.Route{
{
Method: http.MethodGet,
Path: "/log/:module",
Handler: tool.LogHandler(svc.Config.Log),
},
{
Method: http.MethodGet,
Path: "/cache/clear/:app",
Handler: tool.ClearCacheHandler(svc.Redis),
},
}
}
... ...
... ... @@ -8,7 +8,7 @@ Timeout: 30000
LogRequest: true # 记录详细请求日志
Log:
#Mode: file
Mode: file
Encoding: plain
Level: debug # info
MaxSize: 1 # 2MB
... ...
... ... @@ -11,6 +11,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/db"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/gateway/smslib"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"net/http"
... ... @@ -78,5 +79,15 @@ func RoutersOpenapi(svc *svc.ServiceContext) []rest.Route {
Path: "/openapi/sms/send-sms-code",
Handler: smslib.SendSmsCodeHandler(svc.SmsService),
},
{
Method: http.MethodGet,
Path: "/log/:module",
Handler: tool.LogHandler(svc.Config.Log),
},
{
Method: http.MethodGet,
Path: "/cache/clear/:app",
Handler: tool.ClearCacheHandler(svc.Redis),
},
}
}
... ...
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)))
}
}
}
... ...