好像有戏
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
35
srv/internal/route/register.go
Normal file
35
srv/internal/route/register.go
Normal file
@@ -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}
|
||||
}
|
||||
Reference in New Issue
Block a user