diff --git a/srv/cmd/gatekeeper/main.go b/srv/cmd/gatekeeper/main.go index ac56f15..f760063 100644 --- a/srv/cmd/gatekeeper/main.go +++ b/srv/cmd/gatekeeper/main.go @@ -1,7 +1,9 @@ package main import ( - "net/http" + "Crimson-Gatekeeper/internal/common" + "Crimson-Gatekeeper/internal/route" + "fmt" "github.com/go-spring/spring-core/gs" ) @@ -9,8 +11,9 @@ import ( type Test struct{} func main() { - http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("hello world!")) - }) + gs.Provide(common.GetDataBaseClient) + gs.Provide(common.GetQuery) + gs.Provide(route.NewTestBoot).AsRunner() + fmt.Println("注入完成") gs.Run() } diff --git a/srv/internal/application/application.go b/srv/internal/application/application.go deleted file mode 100644 index 0365add..0000000 --- a/srv/internal/application/application.go +++ /dev/null @@ -1,26 +0,0 @@ -package application - -import ( - "context" - - "github.com/gin-gonic/gin" - "github.com/go-spring/spring-core/gs" -) - -type Controller interface { - RegisterRoute() -} - -type AppPak struct { - core *gin.Engine -} - -func (s *AppPak) ListenAndServe(sig gs.ReadySignal) error { - s.core = gin.Default() - s.core.Run("7777") - return nil -} - -func (_ *AppPak) Shutdown(ctx context.Context) error { - return nil -} diff --git a/srv/internal/common/database-client.go b/srv/internal/common/database-client.go index 14bf719..8670655 100644 --- a/srv/internal/common/database-client.go +++ b/srv/internal/common/database-client.go @@ -13,7 +13,7 @@ import ( // 返回值 // - 一个 gorm.DB 的指针 // - 一个清理所有数据库相关数据的函数 -func GetDataBaseClient() (*gorm.DB, func(), error) { +func GetDataBaseClient() *gorm.DB { dsn := "host=localhost " + "user=gatekeeper " + "dbname=crimson " + @@ -25,28 +25,28 @@ func GetDataBaseClient() (*gorm.DB, func(), error) { client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if ero != nil { fmt.Println("数据库链接建立失败") - return nil, nil, ero + panic(ero) } poolCfg, ero := client.DB() if ero != nil { fmt.Println("获取数据库链接池失败") - - return nil, nil, ero + panic(ero) } - cleanPoolCfg := func() { - ero := poolCfg.Close() - if ero != nil { - fmt.Println("清理过程中出现错误") - panic(ero) - } - } + //cleanPoolCfg := func() { + // ero := poolCfg.Close() + // if ero != nil { + // fmt.Println("清理过程中出现错误") + // panic(ero) + // } + //} poolCfg.SetConnMaxIdleTime(3) poolCfg.SetMaxOpenConns(10) + fmt.Println("构建数据库链接") - return client, cleanPoolCfg, nil + return client } func GetQuery(db *gorm.DB) *query.Query { diff --git a/srv/internal/route/register.go b/srv/internal/route/register.go new file mode 100644 index 0000000..31913a7 --- /dev/null +++ b/srv/internal/route/register.go @@ -0,0 +1,35 @@ +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} +}