Add the runnable Gitea, PostgreSQL, and Hop control-plane foundation
Hop-State: A_06FN3QYA1N56NQYGZ81DWGR Hop-Proposal: R_06FN3QXJRYPPEQZQA6S69QG Hop-Task: T_06FN3MVGY3MT82ESQ89BND0 Hop-Attempt: AT_06FN3MVGY092BFA5MR9C7EG
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
HTTPAddr string
|
||||
DatabaseURL string
|
||||
AdminToken string
|
||||
GiteaWebhookSecret string
|
||||
GiteaBaseURL string
|
||||
GiteaAPIToken string
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
cfg := Config{
|
||||
HTTPAddr: value("HOP_HTTP_ADDR", ":8080"),
|
||||
DatabaseURL: os.Getenv("HOP_DATABASE_URL"),
|
||||
AdminToken: os.Getenv("HOP_ADMIN_TOKEN"),
|
||||
GiteaWebhookSecret: os.Getenv("HOP_GITEA_WEBHOOK_SECRET"),
|
||||
GiteaBaseURL: value("GITEA_BASE_URL", "http://localhost:3000"),
|
||||
GiteaAPIToken: os.Getenv("GITEA_API_TOKEN"),
|
||||
}
|
||||
|
||||
if cfg.DatabaseURL == "" {
|
||||
return Config{}, errors.New("HOP_DATABASE_URL is required")
|
||||
}
|
||||
if cfg.AdminToken == "" {
|
||||
return Config{}, errors.New("HOP_ADMIN_TOKEN is required")
|
||||
}
|
||||
if len(cfg.AdminToken) < 16 {
|
||||
return Config{}, errors.New("HOP_ADMIN_TOKEN must be at least 16 characters")
|
||||
}
|
||||
if cfg.GiteaWebhookSecret == "" {
|
||||
return Config{}, errors.New("HOP_GITEA_WEBHOOK_SECRET is required")
|
||||
}
|
||||
if len(cfg.GiteaWebhookSecret) < 16 {
|
||||
return Config{}, errors.New("HOP_GITEA_WEBHOOK_SECRET must be at least 16 characters")
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func value(name, fallback string) string {
|
||||
if v := os.Getenv(name); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLoadRequiresSecretsAndDatabase(t *testing.T) {
|
||||
t.Setenv("HOP_DATABASE_URL", "")
|
||||
t.Setenv("HOP_ADMIN_TOKEN", "")
|
||||
t.Setenv("HOP_GITEA_WEBHOOK_SECRET", "")
|
||||
if _, err := Load(); err == nil {
|
||||
t.Fatal("expected missing configuration error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDefaults(t *testing.T) {
|
||||
t.Setenv("HOP_DATABASE_URL", "postgres://example")
|
||||
t.Setenv("HOP_ADMIN_TOKEN", "admin-token-long-enough")
|
||||
t.Setenv("HOP_GITEA_WEBHOOK_SECRET", "webhook-secret-long-enough")
|
||||
t.Setenv("HOP_HTTP_ADDR", "")
|
||||
t.Setenv("GITEA_BASE_URL", "")
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.HTTPAddr != ":8080" || cfg.GiteaBaseURL != "http://localhost:3000" {
|
||||
t.Fatalf("unexpected defaults: %#v", cfg)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user