feat(auth): 添加完整的用户认证API项目
- 实现用户注册、登录、JWT令牌认证功能 - 集成Gin、GORM、Viper、Zap等框架 - 添加密码加密、数据库操作、中间件等完整功能 - 配置多环境支持、日志轮转、CORS处理 - 创建完整的项目结构和配置文件体系
This commit is contained in:
36
Web开发/04go-viper-demo/01viper_basic.go
Normal file
36
Web开发/04go-viper-demo/01viper_basic.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1. 设置配置文件名和路径
|
||||
viper.SetConfigName("config") // 配置文件名(不含扩展名)
|
||||
viper.SetConfigType("yaml") // 配置文件类型
|
||||
viper.AddConfigPath(".") // 配置文件路径
|
||||
|
||||
// 2. 读取配置文件
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
panic(fmt.Errorf("Fatal error config file: %s", err))
|
||||
}
|
||||
|
||||
fmt.Println("Config file loaded successfully!")
|
||||
|
||||
// 3. 读取单个配置项
|
||||
appName := viper.GetString("app.name")
|
||||
appPort := viper.GetInt("app.port")
|
||||
fmt.Printf("App Name: %s\n", appName)
|
||||
fmt.Printf("App Port: %d\n", appPort)
|
||||
|
||||
// 4. 读取嵌套配置
|
||||
dbHost := viper.GetString("database.host")
|
||||
dbPort := viper.GetInt("database.port")
|
||||
fmt.Printf("Database: %s:%d\n", dbHost, dbPort)
|
||||
|
||||
// 5. 读取所有配置
|
||||
allSettings := viper.AllSettings()
|
||||
fmt.Printf("All settings: %v\n", allSettings)
|
||||
}
|
||||
Reference in New Issue
Block a user