Refactor: Implement uber-go/fx dependency injection

- Replace global variable pattern with proper dependency injection
- Add uber-go/fx for automatic dependency resolution
- Refactor all services and handlers to use constructor injection
- Eliminate fragile initialization order dependencies
- Improve testability and modularity
- Add structured logging with zap

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
2025-09-16 11:41:01 +03:30
co-authored by Qwen-Coder
parent d4044b0eaf
commit f136ae58b3
15 changed files with 389 additions and 255 deletions
+8 -19
View File
@@ -11,13 +11,11 @@ import (
"gorm.io/gorm"
)
var Gorm *gorm.DB
func Connect() (*gorm.DB, error) {
func Connect(cfg *config.Config) (*gorm.DB, error) {
var db *gorm.DB
var err error = nil
if config.Config.DBEngine == "sqlite" {
if cfg.DBEngine == "sqlite" {
_, err = os.ReadDir("data")
if err != nil {
err = os.Mkdir("data", 0755)
@@ -31,11 +29,11 @@ func Connect() (*gorm.DB, error) {
}
} else {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.Config.DBUser,
config.Config.DBPassword,
config.Config.DBHost,
config.Config.DBPort,
config.Config.DBName)
cfg.DBUser,
cfg.DBPassword,
cfg.DBHost,
cfg.DBPort,
cfg.DBName)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
@@ -54,13 +52,4 @@ func Connect() (*gorm.DB, error) {
return nil, err
}
return db, nil
}
// IDK if this is how it works or not, leave it as is for now
func Init() {
var err error
Gorm, err = Connect()
if err != nil {
panic(err)
}
}
}