BlueGit

timfa bluegit

Files

bluegit / internal / config / config.go

config.go

feat: eerste basis van BlueGit
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
151aa352 History
config.go 5.5 KiB Download Raw
1// Package config leest het YAML-configuratiebestand van BlueGit.
2package config
3
4import (
5 "fmt"
6 "net/url"
7 "os"
8 "strings"
9
10 "gopkg.in/yaml.v3"
11)
12
13// Config is de installatie op de mini-PC.
14type Config struct {
15 BaseURL string `yaml:"base_url"`
16 Listen string `yaml:"listen"`
17 StoragePath string `yaml:"storage_path"`
18 BehindProxy bool `yaml:"behind_proxy"`
19 Auth Auth `yaml:"auth"`
20 Git Git `yaml:"git"`
21 SSH SSH `yaml:"ssh"`
22 CI CI `yaml:"ci"`
23}
24
25// Auth wijst naar auth.timfalken.com of naar de lokale ontwikkelmodus.
26type Auth struct {
27 Mode string `yaml:"mode"`
28 BaseURL string `yaml:"base_url"`
29 ClientID string `yaml:"client_id"`
30 ClientSecret string `yaml:"client_secret"`
31 ClientSecretFile string `yaml:"client_secret_file"`
32 RedirectPath string `yaml:"redirect_path"`
33 UserAgent string `yaml:"user_agent"`
34 DevUser string `yaml:"dev_user"`
35 DevAuthID int64 `yaml:"dev_auth_id"`
36}
37
38// Git wijst naar de git-binary en de standaardbranch van nieuwe repositories.
39type Git struct {
40 Binary string `yaml:"binary"`
41 DefaultBranch string `yaml:"default_branch"`
42 HookBinary string `yaml:"hook_binary"`
43}
44
45// SSH is de git-over-SSH-listener.
46type SSH struct {
47 Enabled bool `yaml:"enabled"`
48 Listen string `yaml:"listen"`
49}
50
51// CI stuurt de Docker-runner.
52type CI struct {
53 Enabled bool `yaml:"enabled"`
54 Docker string `yaml:"docker_binary"`
55 Concurrency int `yaml:"concurrency"`
56 Network string `yaml:"network"`
57 TimeoutSec int `yaml:"timeout_seconds"`
58}
59
60// Load leest path en vult ontbrekende waarden aan.
61func Load(path string) (Config, error) {
62 raw, err := os.ReadFile(path)
63 if err != nil {
64 return Config{}, fmt.Errorf("config lezen: %w", err)
65 }
66 var c Config
67 if err := yaml.Unmarshal(raw, &c); err != nil {
68 return Config{}, fmt.Errorf("config: %w", err)
69 }
70 c.applyDefaults()
71 if v := os.Getenv("BLUEGIT_STORAGE_PATH"); v != "" {
72 c.StoragePath = v
73 }
74 if v := os.Getenv("BLUEGIT_LISTEN"); v != "" {
75 c.Listen = v
76 }
77 if v := os.Getenv("BLUEGIT_AUTH_CLIENT_SECRET"); v != "" {
78 c.Auth.ClientSecret = v
79 }
80 if c.Auth.ClientSecret == "" && c.Auth.ClientSecretFile != "" {
81 b, err := os.ReadFile(c.Auth.ClientSecretFile)
82 if err != nil {
83 return Config{}, fmt.Errorf("clientgeheim: %w", err)
84 }
85 c.Auth.ClientSecret = strings.TrimSpace(string(b))
86 }
87 if err := c.Validate(); err != nil {
88 return Config{}, err
89 }
90 return c, nil
91}
92
93func (c *Config) applyDefaults() {
94 if c.Listen == "" {
95 c.Listen = "127.0.0.1:8080"
96 }
97 if c.Auth.Mode == "" {
98 c.Auth.Mode = "oauth"
99 }
100 if c.Auth.BaseURL == "" {
101 c.Auth.BaseURL = "https://auth.timfalken.com"
102 }
103 if c.Auth.RedirectPath == "" {
104 c.Auth.RedirectPath = "/auth/callback"
105 }
106 if c.Auth.UserAgent == "" {
107 c.Auth.UserAgent = "BlueGitAccountClient/0.1"
108 }
109 if c.Auth.DevUser == "" {
110 c.Auth.DevUser = "tim"
111 }
112 if c.Auth.DevAuthID == 0 {
113 c.Auth.DevAuthID = 1
114 }
115 if c.Git.Binary == "" {
116 c.Git.Binary = "git"
117 }
118 if c.Git.DefaultBranch == "" {
119 c.Git.DefaultBranch = "main"
120 }
121 if c.SSH.Listen == "" {
122 c.SSH.Listen = "0.0.0.0:2222"
123 }
124 if c.CI.Docker == "" {
125 c.CI.Docker = "docker"
126 }
127 if c.CI.Concurrency <= 0 {
128 c.CI.Concurrency = 1
129 }
130 if c.CI.Network == "" {
131 c.CI.Network = "bridge"
132 }
133 if c.CI.TimeoutSec <= 0 {
134 c.CI.TimeoutSec = 1800
135 }
136 c.Auth.BaseURL = strings.TrimRight(c.Auth.BaseURL, "/")
137 c.BaseURL = strings.TrimRight(c.BaseURL, "/")
138}
139
140// Validate keurt een configuratie goed of af.
141func (c Config) Validate() error {
142 if c.BaseURL == "" {
143 return fmt.Errorf("base_url ontbreekt")
144 }
145 u, err := url.Parse(c.BaseURL)
146 if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" {
147 return fmt.Errorf("base_url moet een absolute http(s)-URL zijn")
148 }
149 if c.StoragePath == "" {
150 return fmt.Errorf("storage_path ontbreekt")
151 }
152 if !strings.HasPrefix(c.Auth.RedirectPath, "/") {
153 return fmt.Errorf("auth.redirect_path moet met / beginnen")
154 }
155 switch c.Auth.Mode {
156 case "oauth":
157 if c.Auth.ClientID == "" || c.Auth.ClientSecret == "" {
158 return fmt.Errorf("oauth vereist auth.client_id en een clientgeheim (veld, bestand of BLUEGIT_AUTH_CLIENT_SECRET)")
159 }
160 if c.Auth.BaseURL == "" {
161 return fmt.Errorf("auth.base_url ontbreekt")
162 }
163 case "dev":
164 if strings.EqualFold(u.Hostname(), "git.timfalken.com") {
165 return fmt.Errorf("auth.mode dev is geweigerd voor git.timfalken.com")
166 }
167 default:
168 return fmt.Errorf("auth.mode moet oauth of dev zijn")
169 }
170 return nil
171}
172
173// RedirectURI is de callback die bij de auth-server geregistreerd moet staan.
174func (c Config) RedirectURI() string {
175 return c.BaseURL + c.Auth.RedirectPath
176}
177
178// Example is een startconfiguratie voor de mini-PC.
179func Example() string {
180 return `# BlueGit op de mini-PC. Kopieer naar /etc/bluegit/config.yml.
181base_url: https://git.timfalken.com
182listen: 127.0.0.1:8080
183# De 4 TB-schijf. De database, repositories, artifacts en releases staan hier.
184storage_path: /mnt/storage/bluegit
185behind_proxy: true
186
187auth:
188 mode: oauth
189 base_url: https://auth.timfalken.com
190 # Het domein is de client_id. Registreer het pad /auth/callback bij /admin.
191 client_id: git.timfalken.com
192 # Laat leeg en zet BLUEGIT_AUTH_CLIENT_SECRET, of gebruik client_secret_file.
193 client_secret: ""
194 client_secret_file: /etc/bluegit/client_secret
195 redirect_path: /auth/callback
196 user_agent: BlueGitAccountClient/0.1
197
198git:
199 binary: git
200 default_branch: main
201
202ssh:
203 enabled: true
204 listen: 0.0.0.0:2222
205
206ci:
207 enabled: true
208 docker_binary: docker
209 concurrency: 1
210 network: bridge
211 timeout_seconds: 1800
212`
213}