BAMS initial project structure
This commit is contained in:
61
backend/internal/config/config.go
Normal file
61
backend/internal/config/config.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int `yaml:"port"`
|
||||
LogLevel string `yaml:"log_level"`
|
||||
DataDir string `yaml:"data_dir"`
|
||||
SCSTConfig string `yaml:"scst_config"`
|
||||
BaculaConfig string `yaml:"bacula_config"`
|
||||
Security SecurityConfig `yaml:"security"`
|
||||
}
|
||||
|
||||
type SecurityConfig struct {
|
||||
RequireHTTPS bool `yaml:"require_https"`
|
||||
AllowedUsers []string `yaml:"allowed_users"`
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
cfg := &Config{
|
||||
Port: 8080,
|
||||
LogLevel: "info",
|
||||
DataDir: "/var/lib/bams",
|
||||
SCSTConfig: "/etc/scst.conf",
|
||||
BaculaConfig: "/etc/bacula/bacula-sd.conf",
|
||||
Security: SecurityConfig{
|
||||
RequireHTTPS: false,
|
||||
AllowedUsers: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
// Load from environment or config file
|
||||
if configPath := os.Getenv("BAMS_CONFIG"); configPath != "" {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
if err := yaml.Unmarshal(data, cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Override with environment variables
|
||||
if portStr := os.Getenv("BAMS_PORT"); portStr != "" {
|
||||
if port, err := strconv.Atoi(portStr); err == nil {
|
||||
cfg.Port = port
|
||||
}
|
||||
}
|
||||
|
||||
if logLevel := os.Getenv("BAMS_LOG_LEVEL"); logLevel != "" {
|
||||
cfg.LogLevel = logLevel
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user