Skip to content

Commit

Permalink
Change go.mod 版本要求升至1.17及其他优化
Browse files Browse the repository at this point in the history
  • Loading branch information
钟永标 committed Apr 7, 2024
1 parent c218cd6 commit 48d9e34
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 54 deletions.
6 changes: 5 additions & 1 deletion cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"http_addr": "0.0.0.0:9999",
"log_dir": "/apps/logs/go/zgin",
"trace_log_dir": "/apps/logs/go/trace",
"trace_exclude_routes": ["health","metrics","swag/json"],
"trace_exclude_routes": [
"health",
"metrics",
"swag/json"
],
"version_key": "app_version",
"sign_key": "sign",
"sign_time_key": "utime",
Expand Down
7 changes: 4 additions & 3 deletions consts/mode.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package consts

const (
DevMode = "development"
GrayMode = "gray"
ProMode = "production"
DevMode = "development" // 开发环境
TestMode = "test" // 测试环境
GrayMode = "gray" // 预发布环境
ProMode = "production" // 生产环境
)
6 changes: 6 additions & 0 deletions consts/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package consts

const (
NormalFormat = "2006-01-02 15:04:05"
LogTimeFormat = "2006/01/02 15:04:05.000"
)
59 changes: 56 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
module github.com/ZYallers/zgin

go 1.15
go 1.17

require (
github.com/ZYallers/golib v1.3.30
github.com/ZYallers/golib v1.3.40
github.com/arl/statsviz v0.2.2
github.com/gin-gonic/gin v1.5.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/prometheus/client_golang v1.8.0
github.com/spf13/viper v1.7.1
github.com/syyongx/php2go v0.9.7
go.uber.org/zap v1.21.0
gorm.io/gorm v1.20.8
)

require (
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coocood/freecache v1.2.4 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.1.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/go-isatty v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.14.0 // indirect
github.com/prometheus/procfs v0.2.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.3 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/techoner/gophp v0.2.0 // indirect
github.com/tidwall/gjson v1.6.1 // indirect
github.com/tidwall/match v1.0.1 // indirect
github.com/tidwall/pretty v1.0.2 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/go-playground/validator.v9 v9.29.1 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/mysql v1.0.3 // indirect
gorm.io/gorm v1.20.8 // indirect
)
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMx
github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI=
github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/ZYallers/golib v1.3.30 h1:CzAzIQwXlLht0cVGpB33ksE17tJwivc9kLMJ7WU/j6U=
github.com/ZYallers/golib v1.3.30/go.mod h1:bh+bXEQzouJQ+c0hZRa8eeh/EkxPlRJHzTsW0GV5qf8=
github.com/ZYallers/golib v1.3.40 h1:mmY03PhOjt8gtPw7SuVfvzC0oyPOCEXW7+QVR4LGXOQ=
github.com/ZYallers/golib v1.3.40/go.mod h1:bh+bXEQzouJQ+c0hZRa8eeh/EkxPlRJHzTsW0GV5qf8=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -296,7 +296,6 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
Expand Down Expand Up @@ -481,7 +480,6 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
Expand All @@ -490,7 +488,6 @@ golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -612,12 +609,10 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
Expand Down
8 changes: 4 additions & 4 deletions handler/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

var (
once sync.Once
prom = promhttp.Handler()
appInfo = prometheus.NewCounterVec(
promSingleton sync.Once
prom = promhttp.Handler()
appInfo = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "go_app_info",
Help: "now running go app information.",
Expand All @@ -33,7 +33,7 @@ func WithPrometheus() option.App {
}

func PrometheusHandler(ctx *gin.Context, name string) {
once.Do(func() {
promSingleton.Do(func() {
prometheus.MustRegister(appInfo)
appInfo.WithLabelValues(name, strings.Join(os.Args, " ")).Inc()
})
Expand Down
8 changes: 3 additions & 5 deletions helper/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

var (
cfg map[string]interface{}
cfgOnce sync.Once
singleton sync.Once
cfg map[string]interface{}
)

func ReadFile(args ...string) error {
Expand All @@ -37,9 +37,7 @@ func ReadFile(args ...string) error {
}

func AllSettings() map[string]interface{} {
cfgOnce.Do(func() {
cfg = viper.AllSettings()
})
singleton.Do(func() { cfg = viper.AllSettings() })
return cfg
}

Expand Down
10 changes: 9 additions & 1 deletion helper/dingtalk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import (
"github.com/ZYallers/zgin/helper/config"
)

var systemIP, publicIP, hostName, name, httpAddr, gracefulToken, errorToken string
var (
systemIP string
publicIP string
hostName string
name string
httpAddr string
gracefulToken string
errorToken string
)

func getHostName() string {
if hostName == "" {
Expand Down
12 changes: 8 additions & 4 deletions helper/dingtalk/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ZYallers/golib/funcs/nets"
"github.com/ZYallers/golib/utils/curl"
"github.com/ZYallers/zgin/consts"
"github.com/gin-gonic/gin"
)

Expand All @@ -29,11 +30,11 @@ func PushMessage(token string, msg interface{}, options ...interface{}) {
"Mode: " + gin.Mode(),
"Listen: " + getHttpAddr(),
"HostName: " + getHostName(),
"Time: " + time.Now().Format("2006/01/02 15:04:05.000"),
"Time: " + time.Now().Format(consts.LogTimeFormat),
"SystemIP: " + getSystemIP(),
"PublicIP: " + getPublicIP(),
}
ol := len(options)
var ol = len(options)
var isAtAll bool
if ol > 0 && !gin.IsDebugging() {
if val, ok := options[0].(bool); ok {
Expand All @@ -57,9 +58,12 @@ func PushMessage(token string, msg interface{}, options ...interface{}) {
}
data := map[string]interface{}{
"msgtype": "text",
"text": map[string]string{"content": strings.Join(text, "\n") + "\n"},
"at": map[string]interface{}{"isAtAll": isAtAll},
"text": map[string]string{"content": strings.Join(text, "\n") + "\n"},
}
_, _ = curl.NewRequest("https://oapi.dingtalk.com/robot/send?access_token=" + token).
SetContentType("application/json").SetTimeOut(3 * time.Second).SetPostData(data).Post()
SetContentType(consts.JsonContentTypeValue).
SetTimeOut(3 * time.Second).
SetPostData(data).
Post()
}
11 changes: 9 additions & 2 deletions helper/safe/defer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ func Defer() {
if r == nil {
return
}
err := fmt.Sprintf("%v", r)

// 尝试将r转换为error,以获得更好的错误信息
var err error
if e, ok := r.(error); ok {
err = e
} else {
err = fmt.Errorf("%v", r)
}
stack := string(debug.Stack())
logger.Use("panic").Error(err, zap.String("debug_stack", stack))
logger.Use("panic").Error(err.Error(), zap.String("debug_stack", stack))
dingtalk.PushSimpleMessage(fmt.Sprintf("recovery from panic:\n%s\n%s", err, stack), true)
}
24 changes: 0 additions & 24 deletions types/model.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
package types

import (
"time"

"github.com/ZYallers/golib/utils/logger"
"github.com/ZYallers/golib/utils/mysql"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
)

type Model struct {
mysql.Model
}

func (m *Model) Config(db string, options ...interface{}) func() *gorm.Config {
logLevel := gormLogger.Warn
slowThreshold := 500 * time.Millisecond
ol := len(options)
if ol == 0 && gin.IsDebugging() {
logLevel = gormLogger.Info
}
if ol > 0 {
logLevel = options[0].(gormLogger.LogLevel)
}
if ol > 1 {
slowThreshold = options[1].(time.Duration)
}
return func() *gorm.Config {
return &gorm.Config{Logger: logger.NewGormLogger(db, slowThreshold, logLevel, &Sender{})}
}
}

0 comments on commit 48d9e34

Please sign in to comment.