refactor(app): provide sub-config structures from main config
Extract DatabaseConfig, RedisConfig, RustFSConfig, LogConfig, and EmailConfig from the main Config to enable pkg/* package constructors to receive specific config types by value, supporting the fx.Lifecycle OnStop hooks pattern.
This commit is contained in:
21
cmd/genhash/main.go
Normal file
21
cmd/genhash/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"carrotskin/pkg/auth"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "usage: genhash <password>")
|
||||
os.Exit(2)
|
||||
}
|
||||
h, err := auth.HashPassword(os.Args[1])
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "err:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Print(h)
|
||||
}
|
||||
@@ -20,6 +20,13 @@ import (
|
||||
// 每个需要资源释放的组件都通过 fx.Lifecycle 注册 OnStop 钩子,
|
||||
// 彻底替代 main.go 里分散的 defer xxx.Close()。
|
||||
var InfraModule = fx.Options(
|
||||
// 顶层 Config 子结构 Provider:让 pkg/* 包的构造函数按值接收细分 Config
|
||||
fx.Provide(func(c *config.Config) config.DatabaseConfig { return c.Database }),
|
||||
fx.Provide(func(c *config.Config) config.RedisConfig { return c.Redis }),
|
||||
fx.Provide(func(c *config.Config) config.RustFSConfig { return c.RustFS }),
|
||||
fx.Provide(func(c *config.Config) config.LogConfig { return c.Log }),
|
||||
fx.Provide(func(c *config.Config) config.EmailConfig { return c.Email }),
|
||||
|
||||
// Logger
|
||||
fx.Provide(logger.New),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user