fix: import cycle

This commit is contained in:
新亮
2019-11-04 19:25:10 +08:00
parent 2e8c923e74
commit 99726835cd
22 changed files with 83 additions and 65 deletions
+4 -3
View File
@@ -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)
}
+4 -3
View File
@@ -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(&param_bind.ProductAdd{}, c)
s, e := bind.Bind(&param_bind.ProductAdd{}, c)
if e != nil {
utilGin.Response(-1, e.Error(), nil)
return
+13 -10
View File
@@ -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)
}
+6 -4
View File
@@ -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)
}
}()
+2 -2
View File
@@ -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
+11 -9
View File
@@ -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
}
+7 -5
View File
@@ -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 {
+6 -4
View File
@@ -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 {
+7 -5
View File
@@ -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 {
+3 -3
View File
@@ -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)
})
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package aes
import (
"bytes"
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package bind
import (
"github.com/gin-gonic/gin"
@@ -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")
}
}
+5 -4
View File
@@ -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
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package json
import "encoding/json"
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package mail
import (
"fmt"
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package md5
import (
"crypto/md5"
@@ -1,4 +1,4 @@
package util
package request
import (
"crypto/tls"
@@ -1,4 +1,4 @@
package util
package response
import "github.com/gin-gonic/gin"
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package rsa
import (
"crypto/rand"
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package time
import "time"
+1 -1
View File
@@ -1,4 +1,4 @@
package util
package uuid
import (
"github.com/google/uuid"