Skip to content

Commit

Permalink
✨ add sqlx connect method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiertop committed Mar 28, 2024
1 parent 9f293f8 commit 5f3ee43
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
go.work

.idea
*.log
*.log
.vscode
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/hertz-contrib/keyauth v0.0.0-20221011055545-d02ab20cb1ca
github.com/hertz-contrib/logger/zap v1.1.0
github.com/hertz-contrib/pprof v0.1.2
github.com/jmoiron/sqlx v1.3.5
github.com/redis/go-redis/v9 v9.5.1
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.14.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.9.4/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
Expand Down Expand Up @@ -73,6 +74,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
Expand All @@ -89,6 +92,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
Expand Down
1 change: 1 addition & 0 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var enablePprof = flag.Bool("pprof", false, "open/close pprof")

func Init() *server.Hertz {
flag.Parse()

h := server.New()
// use middleware
if *enablePprof {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"hertz/internal/router"
"hertz/pkg/config"
Expand All @@ -22,6 +23,7 @@ func Init() error {
func main() {
if err := Init(); err != nil {
fmt.Printf("init server failed, err: %v\n", err)
os.Exit(1)
}
engine := router.Init()

Expand Down
7 changes: 3 additions & 4 deletions pkg/middleware/zap_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ func Logger() app.HandlerFunc {
logMsg := []zap.Field{
zap.String("method", utils.BytesToString(c.Request.Method())),
zap.String("route", c.FullPath()),
zap.String("ip", c.RemoteAddr().String()),
}
logMsg = requestArg(c, logMsg)
s := time.Now()
c.Next(ctx)
cost := time.Since(s)

logMsg = append(logMsg,
zap.String("cost", cost.String()),
zap.Int("status", c.Response.StatusCode()),
zap.String("cost", cost.String()),
zap.String("handler", c.FullPath()),
zap.String("ip", c.RemoteAddr().String()),
zap.String("agent", utils.BytesToString(c.UserAgent())))
switch c.Response.StatusCode() {
case http.StatusOK:
Expand Down Expand Up @@ -70,9 +70,8 @@ func requestArg(c *app.RequestContext, logMsg []zap.Field) []zap.Field {
}
}
}
case http.MethodDelete:
case http.MethodDelete, http.MethodPatch:
case http.MethodPut:
case http.MethodPatch:
default:

}
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions pkg/storage/sqlx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package storage

import "github.com/jmoiron/sqlx"

func InitSqlx() error {
var (
driver string
dataSourceName string
)

db, err := sqlx.Open(driver, dataSourceName)
if err != nil {
return err
}
if err = db.Ping(); err != nil {
return err
}
return db.Ping()
}

0 comments on commit 5f3ee43

Please sign in to comment.