Files
2025-07-23 22:17:47 +08:00

72 lines
1.7 KiB
Go

package config
import (
"ca-mini/pkg/utils"
"fmt"
"github.com/spf13/viper"
)
type AppConfig struct {
Name string `mapstructure:"name"`
Version string `mapstructure:"version"`
CopyrightYear int `mapstructure:"copyrightYear"`
}
type ServerConfig struct {
Port int `mapstructure:"port"`
ContextPath string `mapstructure:"context-path"`
}
type LoggingConfig struct {
Level string `mapstructure:"level"`
Path string `mapstructure:"path"`
}
type DatasourceConfig struct {
Url string `mapstructure:"url"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}
// 定义 OAuth 配置结构体
type OAuthConfig struct {
AUTHORIZATION_SERVER_HOST string `yaml:"authorization_server_host"`
CLIENT_ID string `yaml:"client_id"`
CLIENT_SECRET string `yaml:"client_secret"`
REDIRECT_URI string `yaml:"redirect_uri"`
TOKEN_URL string `yaml:"token_url"`
}
type Config struct {
App AppConfig `mapstructure:"app"`
Server ServerConfig `mapstructure:"server"`
Datasource DatasourceConfig `mapstructure:"datasource"`
Logging LoggingConfig `mapstructure:"logging"`
OAuth OAuthConfig `mapstructure:"oauth"`
ServerAddress string
}
func Load() (*Config, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(utils.WORK_PATH + "/conf")
err := viper.ReadInConfig()
if err != nil {
return nil, err
}
var config Config
err = viper.Unmarshal(&config)
if err != nil {
return nil, err
}
// 设置ServerAddress
config.ServerAddress = fmt.Sprintf(":%d", config.Server.Port)
return &config, nil
}