From a289a90676cbebd18d99e5c063150d0da8fc6917 Mon Sep 17 00:00:00 2001 From: des <18638715007@163.com> Date: Tue, 13 Jan 2026 13:02:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=90=AD=E5=BB=BA02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srv/cmd/gatekeeper/main.go | 12 ++--------- srv/internal/common/database-client.go | 29 +++++++++++++++++++++----- srv/internal/user/controller.go | 23 ++++++++++++++++---- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/srv/cmd/gatekeeper/main.go b/srv/cmd/gatekeeper/main.go index a68c39e..159d43b 100644 --- a/srv/cmd/gatekeeper/main.go +++ b/srv/cmd/gatekeeper/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "net/http" "github.com/labstack/echo" @@ -10,15 +9,8 @@ import ( ) func main() { - client := common.GetDataBaseClient() - - config, ero := client.DB() - if ero != nil { - fmt.Println("获取连接池失败") - panic(ero) - } - - defer config.Close() + client, close := common.GetDataBaseClient() + defer close() app := echo.New() app.GET("/", func(ctx echo.Context) error { return ctx.String(http.StatusOK, "Hello, World!") diff --git a/srv/internal/common/database-client.go b/srv/internal/common/database-client.go index dc4190f..be29f4c 100644 --- a/srv/internal/common/database-client.go +++ b/srv/internal/common/database-client.go @@ -1,11 +1,18 @@ package common import ( + "fmt" + "gorm.io/driver/postgres" "gorm.io/gorm" ) -func GetDataBaseClient() *gorm.DB { +// GetDataBaseClient 获取数据库链接 +// +// 返回值 +// - 一个 gorm.DB 的指针 +// - 一个清理所有数据库相关数据的函数 +func GetDataBaseClient() (*gorm.DB, func() error) { dsn := "host=localhost " + "user=gatekeeper " + "dbname=crimson " + @@ -13,9 +20,21 @@ func GetDataBaseClient() *gorm.DB { "port=5432 " + "password=crimson " + "connect_timeout=20 " - client, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) - if err != nil { - panic("无法连接数据库") + + client, ero := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if ero != nil { + fmt.Println("数据库链接建立失败") + panic(ero) } - return client + + poolCfg, ero := client.DB() + if ero != nil { + fmt.Println("获取数据库链接池失败") + panic(ero) + } + + poolCfg.SetConnMaxIdleTime(3) + poolCfg.SetMaxOpenConns(10) + + return client, poolCfg.Close } diff --git a/srv/internal/user/controller.go b/srv/internal/user/controller.go index b794b4c..888f5e0 100644 --- a/srv/internal/user/controller.go +++ b/srv/internal/user/controller.go @@ -4,14 +4,29 @@ import ( "net/http" "github.com/labstack/echo" + "gorm.io/gorm" ) -type Controller struct{} +type Controller struct { + client *gorm.DB + group string +} -func (Controller) GetLogin(ctx echo.Context) error { +func (*Controller) GetLogin(ctx echo.Context) error { return ctx.String(http.StatusOK, "success") } -func New() *Controller { - return &Controller{} +func (s *Controller) RegisterRoute(echo *echo.Echo) { + echo.GET("/login", s.GetLogin) +} + +// New 获取用户模块的控制器 +// +// 参数 +// - gorm.DB 该模块所使用的数据库链接 +// +// 返回值 +// - Controller 控制器的实例指针 +func New(client *gorm.DB) *Controller { + return &Controller{client} }