Skip to content

Commit

Permalink
fix: import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Nov 4, 2019
1 parent 2e8c923 commit 9972683
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 65 deletions.
7 changes: 4 additions & 3 deletions app/controller/jaeger_conn/jaeger_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]"
}
Expand All @@ -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)
}
7 changes: 4 additions & 3 deletions app/controller/product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 13 additions & 10 deletions app/controller/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}
10 changes: 6 additions & 4 deletions app/route/middleware/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}()
Expand Down
4 changes: 2 additions & 2 deletions app/route/middleware/limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
20 changes: 11 additions & 9 deletions app/route/middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -37,7 +39,7 @@ func SetUp() gin.HandlerFunc {
c.Writer = bodyLogWriter

// 开始时间
startTime := util.GetCurrentMilliUnix()
startTime := time.GetCurrentMilliUnix()

// 处理请求
c.Next()
Expand All @@ -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()
Expand All @@ -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
}
Expand Down
12 changes: 7 additions & 5 deletions app/route/middleware/sign/aes/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
}
Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions app/route/middleware/sign/md5/md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions app/route/middleware/sign/rsa/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
}
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions app/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
})

Expand Down
2 changes: 1 addition & 1 deletion app/util/aes.go → app/util/aes/aes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package aes

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion app/util/bind.go → app/util/bind/bind.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package bind

import (
"github.com/gin-gonic/gin"
Expand Down
Loading

0 comments on commit 9972683

Please sign in to comment.