Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42331cde57 | ||
|
|
36fd04cac9 | ||
|
|
956cf69a82 |
@@ -5,21 +5,21 @@ import (
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
//mdb, err := New(WithMysql("127.0.0.1", 3306, "root", "MyPassw0rd", "mydb"))
|
||||
//if err != nil {
|
||||
// t.Fatal(err)
|
||||
//}
|
||||
//
|
||||
//type User struct {
|
||||
// Id uint64 `gorm:"primaryKey"`
|
||||
// Username string `gorm:"unique"`
|
||||
//}
|
||||
//
|
||||
//if err = mdb.Session(t.Context()).AutoMigrate(&User{}); err != nil {
|
||||
// t.Fatal(err)
|
||||
//}
|
||||
//
|
||||
//if err = mdb.Session(t.Context()).Create(&User{Username: "zyp"}).Error; err != nil {
|
||||
// t.Fatal(err)
|
||||
//}
|
||||
mdb, err := New(WithMysql("127.0.0.1", 2881, "yizhi@test", "yizhi", "mie"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id uint64 `gorm:"primaryKey"`
|
||||
Username string `gorm:"unique"`
|
||||
}
|
||||
|
||||
if err = mdb.Session(t.Context()).AutoMigrate(&User{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = mdb.Session(t.Context()).Create(&User{Username: "zyp"}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.loveuer.com/yizhisec/packages/logger"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
@@ -25,6 +27,7 @@ func WithCtx(ctx context.Context) OptionFn {
|
||||
func WithMysql(host string, port int, user string, password string, database string) OptionFn {
|
||||
return func(c *config) {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)
|
||||
logger.Debug("db init with mysql, dsn: %s", dsn)
|
||||
c.mysql = &dsn
|
||||
}
|
||||
}
|
||||
@@ -32,6 +35,7 @@ func WithMysql(host string, port int, user string, password string, database str
|
||||
func WithPg(host string, port int, user string, password string, database string) OptionFn {
|
||||
return func(c *config) {
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", host, user, password, database, port)
|
||||
logger.Debug("db init with pg, dsn: %s", dsn)
|
||||
c.pg = &dsn
|
||||
}
|
||||
}
|
||||
@@ -39,6 +43,7 @@ func WithPg(host string, port int, user string, password string, database string
|
||||
func WithSqlite(path string) OptionFn {
|
||||
return func(c *config) {
|
||||
if path != "" {
|
||||
logger.Debug("db init with sqlite, path: %s", path)
|
||||
c.sqlite = &path
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,9 +53,8 @@ func New() gin.HandlerFunc {
|
||||
b := stringBuilderPool.Get().(*strings.Builder)
|
||||
b.Reset()
|
||||
|
||||
b.WriteString("[")
|
||||
b.WriteString(fmt.Sprintf("%36s", trace))
|
||||
b.WriteString("] | ")
|
||||
b.WriteString(" | ")
|
||||
b.WriteString(fmt.Sprintf("%3d", status))
|
||||
b.WriteString(" | ")
|
||||
b.WriteString(fmt.Sprintf("%15s", ip))
|
||||
|
||||
12
resp/resp.go
12
resp/resp.go
@@ -30,6 +30,18 @@ func R200(c *gin.Context, data any, msgs ...string) {
|
||||
c.AbortWithStatusJSON(200, r)
|
||||
}
|
||||
|
||||
func RC(c *gin.Context, status int, args ...any) {
|
||||
code := 1
|
||||
if status != 200 {
|
||||
code = -1
|
||||
}
|
||||
|
||||
_r(c, &res{
|
||||
Status: status,
|
||||
Code: code,
|
||||
}, args...)
|
||||
}
|
||||
|
||||
func RE(c *gin.Context, err error) {
|
||||
var re *Error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user