You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.7 KiB
74 lines
1.7 KiB
|
3 months ago
|
package config
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"os"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Config 配置管理
|
||
|
|
type Config struct {
|
||
|
|
Server ServerConfig `json:"server" yaml:"server"`
|
||
|
|
Database DatabaseConfig `json:"database" yaml:"database"`
|
||
|
|
Redis RedisConfig `json:"redis" yaml:"redis"`
|
||
|
|
Sources SourcesConfig `json:"sources" yaml:"sources"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type ServerConfig struct {
|
||
|
|
Port int `json:"port" yaml:"port"`
|
||
|
|
Mode string `json:"mode" yaml:"mode"` // debug/release
|
||
|
|
APIKey string `json:"api_key" yaml:"api_key"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type DatabaseConfig struct {
|
||
|
|
Host string `json:"host" yaml:"host"`
|
||
|
|
Port int `json:"port" yaml:"port"`
|
||
|
|
User string `json:"user" yaml:"user"`
|
||
|
|
Password string `json:"password" yaml:"password"`
|
||
|
|
Database string `json:"database" yaml:"database"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type RedisConfig struct {
|
||
|
|
Host string `json:"host" yaml:"host"`
|
||
|
|
Port int `json:"port" yaml:"port"`
|
||
|
|
Password string `json:"password" yaml:"password"`
|
||
|
|
DB int `json:"db" yaml:"db"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type SourcesConfig struct {
|
||
|
|
Stock SourceConfig `json:"stock" yaml:"stock"`
|
||
|
|
Futures SourceConfig `json:"futures" yaml:"futures"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type SourceConfig struct {
|
||
|
|
Active string `json:"active" yaml:"active"`
|
||
|
|
Sources map[string]SourceInfo `json:"list" yaml:"list"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type SourceInfo struct {
|
||
|
|
Type string `json:"type" yaml:"type"`
|
||
|
|
Config map[string]string `json:"config" yaml:"config"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// Load 加载配置
|
||
|
|
func Load(path string) (*Config, error) {
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
var cfg Config
|
||
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 设置默认值
|
||
|
|
if cfg.Server.Port == 0 {
|
||
|
|
cfg.Server.Port = 8080
|
||
|
|
}
|
||
|
|
if cfg.Server.Mode == "" {
|
||
|
|
cfg.Server.Mode = "debug"
|
||
|
|
}
|
||
|
|
|
||
|
|
return &cfg, nil
|
||
|
|
}
|