arche / internal/archesrv/config.go

commit 154431fd
 1package archesrv
 2
 3import (
 4	"fmt"
 5	"os"
 6
 7	"github.com/BurntSushi/toml"
 8)
 9
10type Config struct {
11	Server  ServerSection             `toml:"server"`
12	Storage StorageSection            `toml:"storage"`
13	Auth    AuthSection               `toml:"auth"`
14	Hooks   HooksSection              `toml:"hooks"`
15	Repo    map[string]RepoHookConfig `toml:"repo"`
16}
17
18type RepoHookConfig struct {
19	AllowShellHooks      bool   `toml:"allow_shell_hooks"`
20	PostReceive          string `toml:"post-receive"`
21	RequireSignedCommits bool   `toml:"require_signed_commits"`
22}
23
24type ServerSection struct {
25	ListenHTTP  string `toml:"listen_http"`
26	ListenHTTPS string `toml:"listen_https"`
27	ListenSSH   string `toml:"listen_ssh"`
28	ListenMTLS  string `toml:"listen_mtls"`
29	TLSCert     string `toml:"tls_cert"`
30	TLSKey      string `toml:"tls_key"`
31}
32
33type HooksSection struct {
34	PreReceive  string `toml:"pre-receive"`
35	Update      string `toml:"update"`
36	PostReceive string `toml:"post-receive"`
37	TimeoutSec  int    `toml:"timeout_sec"`
38}
39
40type StorageSection struct {
41	DataDir string `toml:"data_dir"`
42}
43
44type AuthSection struct {
45	Registration string `toml:"registration"`
46}
47
48func DefaultConfig() Config {
49	return Config{
50		Server: ServerSection{
51			ListenHTTP: ":8080",
52		},
53		Storage: StorageSection{
54			DataDir: "./data",
55		},
56		Auth: AuthSection{
57			Registration: "disabled",
58		},
59		Hooks: HooksSection{
60			TimeoutSec: 30,
61		},
62	}
63}
64
65func LoadConfig(path string) (Config, error) {
66	cfg := DefaultConfig()
67	if _, err := os.Stat(path); os.IsNotExist(err) {
68		return cfg, nil
69	}
70	if _, err := toml.DecodeFile(path, &cfg); err != nil {
71		return cfg, fmt.Errorf("load config %s: %w", path, err)
72	}
73	return cfg, nil
74}