▍ humdrum codex / custard v0.3.0
license AGPL-3.0
1.4 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Package config resolves runtime configuration from flags and environment.
// Flags win over env; env wins over defaults.
package config

import (
	"flag"
	"os"
)

type Config struct {
	ReposPath     string // directory holding bare *.git repos
	ListenAddr    string // host:port to listen on
	BaseURL       string // public base URL (for absolute links, feeds)
	SoftServeHTTP string // Soft Serve HTTP clone base, reverse-proxied at /git in prod
	SoftServeDB   string // path to soft-serve.db; when set, only public repos are served
}

// Load parses flags (with env fallbacks) and returns the config.
func Load() Config {
	var c Config
	flag.StringVar(&c.ReposPath, "repos", env("REPOS_PATH", "./repos"), "directory of bare *.git repositories")
	flag.StringVar(&c.ListenAddr, "addr", env("LISTEN_ADDR", ":8080"), "listen address")
	flag.StringVar(&c.BaseURL, "base-url", env("BASE_URL", "http://localhost:8080"), "public base URL")
	flag.StringVar(&c.SoftServeHTTP, "soft-serve-http", env("SOFT_SERVE_HTTP", "http://localhost:23232"), "git clone base shown in the UI footer (e.g. https://your-host/git)")
	flag.StringVar(&c.SoftServeDB, "soft-serve-db", env("SOFT_SERVE_DB", ""), "path to soft-serve.db; when set, only public (non-private, non-hidden) repos are served")
	flag.Parse()
	return c
}

func env(key, def string) string {
	if v := os.Getenv(key); v != "" {
		return v
	}
	return def
}