diff --git a/app/controller/jaeger_conn/jaeger_conn.go b/app/controller/jaeger_conn/jaeger_conn.go index 5dea1590..88e81fb2 100644 --- a/app/controller/jaeger_conn/jaeger_conn.go +++ b/app/controller/jaeger_conn/jaeger_conn.go @@ -7,8 +7,9 @@ import ( "go-gin-api/app/model/proto/read" "go-gin-api/app/model/proto/speak" "go-gin-api/app/model/proto/write" - "go-gin-api/app/util" "go-gin-api/app/util/grpc_client" + "go-gin-api/app/util/request" + "go-gin-api/app/util/response" ) func JaegerTest(c *gin.Context) { @@ -37,7 +38,7 @@ func JaegerTest(c *gin.Context) { // 调用 HTTP 服务 resHttpGet := "" - _, err := util.HttpGet("http://localhost:9905/sing", c) + _, err := request.HttpGet("http://localhost:9905/sing", c) if err == nil { resHttpGet = "[HttpGetOk]" } @@ -51,6 +52,6 @@ func JaegerTest(c *gin.Context) { resHttpGet - utilGin := util.Gin{Ctx:c} + utilGin := response.Gin{Ctx:c} utilGin.Response(1, msg, nil) } diff --git a/app/controller/product/product.go b/app/controller/product/product.go index 035c2e5c..029112d3 100644 --- a/app/controller/product/product.go +++ b/app/controller/product/product.go @@ -5,16 +5,17 @@ import ( "github.com/gin-gonic/gin" "go-gin-api/app/controller/param_bind" "go-gin-api/app/controller/param_verify" - "go-gin-api/app/util" + "go-gin-api/app/util/bind" + "go-gin-api/app/util/response" "gopkg.in/go-playground/validator.v9" ) // 新增 func Add(c *gin.Context) { - utilGin := util.Gin{Ctx:c} + utilGin := response.Gin{Ctx: c} // 参数绑定 - s, e := util.Bind(¶m_bind.ProductAdd{}, c) + s, e := bind.Bind(¶m_bind.ProductAdd{}, c) if e != nil { utilGin.Response(-1, e.Error(), nil) return diff --git a/app/controller/test/test.go b/app/controller/test/test.go index 129f3320..d38b2277 100644 --- a/app/controller/test/test.go +++ b/app/controller/test/test.go @@ -3,7 +3,10 @@ package test import ( "fmt" "github.com/gin-gonic/gin" - "go-gin-api/app/util" + "go-gin-api/app/util/aes" + "go-gin-api/app/util/md5" + "go-gin-api/app/util/response" + "go-gin-api/app/util/rsa" "time" ) @@ -14,12 +17,12 @@ func Md5Test(c *gin.Context) { count := 1000000 for i := 0; i < count; i++ { // 生成签名 - util.MD5(appSecret + encryptStr + appSecret) + md5.MD5(appSecret + encryptStr + appSecret) // 验证签名 - util.MD5(appSecret + encryptStr + appSecret) + md5.MD5(appSecret + encryptStr + appSecret) } - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} utilGin.Response(1, fmt.Sprintf("%v次 - %v", count, time.Since(startTime)), nil) } @@ -30,12 +33,12 @@ func AesTest(c *gin.Context) { count := 1000000 for i := 0; i < count; i++ { // 生成签名 - sn, _ := util.AesEncrypt(encryptStr, []byte(appSecret), appSecret) + sn, _ := aes.AesEncrypt(encryptStr, []byte(appSecret), appSecret) // 验证签名 - util.AesDecrypt(sn, []byte(appSecret), appSecret) + aes.AesDecrypt(sn, []byte(appSecret), appSecret) } - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} utilGin.Response(1, fmt.Sprintf("%v次 - %v", count, time.Since(startTime)), nil) } @@ -45,11 +48,11 @@ func RsaTest(c *gin.Context) { count := 500 for i := 0; i < count; i++ { // 生成签名 - sn, _ := util.RsaPublicEncrypt(encryptStr, "rsa/public.pem") + sn, _ := rsa.RsaPublicEncrypt(encryptStr, "rsa/public.pem") // 验证签名 - util.RsaPrivateDecrypt(sn, "rsa/private.pem") + rsa.RsaPrivateDecrypt(sn, "rsa/private.pem") } - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} utilGin.Response(1, fmt.Sprintf("%v次 - %v", count, time.Since(startTime)), nil) } diff --git a/app/route/middleware/exception/exception.go b/app/route/middleware/exception/exception.go index ba54c462..8171f158 100644 --- a/app/route/middleware/exception/exception.go +++ b/app/route/middleware/exception/exception.go @@ -4,7 +4,9 @@ import ( "fmt" "github.com/gin-gonic/gin" "go-gin-api/app/config" - "go-gin-api/app/util" + "go-gin-api/app/util/mail" + "go-gin-api/app/util/response" + "go-gin-api/app/util/time" "runtime/debug" "strings" ) @@ -23,15 +25,15 @@ func SetUp() gin.HandlerFunc { subject := fmt.Sprintf("【重要错误】%s 项目出错了!", config.AppName) body := strings.ReplaceAll(MailTemplate, "{ErrorMsg}", fmt.Sprintf("%s", err)) - body = strings.ReplaceAll(body, "{RequestTime}", util.GetCurrentDate()) + body = strings.ReplaceAll(body, "{RequestTime}", time.GetCurrentDate()) body = strings.ReplaceAll(body, "{RequestURL}", c.Request.Method + " " + c.Request.Host + c.Request.RequestURI) body = strings.ReplaceAll(body, "{RequestUA}", c.Request.UserAgent()) body = strings.ReplaceAll(body, "{RequestIP}", c.ClientIP()) body = strings.ReplaceAll(body, "{DebugStack}", DebugStack) - _ = util.SendMail(config.ErrorNotifyUser, subject, body) + _ = mail.SendMail(config.ErrorNotifyUser, subject, body) - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} utilGin.Response(500, "系统异常,请联系管理员!", nil) } }() diff --git a/app/route/middleware/limiter/limiter.go b/app/route/middleware/limiter/limiter.go index 8ed052cb..b5275b75 100644 --- a/app/route/middleware/limiter/limiter.go +++ b/app/route/middleware/limiter/limiter.go @@ -3,7 +3,7 @@ package limiter import ( "fmt" "github.com/gin-gonic/gin" - "go-gin-api/app/util" + "go-gin-api/app/util/response" "golang.org/x/time/rate" "time" ) @@ -17,7 +17,7 @@ func SetUp (maxBurstSize int) gin.HandlerFunc { return } fmt.Println("Too many requests") - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} utilGin.Response(-1, "Too many requests", nil) c.Abort() return diff --git a/app/route/middleware/logger/logger.go b/app/route/middleware/logger/logger.go index 835268cf..18ada5c1 100644 --- a/app/route/middleware/logger/logger.go +++ b/app/route/middleware/logger/logger.go @@ -6,7 +6,9 @@ import ( "fmt" "github.com/gin-gonic/gin" "go-gin-api/app/config" - "go-gin-api/app/util" + jsonUtil "go-gin-api/app/util/json" + "go-gin-api/app/util/response" + "go-gin-api/app/util/time" "log" "os" ) @@ -37,7 +39,7 @@ func SetUp() gin.HandlerFunc { c.Writer = bodyLogWriter // 开始时间 - startTime := util.GetCurrentMilliUnix() + startTime := time.GetCurrentMilliUnix() // 处理请求 c.Next() @@ -49,17 +51,17 @@ func SetUp() gin.HandlerFunc { var responseData interface{} if responseBody != "" { - response := util.Response{} - err := json.Unmarshal([]byte(responseBody), &response) + res := response.Response{} + err := json.Unmarshal([]byte(responseBody), &res) if err == nil { - responseCode = response.Code - responseMsg = response.Message - responseData = response.Data + responseCode = res.Code + responseMsg = res.Message + responseData = res.Data } } // 结束时间 - endTime := util.GetCurrentMilliUnix() + endTime := time.GetCurrentMilliUnix() if c.Request.Method == "POST" { _ = c.Request.ParseForm() @@ -84,7 +86,7 @@ func SetUp() gin.HandlerFunc { accessLogMap["cost_time"] = fmt.Sprintf("%vms", endTime-startTime) - accessLogJson, _ := util.JsonEncode(accessLogMap) + accessLogJson, _ := jsonUtil.JsonEncode(accessLogMap) accessChannel <- accessLogJson } diff --git a/app/route/middleware/sign/aes/aes.go b/app/route/middleware/sign/aes/aes.go index d392d964..65b5eeae 100644 --- a/app/route/middleware/sign/aes/aes.go +++ b/app/route/middleware/sign/aes/aes.go @@ -5,7 +5,9 @@ import ( "fmt" "github.com/gin-gonic/gin" "go-gin-api/app/config" - "go-gin-api/app/util" + "go-gin-api/app/util/aes" + "go-gin-api/app/util/response" + timeUtil "go-gin-api/app/util/time" "net/url" "sort" "strconv" @@ -19,7 +21,7 @@ var AppSecret string func SetUp() gin.HandlerFunc { return func(c *gin.Context) { - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} sign, err := verifySign(c) @@ -57,7 +59,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { } if debug == "1" { - currentUnix := util.GetCurrentUnix() + currentUnix := timeUtil.GetCurrentUnix() req.Set("ts", strconv.FormatInt(currentUnix, 10)) sn, err := createSign(req) @@ -85,7 +87,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { return nil, errors.New("sn Error") } - decryptStr, decryptErr := util.AesDecrypt(sn, []byte(AppSecret), AppSecret) + decryptStr, decryptErr := aes.AesDecrypt(sn, []byte(AppSecret), AppSecret) if decryptErr != nil { return nil, errors.New(decryptErr.Error()) } @@ -97,7 +99,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { // 创建签名 func createSign(params url.Values) (string, error) { - return util.AesEncrypt(createEncryptStr(params), []byte(AppSecret), AppSecret) + return aes.AesEncrypt(createEncryptStr(params), []byte(AppSecret), AppSecret) } func createEncryptStr(params url.Values) string { diff --git a/app/route/middleware/sign/md5/md5.go b/app/route/middleware/sign/md5/md5.go index bab9ab03..74ccbc51 100644 --- a/app/route/middleware/sign/md5/md5.go +++ b/app/route/middleware/sign/md5/md5.go @@ -5,7 +5,9 @@ import ( "fmt" "github.com/gin-gonic/gin" "go-gin-api/app/config" - "go-gin-api/app/util" + "go-gin-api/app/util/md5" + "go-gin-api/app/util/response" + timeUtil "go-gin-api/app/util/time" "net/url" "sort" "strconv" @@ -19,7 +21,7 @@ var AppSecret string func SetUp() gin.HandlerFunc { return func(c *gin.Context) { - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} sign, err := verifySign(c) @@ -57,7 +59,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { } if debug == "1" { - currentUnix := util.GetCurrentUnix() + currentUnix := timeUtil.GetCurrentUnix() req.Set("ts", strconv.FormatInt(currentUnix, 10)) res := map[string]string{ "ts": strconv.FormatInt(currentUnix, 10), @@ -85,7 +87,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { // 创建签名 func createSign(params url.Values) string { // 自定义 MD5 组合 - return util.MD5(AppSecret + createEncryptStr(params) + AppSecret) + return md5.MD5(AppSecret + createEncryptStr(params) + AppSecret) } func createEncryptStr(params url.Values) string { diff --git a/app/route/middleware/sign/rsa/rsa.go b/app/route/middleware/sign/rsa/rsa.go index 841db166..ad5147be 100644 --- a/app/route/middleware/sign/rsa/rsa.go +++ b/app/route/middleware/sign/rsa/rsa.go @@ -5,7 +5,9 @@ import ( "fmt" "github.com/gin-gonic/gin" "go-gin-api/app/config" - "go-gin-api/app/util" + "go-gin-api/app/util/response" + "go-gin-api/app/util/rsa" + timeUtil "go-gin-api/app/util/time" "net/url" "sort" "strconv" @@ -19,7 +21,7 @@ var AppSecret string func SetUp() gin.HandlerFunc { return func(c *gin.Context) { - utilGin := util.Gin{Ctx: c} + utilGin := response.Gin{Ctx: c} sign, err := verifySign(c) @@ -57,7 +59,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { } if debug == "1" { - currentUnix := util.GetCurrentUnix() + currentUnix := timeUtil.GetCurrentUnix() req.Set("ts", strconv.FormatInt(currentUnix, 10)) sn, err := createSign(req) @@ -85,7 +87,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { return nil, errors.New("sn Error") } - decryptStr, decryptErr := util.RsaPrivateDecrypt(sn, config.AppRsaPrivateFile) + decryptStr, decryptErr := rsa.RsaPrivateDecrypt(sn, config.AppRsaPrivateFile) if decryptErr != nil { return nil, errors.New(decryptErr.Error()) } @@ -97,7 +99,7 @@ func verifySign(c *gin.Context) (map[string]string, error) { // 创建签名 func createSign(params url.Values) (string, error) { - return util.RsaPublicEncrypt(createEncryptStr(params), AppSecret) + return rsa.RsaPublicEncrypt(createEncryptStr(params), AppSecret) } func createEncryptStr(params url.Values) string { diff --git a/app/route/route.go b/app/route/route.go index b1c90531..c0c8b8ab 100644 --- a/app/route/route.go +++ b/app/route/route.go @@ -8,7 +8,7 @@ import ( "go-gin-api/app/route/middleware/exception" "go-gin-api/app/route/middleware/jaeger" "go-gin-api/app/route/middleware/logger" - "go-gin-api/app/util" + "go-gin-api/app/util/response" ) func SetupRouter(engine *gin.Engine) { @@ -18,12 +18,12 @@ func SetupRouter(engine *gin.Engine) { //404 engine.NoRoute(func(c *gin.Context) { - utilGin := util.Gin{Ctx:c} + utilGin := response.Gin{Ctx: c} utilGin.Response(404,"请求方法不存在", nil) }) engine.GET("/ping", func(c *gin.Context) { - utilGin := util.Gin{Ctx:c} + utilGin := response.Gin{Ctx: c} utilGin.Response(1,"pong", nil) }) diff --git a/app/util/aes.go b/app/util/aes/aes.go similarity index 99% rename from app/util/aes.go rename to app/util/aes/aes.go index b8bfce8f..e9a35c77 100644 --- a/app/util/aes.go +++ b/app/util/aes/aes.go @@ -1,4 +1,4 @@ -package util +package aes import ( "bytes" diff --git a/app/util/bind.go b/app/util/bind/bind.go similarity index 95% rename from app/util/bind.go rename to app/util/bind/bind.go index 360f8907..bfbd5a19 100644 --- a/app/util/bind.go +++ b/app/util/bind/bind.go @@ -1,4 +1,4 @@ -package util +package bind import ( "github.com/gin-gonic/gin" diff --git a/app/util/error.go b/app/util/error/error.go similarity index 89% rename from app/util/error.go rename to app/util/error/error.go index 8b4295fb..cd4c004a 100644 --- a/app/util/error.go +++ b/app/util/error/error.go @@ -1,9 +1,11 @@ -package util +package error import ( "fmt" "go-gin-api/app/config" "go-gin-api/app/route/middleware/exception" + "go-gin-api/app/util/json" + time2 "go-gin-api/app/util/time" "log" "os" "runtime/debug" @@ -53,7 +55,7 @@ func alarm(level string, str string) { subject := fmt.Sprintf("【系统告警】%s 项目出错了!", config.AppName) body := strings.ReplaceAll(exception.MailTemplate, "{ErrorMsg}", fmt.Sprintf("%s", str)) - body = strings.ReplaceAll(body, "{RequestTime}", GetCurrentDate()) + body = strings.ReplaceAll(body, "{RequestTime}", time2.GetCurrentDate()) body = strings.ReplaceAll(body, "{RequestURL}", "--") body = strings.ReplaceAll(body, "{RequestUA}", "--") body = strings.ReplaceAll(body, "{RequestIP}", "--") @@ -78,7 +80,7 @@ func alarm(level string, str string) { errorLogMap["time"] = time.Now().Format("2006/01/02 - 15:04:05") errorLogMap["info"] = str - errorLogJson, _ := JsonEncode(errorLogMap) + errorLogJson, _ := json.JsonEncode(errorLogMap) _, _ = f.WriteString(errorLogJson + "\n") } } diff --git a/app/util/grpc_log/grpc_log.go b/app/util/grpc_log/grpc_log.go index 4e69bee6..f39f4ecd 100644 --- a/app/util/grpc_log/grpc_log.go +++ b/app/util/grpc_log/grpc_log.go @@ -4,7 +4,8 @@ import ( "context" "fmt" "go-gin-api/app/config" - "go-gin-api/app/util" + "go-gin-api/app/util/json" + "go-gin-api/app/util/time" "google.golang.org/grpc" "log" "os" @@ -21,12 +22,12 @@ func ClientInterceptor() grpc.UnaryClientInterceptor { invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { // 开始时间 - startTime := util.GetCurrentMilliUnix() + startTime := time.GetCurrentMilliUnix() err := invoker(ctx, method, req, reply, cc, opts...) // 结束时间 - endTime := util.GetCurrentMilliUnix() + endTime := time.GetCurrentMilliUnix() // 日志格式 grpcLogMap := make(map[string]interface{}) @@ -40,7 +41,7 @@ func ClientInterceptor() grpc.UnaryClientInterceptor { grpcLogMap["cost_time"] = fmt.Sprintf("%vms", endTime-startTime) - grpcLogJson, _ := util.JsonEncode(grpcLogMap) + grpcLogJson, _ := json.JsonEncode(grpcLogMap) grpcChannel <- grpcLogJson diff --git a/app/util/json.go b/app/util/json/json.go similarity index 92% rename from app/util/json.go rename to app/util/json/json.go index 400c6064..2a0dc6a5 100644 --- a/app/util/json.go +++ b/app/util/json/json.go @@ -1,4 +1,4 @@ -package util +package json import "encoding/json" diff --git a/app/util/mail.go b/app/util/mail/mail.go similarity index 98% rename from app/util/mail.go rename to app/util/mail/mail.go index 91c287f0..e8475849 100644 --- a/app/util/mail.go +++ b/app/util/mail/mail.go @@ -1,4 +1,4 @@ -package util +package mail import ( "fmt" diff --git a/app/util/md5.go b/app/util/md5/md5.go similarity index 92% rename from app/util/md5.go rename to app/util/md5/md5.go index 42ca96a5..82f9d70b 100644 --- a/app/util/md5.go +++ b/app/util/md5/md5.go @@ -1,4 +1,4 @@ -package util +package md5 import ( "crypto/md5" diff --git a/app/util/request.go b/app/util/request/request.go similarity index 98% rename from app/util/request.go rename to app/util/request/request.go index 0c19a608..fef7cb0f 100644 --- a/app/util/request.go +++ b/app/util/request/request.go @@ -1,4 +1,4 @@ -package util +package request import ( "crypto/tls" diff --git a/app/util/response.go b/app/util/response/response.go similarity index 95% rename from app/util/response.go rename to app/util/response/response.go index dcdcd831..53d2e0d8 100644 --- a/app/util/response.go +++ b/app/util/response/response.go @@ -1,4 +1,4 @@ -package util +package response import "github.com/gin-gonic/gin" diff --git a/app/util/rsa.go b/app/util/rsa/rsa.go similarity index 99% rename from app/util/rsa.go rename to app/util/rsa/rsa.go index 9eccafdf..a9948071 100644 --- a/app/util/rsa.go +++ b/app/util/rsa/rsa.go @@ -1,4 +1,4 @@ -package util +package rsa import ( "crypto/rand" diff --git a/app/util/time.go b/app/util/time/time.go similarity index 97% rename from app/util/time.go rename to app/util/time/time.go index acf94af1..f5f5eca8 100644 --- a/app/util/time.go +++ b/app/util/time/time.go @@ -1,4 +1,4 @@ -package util +package time import "time" diff --git a/app/util/uuid.go b/app/util/uuid/uuid.go similarity index 89% rename from app/util/uuid.go rename to app/util/uuid/uuid.go index a60a57c5..4c07cb09 100644 --- a/app/util/uuid.go +++ b/app/util/uuid/uuid.go @@ -1,4 +1,4 @@ -package util +package uuid import ( "github.com/google/uuid"