feat(auth): 添加完整的用户认证API项目
- 实现用户注册、登录、JWT令牌认证功能 - 集成Gin、GORM、Viper、Zap等框架 - 添加密码加密、数据库操作、中间件等完整功能 - 配置多环境支持、日志轮转、CORS处理 - 创建完整的项目结构和配置文件体系
This commit is contained in:
39
Web开发/04go-viper-demo/05viper_watch.go
Normal file
39
Web开发/04go-viper-demo/05viper_watch.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 读取配置文件
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".")
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Initial config loaded")
|
||||
fmt.Printf("App Name: %s\n", viper.GetString("app.name"))
|
||||
fmt.Printf("App Port: %d\n", viper.GetInt("app.port"))
|
||||
|
||||
// 监听配置文件变化
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(in fsnotify.Event) {
|
||||
fmt.Println("\n--- Config file changed! ---")
|
||||
fmt.Printf("Event: %s\n", in.Op.String())
|
||||
fmt.Printf("File: %s\n", in.Name)
|
||||
fmt.Printf("App Name: %s\n", viper.GetString("app.name"))
|
||||
fmt.Printf("App Port: %d\n", viper.GetInt("app.port"))
|
||||
})
|
||||
|
||||
fmt.Println("\nWatching for config changes... (modify config.yaml to see changes)")
|
||||
fmt.Println("Press Ctrl+C to exit")
|
||||
|
||||
// 保持程序运行
|
||||
select {}
|
||||
}
|
||||
Reference in New Issue
Block a user