Compare commits

..

2 Commits

Author SHA1 Message Date
f681f0bb17 ```
refactor(service): 优化Use函数参数类型

移除Use函数中initFunc参数的指针包装,直接使用func() error类型。
简化函数内部调用逻辑,提升代码可读性和健壮性。
```
2025-09-23 12:41:31 +08:00
44319d03b9 ```
refactor(database): 移除全局初始化函数定义

将数据库初始化函数从 database 包中移除,避免全局状态污染。

feat(service): 新增 Use 方法用于执行初始化函数

在 Service 结构体中添加 Use 方法,允许传入并执行初始化函数。
如果函数执行失败,则打印错误并 panic。

refactor(with): 删除旧的初始化逻辑包

删除 with 包中与数据库初始化相关的旧逻辑,统一初始化入口。
```
2025-09-23 12:37:00 +08:00
3 changed files with 8 additions and 20 deletions

View File

@@ -15,8 +15,6 @@ import (
var ( var (
// MigrateTables holds the tables that need to be auto-migrated on database initialization // MigrateTables holds the tables that need to be auto-migrated on database initialization
MigrateTables []any MigrateTables []any
// Init is an optional initialization function that can be executed after database connection is established
InitFunc *func() error = nil
) )
// NewDatabase creates a new database connection based on the provided driver type // NewDatabase creates a new database connection based on the provided driver type

View File

@@ -108,6 +108,14 @@ func (s *Service) Gateway(grpcAddr string, httpAddr string) {
http.ListenAndServe(httpAddr, s.Opts.GatewayMux) http.ListenAndServe(httpAddr, s.Opts.GatewayMux)
} }
func (s *Service) Use(initFunc func() error) {
err := (initFunc)()
if err != nil {
print.Error(err.Error())
panic(err)
}
}
func (s *Service) Stop() { func (s *Service) Stop() {
s.GrpcSrv.GracefulStop() s.GrpcSrv.GracefulStop()
} }

View File

@@ -1,18 +0,0 @@
package with
import (
"git.apinb.com/bsm-sdk/core/database"
"git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/print"
)
func InitData() {
// Execute the Init function if it's not nil
if database.InitFunc != nil {
err := (*database.InitFunc)()
if err != nil {
print.Error(errcode.ErrEtcd.Error())
panic(err)
}
}
}