50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Env string `yaml:"env"`
|
|
Port string `yaml:"port"`
|
|
DSN string `yaml:"dsn"`
|
|
StorageDir string `yaml:"storage_dir"`
|
|
MaxUploadBytes int64 `yaml:"max_upload_bytes"`
|
|
AuthSecret string `yaml:"auth_secret"`
|
|
SystemAIKey string `yaml:"system_ai_key"`
|
|
AIKeyEncryptionSecret string `yaml:"ai_key_encryption_secret"`
|
|
AllowedOrigins []string `yaml:"allowed_origins"`
|
|
}
|
|
|
|
func Load() Config {
|
|
cfg, err := LoadFromDir("etc")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func LoadFromDir(configDir string) (Config, error) {
|
|
mode := strings.TrimSpace(os.Getenv("SENLIN_APP_MODE"))
|
|
if mode == "" {
|
|
mode = "dev"
|
|
}
|
|
path := filepath.Join(configDir, "agent."+strings.ToLower(mode)+".yaml")
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if cfg.MaxUploadBytes <= 0 {
|
|
cfg.MaxUploadBytes = 32 << 20
|
|
}
|
|
return cfg, nil
|
|
}
|