feat: add config package with env-var loading
This commit is contained in:
29
internal/config/config.go
Normal file
29
internal/config/config.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Port string // SUPERVISOR_PORT, default 3200
|
||||||
|
LiteLLMBaseURL string // LITELLM_BASE_URL, default http://iguana:4000
|
||||||
|
LiteLLMAPIKey string // LITELLM_API_KEY
|
||||||
|
ConfigDir string // SUPERVISOR_CONFIG_DIR, default ./config/supervisor
|
||||||
|
ModelsFile string // SUPERVISOR_MODELS_FILE, default <ConfigDir>/../models.yaml
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load() (Config, error) {
|
||||||
|
cfg := Config{
|
||||||
|
Port: envOr("SUPERVISOR_PORT", "3200"),
|
||||||
|
LiteLLMBaseURL: envOr("LITELLM_BASE_URL", "http://iguana:4000"),
|
||||||
|
LiteLLMAPIKey: os.Getenv("LITELLM_API_KEY"),
|
||||||
|
ConfigDir: envOr("SUPERVISOR_CONFIG_DIR", "./config/supervisor"),
|
||||||
|
}
|
||||||
|
cfg.ModelsFile = envOr("SUPERVISOR_MODELS_FILE", cfg.ConfigDir+"/../models.yaml")
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func envOr(key, def string) string {
|
||||||
|
if v := os.Getenv(key); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return def
|
||||||
|
}
|
||||||
36
internal/config/config_test.go
Normal file
36
internal/config/config_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package config_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mathiasbq/supervisor/internal/config"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadDefaults(t *testing.T) {
|
||||||
|
t.Setenv("SUPERVISOR_PORT", "")
|
||||||
|
t.Setenv("LITELLM_BASE_URL", "")
|
||||||
|
t.Setenv("LITELLM_API_KEY", "")
|
||||||
|
t.Setenv("SUPERVISOR_CONFIG_DIR", "")
|
||||||
|
|
||||||
|
cfg, err := config.Load()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "3200", cfg.Port)
|
||||||
|
assert.Equal(t, "http://iguana:4000", cfg.LiteLLMBaseURL)
|
||||||
|
assert.Equal(t, "./config/supervisor", cfg.ConfigDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadFromEnv(t *testing.T) {
|
||||||
|
t.Setenv("SUPERVISOR_PORT", "4000")
|
||||||
|
t.Setenv("LITELLM_BASE_URL", "http://localhost:4000")
|
||||||
|
t.Setenv("LITELLM_API_KEY", "test-key")
|
||||||
|
t.Setenv("SUPERVISOR_CONFIG_DIR", "/etc/supervisor")
|
||||||
|
|
||||||
|
cfg, err := config.Load()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "4000", cfg.Port)
|
||||||
|
assert.Equal(t, "http://localhost:4000", cfg.LiteLLMBaseURL)
|
||||||
|
assert.Equal(t, "test-key", cfg.LiteLLMAPIKey)
|
||||||
|
assert.Equal(t, "/etc/supervisor", cfg.ConfigDir)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user