DI&GIN验证尝试

DI 框架我是没招了,GO 的狗屎接口系统让运行时的DI注入都没办法自动收集接口。
所以...只能捏着鼻子用了。正在想办法解决 GIN 验证框架返回英文的问题
This commit is contained in:
des
2026-01-23 00:02:13 +08:00
parent 688177ba06
commit 731f1d01e6
12 changed files with 157 additions and 132 deletions

View File

@@ -1,17 +1,16 @@
package main
import (
"Crimson-Gatekeeper/internal/database"
"Crimson-Gatekeeper/internal/route"
"github.com/go-spring/spring-core/gs"
)
type Test struct{}
import "fmt"
func main() {
gs.Provide(database.GetDataBaseClient)
gs.Provide(database.GetQuery)
gs.Provide(route.NewTestBoot).AsRunner()
gs.Run()
app, err := getApplication()
if err != nil {
fmt.Println("应用初始化失败")
panic(err)
}
err = app.Start()
if err != nil {
fmt.Println("应用启动失败")
panic(err)
}
}

View File

@@ -0,0 +1,30 @@
//go:build wireinject
// +build wireinject
package main
import (
"Crimson-Gatekeeper/internal/application"
"Crimson-Gatekeeper/internal/database"
"Crimson-Gatekeeper/internal/module/userpak"
"github.com/google/wire"
)
func getApplication() (*application.Application, error) {
wire.Build(
CtrlCollector,
application.NewApplication,
database.GetDataBaseClient,
database.GetQuery,
database.GetPoolCfg,
userpak.NewUserCtl,
)
return nil, nil
}
func CtrlCollector(
c1 *userpak.UserCtl,
) []application.Ctrl {
return []application.Ctrl{c1}
}

View File

@@ -0,0 +1,39 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
package main
import (
"Crimson-Gatekeeper/internal/application"
"Crimson-Gatekeeper/internal/database"
"Crimson-Gatekeeper/internal/module/userpak"
)
// Injectors from wire.go:
func getApplication() (*application.Application, error) {
db, err := database.GetDataBaseClient()
if err != nil {
return nil, err
}
query := database.GetQuery(db)
userCtl := userpak.NewUserCtl(query)
v := CtrlCollector(userCtl)
sqlDB, err := database.GetPoolCfg(db)
if err != nil {
return nil, err
}
applicationApplication := application.NewApplication(v, sqlDB)
return applicationApplication, nil
}
// wire.go:
func CtrlCollector(
c1 *userpak.UserCtl,
) []application.Ctrl {
return []application.Ctrl{c1}
}

View File

@@ -4,7 +4,7 @@ go 1.25.5
require (
github.com/gin-gonic/gin v1.11.0
github.com/go-spring/spring-core v1.2.5
github.com/google/wire v0.7.0
gorm.io/driver/postgres v1.6.0
gorm.io/gen v0.3.27
gorm.io/gorm v1.31.1
@@ -16,15 +16,11 @@ require (
github.com/bytedance/sonic v1.14.0 // indirect
github.com/bytedance/sonic/loader v0.3.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/expr-lang/expr v1.17.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.27.0 // indirect
github.com/go-spring/gs-mock v0.0.5 // indirect
github.com/go-spring/log v0.0.12 // indirect
github.com/go-spring/spring-base v1.2.4 // indirect
github.com/go-sql-driver/mysql v1.9.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-yaml v1.18.0 // indirect
@@ -38,15 +34,12 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.10 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.54.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
go.uber.org/mock v0.5.0 // indirect
@@ -59,7 +52,6 @@ require (
golang.org/x/text v0.33.0 // indirect
golang.org/x/tools v0.41.0 // indirect
google.golang.org/protobuf v1.36.9 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/datatypes v1.2.7 // indirect
gorm.io/driver/mysql v1.6.0 // indirect
gorm.io/hints v1.1.2 // indirect

View File

@@ -9,10 +9,6 @@ github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gE
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/expr-lang/expr v1.17.6 h1:1h6i8ONk9cexhDmowO/A64VPxHScu7qfSl2k8OlINec=
github.com/expr-lang/expr v1.17.6/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
@@ -27,14 +23,6 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
github.com/go-spring/gs-mock v0.0.5 h1:OGC+Lx1XgOoaKfmiN6mLasASQUtbhgVVoDZOhR4d+GA=
github.com/go-spring/gs-mock v0.0.5/go.mod h1:QK0PqZ+Vu9F+BU97zl8fip5XKibvDSoN+ofky413Z6Q=
github.com/go-spring/log v0.0.12 h1:q7we9bk+rZ/1r1HwiEMj5k6v9c4j790VdboYlB6+4/0=
github.com/go-spring/log v0.0.12/go.mod h1:l2L8e4cpQYZETRV2wHPII7CZTAnn2SUBrZnaiTR3QH4=
github.com/go-spring/spring-base v1.2.4 h1:z113Werjmcvoo/78Wp8/QEmxpfga+UpBrVcp9xffShU=
github.com/go-spring/spring-base v1.2.4/go.mod h1:IZDihx2XI4IpAdY3mkKOOHhU3nQbg5xLpi/06EqTvHU=
github.com/go-spring/spring-core v1.2.5 h1:Gnp1dSJMBMnwcV1xWN7jjd/rW7kFKp2aI4qZfh31c0g=
github.com/go-spring/spring-core v1.2.5/go.mod h1:b7RozduuHQod2h93WIU2miOhnytLevjdEgXCaY5x+ug=
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
@@ -50,6 +38,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.7.0 h1:JxUKI6+CVBgCO2WToKy/nQk0sS+amI9z9EjVmdaocj4=
github.com/google/wire v0.7.0/go.mod h1:n6YbUQD9cPKTnHXEBN2DXlOp/mVADhVErcMFb0v3J18=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
@@ -66,14 +56,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
@@ -86,8 +70,6 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -96,10 +78,6 @@ github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -136,10 +114,6 @@ golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View 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}
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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)
}

View File

@@ -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"`
}

View File

@@ -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]}
}

View File

@@ -1 +1,3 @@
DROP TABLE "user" ;
DROP TABLE "user" ;
-- サキュバス喚んだら義母が来た!? --