zhipeng.hu
3 years ago
6 changed files with 73 additions and 65 deletions
-
10go.mod
-
11initialize/gorm.go
-
52initialize/gorm_mysql.go
-
6log/server_info.log
-
14model/autocode/ercode.go
-
45utils/net.go
@ -1,52 +0,0 @@ |
|||
package initialize |
|||
|
|||
import ( |
|||
"autocode/config" |
|||
"autocode/global" |
|||
"autocode/initialize/internal" |
|||
"gorm.io/driver/mysql" |
|||
"gorm.io/gorm" |
|||
) |
|||
|
|||
// GormMysql 初始化Mysql数据库
|
|||
// Author [piexlmax](https://github.com/piexlmax)
|
|||
// Author [SliverHorn](https://github.com/SliverHorn)
|
|||
func GormMysql() *gorm.DB { |
|||
m := global.GVA_CONFIG.Mysql |
|||
if m.Dbname == "" { |
|||
return nil |
|||
} |
|||
mysqlConfig := mysql.Config{ |
|||
DSN: m.Dsn(), // DSN data source name
|
|||
DefaultStringSize: 191, // string 类型字段的默认长度
|
|||
SkipInitializeWithVersion: false, // 根据版本自动配置
|
|||
} |
|||
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config()); err != nil { |
|||
return nil |
|||
} else { |
|||
sqlDB, _ := db.DB() |
|||
sqlDB.SetMaxIdleConns(m.MaxIdleConns) |
|||
sqlDB.SetMaxOpenConns(m.MaxOpenConns) |
|||
return db |
|||
} |
|||
} |
|||
|
|||
// GormMysqlByConfig 初始化Mysql数据库用过传入配置
|
|||
func GormMysqlByConfig(m config.DB) *gorm.DB { |
|||
if m.Dbname == "" { |
|||
return nil |
|||
} |
|||
mysqlConfig := mysql.Config{ |
|||
DSN: m.Dsn(), // DSN data source name
|
|||
DefaultStringSize: 191, // string 类型字段的默认长度
|
|||
SkipInitializeWithVersion: false, // 根据版本自动配置
|
|||
} |
|||
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config()); err != nil { |
|||
panic(err) |
|||
} else { |
|||
sqlDB, _ := db.DB() |
|||
sqlDB.SetMaxIdleConns(m.MaxIdleConns) |
|||
sqlDB.SetMaxOpenConns(m.MaxOpenConns) |
|||
return db |
|||
} |
|||
} |
@ -0,0 +1,14 @@ |
|||
// 自动生成模板UserInfo
|
|||
package autocode |
|||
|
|||
// UserInfo 结构体
|
|||
// 如果含有time.Time 请自行import time包
|
|||
type Ercode struct { |
|||
WechatId string `json:"wechatId" form:"wechatId" gorm:"column:wechat_id;comment:;"` |
|||
QrcodeId string `json:"qrcodeId" form:"qrcodeId" gorm:"column:qrcode_id;comment:;"` |
|||
} |
|||
|
|||
// TableName 表名
|
|||
func (Ercode) TableName() string { |
|||
return "ercode" |
|||
} |
@ -0,0 +1,45 @@ |
|||
package utils |
|||
|
|||
import ( |
|||
"errors" |
|||
"fmt" |
|||
"github.com/valyala/fasthttp" |
|||
) |
|||
|
|||
func Get(url string) ([]byte, error) { |
|||
status, resp, err := fasthttp.Get(nil, url) |
|||
if err != nil { |
|||
return []byte{}, err |
|||
} |
|||
if status != fasthttp.StatusOK { |
|||
return []byte{}, errors.New(fmt.Sprintf("请求失败:%d", status)) |
|||
} |
|||
return resp, nil |
|||
|
|||
} |
|||
|
|||
func PostJson(url string, JsonString string) ([]byte, error) { |
|||
req := fasthttp.AcquireRequest() |
|||
resp := fasthttp.AcquireResponse() |
|||
defer func() { |
|||
// 用完需要释放资源
|
|||
fasthttp.ReleaseResponse(resp) |
|||
fasthttp.ReleaseRequest(req) |
|||
}() |
|||
|
|||
// 默认是application/x-www-form-urlencoded
|
|||
req.Header.SetContentType("application/json") |
|||
req.Header.SetMethod("POST") |
|||
|
|||
req.SetRequestURI(url) |
|||
|
|||
requestBody := []byte(JsonString) |
|||
req.SetBody(requestBody) |
|||
|
|||
if err := fasthttp.Do(req, resp); err != nil { |
|||
return []byte{}, err |
|||
} |
|||
b := resp.Body() |
|||
fmt.Println(string(b)) |
|||
return b, nil |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue