init
This commit is contained in:
61
ca-server/internal/config/config.go
Normal file
61
ca-server/internal/config/config.go
Normal file
@@ -0,0 +1,61 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
App AppConfig `mapstructure:"app"`
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Datasource DatasourceConfig `mapstructure:"datasource"`
|
||||
Logging LoggingConfig `mapstructure:"logging"`
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user