DI&GIN验证尝试
DI 框架我是没招了,GO 的狗屎接口系统让运行时的DI注入都没办法自动收集接口。 所以...只能捏着鼻子用了。正在想办法解决 GIN 验证框架返回英文的问题
This commit is contained in:
48
srv/internal/application/appliceation.go
Normal file
48
srv/internal/application/appliceation.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Ctrl interface {
|
||||
RegisterRoutes(engine *gin.Engine)
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
srv *http.Server
|
||||
pool *sql.DB
|
||||
}
|
||||
|
||||
func (app *Application) Start() error {
|
||||
return app.srv.ListenAndServe()
|
||||
}
|
||||
|
||||
func (app *Application) Stop() {
|
||||
err := app.pool.Close()
|
||||
if err != nil {
|
||||
fmt.Println("关闭数据库链接遭遇错误")
|
||||
fmt.Println(err)
|
||||
}
|
||||
err = app.srv.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println("优雅关闭服务遭遇错误")
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func NewApplication(cs []Ctrl, p *sql.DB) *Application {
|
||||
route := gin.Default()
|
||||
for _, ctrl := range cs {
|
||||
ctrl.RegisterRoutes(route)
|
||||
}
|
||||
srv := &http.Server{
|
||||
Addr: ":8443",
|
||||
Handler: route,
|
||||
}
|
||||
return &Application{srv, p}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ctxKey struct {
|
||||
desc string
|
||||
}
|
||||
|
||||
var (
|
||||
dataKey = ctxKey{"响应体内容"}
|
||||
)
|
||||
|
||||
// SetData 设置响应数据,可以设置为 nil,或者干脆不设置。此时会返回空的响应包装。
|
||||
func SetData(c *gin.Context, data any) {
|
||||
c.Set(dataKey, data)
|
||||
}
|
||||
|
||||
// GetData 获取响应数据
|
||||
// 除了响应包装器,你不应该在任何地方调用该方法。因为响应数据的类型不一。
|
||||
// 如果你真的对响应数据处理。请在中间件向上下文中塞入数据,在 handler 中处理好。
|
||||
func GetData(c *gin.Context) any {
|
||||
data, _ := c.Get(dataKey)
|
||||
return data
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
@@ -13,7 +14,7 @@ import (
|
||||
// 返回值
|
||||
// - 一个 gorm.DB 的指针
|
||||
// - 一个清理所有数据库相关数据的函数
|
||||
func GetDataBaseClient() *gorm.DB {
|
||||
func GetDataBaseClient() (*gorm.DB, error) {
|
||||
dsn := "host=localhost " +
|
||||
"user=gatekeeper " +
|
||||
"dbname=crimson " +
|
||||
@@ -25,27 +26,21 @@ func GetDataBaseClient() *gorm.DB {
|
||||
client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if ero != nil {
|
||||
fmt.Println("数据库链接建立失败")
|
||||
panic(ero)
|
||||
return nil, ero
|
||||
}
|
||||
|
||||
poolCfg, ero := client.DB()
|
||||
if ero != nil {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func GetPoolCfg(g *gorm.DB) (*sql.DB, error) {
|
||||
pool, err := g.DB()
|
||||
if err != nil {
|
||||
fmt.Println("获取数据库链接池失败")
|
||||
panic(ero)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//cleanPoolCfg := func() {
|
||||
// ero := poolCfg.Close()
|
||||
// if ero != nil {
|
||||
// fmt.Println("清理过程中出现错误")
|
||||
// panic(ero)
|
||||
// }
|
||||
//}
|
||||
|
||||
poolCfg.SetConnMaxIdleTime(3)
|
||||
poolCfg.SetMaxOpenConns(10)
|
||||
|
||||
return client
|
||||
pool.SetConnMaxIdleTime(3)
|
||||
pool.SetMaxOpenConns(10)
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func GetQuery(db *gorm.DB) *query.Query {
|
||||
|
||||
@@ -3,6 +3,7 @@ package userpak
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -17,8 +18,14 @@ func NewUserCtl(q *query.Query) *UserCtl {
|
||||
|
||||
func (u *UserCtl) login(ctx *gin.Context) {
|
||||
lp := loginParam{}
|
||||
err := ctx.ShouldBindBodyWithJSON(&lp)
|
||||
err := ctx.ShouldBindQuery(&lp)
|
||||
if err != nil {
|
||||
fmt.Println("出错啦")
|
||||
fmt.Println(err)
|
||||
}
|
||||
ctx.JSON(http.StatusOK, lp)
|
||||
}
|
||||
|
||||
func (u *UserCtl) RegisterRoutes(eng *gin.Engine) {
|
||||
eng.GET("/login", u.login)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package userpak
|
||||
|
||||
type loginParam struct {
|
||||
Account string `json:"account"`
|
||||
Password string `json:"password"`
|
||||
Account string `json:"account" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Controller interface {
|
||||
register(gin *gin.Engine)
|
||||
}
|
||||
|
||||
func RegCtrl(cs []Controller) {
|
||||
route := gin.Default()
|
||||
for _, ctrl := range cs {
|
||||
ctrl.register(route)
|
||||
}
|
||||
http.Handle("/", route)
|
||||
}
|
||||
|
||||
type TestBoot struct {
|
||||
c *query.Query
|
||||
}
|
||||
|
||||
func (t *TestBoot) Run() error {
|
||||
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello world!"))
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewTestBoot(q []*query.Query) *TestBoot {
|
||||
return &TestBoot{q[0]}
|
||||
}
|
||||
Reference in New Issue
Block a user