Files
PR-Helper/config/config.go
T

50 lines
1001 B
Go
Raw Normal View History

package config
import (
"crypto/rand"
"encoding/hex"
"os"
"path/filepath"
)
type Config struct {
Port string
GinMode string
DataDir string
SessionSecret string
}
func Load() *Config {
cfg := &Config{
Port: getEnv("PORT", "8080"),
GinMode: getEnv("GIN_MODE", "debug"),
DataDir: getEnv("DATA_DIR", "data"),
SessionSecret: getEnv("SESSION_SECRET", ""),
}
// Generate random session secret if not provided
if cfg.SessionSecret == "" {
b := make([]byte, 32)
rand.Read(b)
cfg.SessionSecret = hex.EncodeToString(b)
}
// Ensure data directories exist
os.MkdirAll(cfg.DataDir, 0o755)
os.MkdirAll(filepath.Join(cfg.DataDir, "repos"), 0o755)
return cfg
}
func (c *Config) DBPath() string {
return filepath.Join(c.DataDir, "pr-helper.db")
}
func (c *Config) ReposDir() string {
return filepath.Join(c.DataDir, "repos")
}
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}