2025-07-23 17:30:33 +08:00
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 22:17:47 +08:00
|
|
|
// 定义 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"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 17:30:33 +08:00
|
|
|
type Config struct {
|
|
|
|
|
App AppConfig `mapstructure:"app"`
|
|
|
|
|
Server ServerConfig `mapstructure:"server"`
|
|
|
|
|
Datasource DatasourceConfig `mapstructure:"datasource"`
|
|
|
|
|
Logging LoggingConfig `mapstructure:"logging"`
|
2025-07-23 22:17:47 +08:00
|
|
|
OAuth OAuthConfig `mapstructure:"oauth"`
|
2025-07-23 17:30:33 +08:00
|
|
|
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
|
|
|
|
|
}
|