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

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