1package config
2
3import (
4 "github.com/caarlos0/env/v6"
5 "github.com/charmbracelet/log"
6 "github.com/charmbracelet/soft-serve/server/backend"
7 "github.com/charmbracelet/soft-serve/server/backend/file"
8)
9
10// SSHConfig is the configuration for the SSH server.
11type SSHConfig struct {
12 // ListenAddr is the address on which the SSH server will listen.
13 ListenAddr string `env:"LISTEN_ADDR" envDefault:":23231"`
14
15 // KeyPath is the path to the SSH server's private key.
16 KeyPath string `env:"KEY_PATH" envDefault:"soft_serve"`
17
18 // MaxTimeout is the maximum number of seconds a connection can take.
19 MaxTimeout int `env:"MAX_TIMEOUT" envDefault:"0"`
20
21 // IdleTimeout is the number of seconds a connection can be idle before it is closed.
22 IdleTimeout int `env:"IDLE_TIMEOUT" envDefault:"120"`
23}
24
25// GitConfig is the Git daemon configuration for the server.
26type GitConfig struct {
27 // ListenAddr is the address on which the Git daemon will listen.
28 ListenAddr string `env:"LISTEN_ADDR" envDefault:":9418"`
29
30 // MaxTimeout is the maximum number of seconds a connection can take.
31 MaxTimeout int `env:"MAX_TIMEOUT" envDefault:"0"`
32
33 // IdleTimeout is the number of seconds a connection can be idle before it is closed.
34 IdleTimeout int `env:"IDLE_TIMEOUT" envDefault:"3"`
35
36 // MaxConnections is the maximum number of concurrent connections.
37 MaxConnections int `env:"MAX_CONNECTIONS" envDefault:"32"`
38}
39
40// Config is the configuration for Soft Serve.
41type Config struct {
42 // SSH is the configuration for the SSH server.
43 SSH SSHConfig `envPrefix:"SSH_"`
44
45 // Git is the configuration for the Git daemon.
46 Git GitConfig `envPrefix:"GIT_"`
47
48 // InitialAdminKeys is a list of public keys that will be added to the list of admins.
49 InitialAdminKeys []string `env:"INITIAL_ADMIN_KEY" envSeparator:"\n"`
50
51 // DataPath is the path to the directory where Soft Serve will store its data.
52 DataPath string `env:"DATA_PATH" envDefault:"data"`
53
54 // Debug enables debug logging.
55 Debug bool `env:"DEBUG" envDefault:"false"`
56
57 // Backend is the Git backend to use.
58 Backend backend.Backend
59
60 // Access is the access control backend to use.
61 Access backend.AccessMethod
62}
63
64// DefaultConfig returns a Config with the values populated with the defaults
65// or specified environment variables.
66func DefaultConfig() *Config {
67 cfg := &Config{}
68 if err := env.Parse(cfg, env.Options{
69 Prefix: "SOFT_SERVE_",
70 }); err != nil {
71 log.Fatal(err)
72 }
73 if cfg.Debug {
74 log.SetLevel(log.DebugLevel)
75 }
76 fb, err := file.NewFileBackend(cfg.DataPath)
77 if err != nil {
78 log.Fatal(err)
79 }
80 return cfg.WithBackend(fb).WithAccessMethod(fb)
81}
82
83// WithBackend sets the backend for the configuration.
84func (c *Config) WithBackend(backend backend.Backend) *Config {
85 c.Backend = backend
86 return c
87}
88
89// WithAccessMethod sets the access control method for the configuration.
90func (c *Config) WithAccessMethod(access backend.AccessMethod) *Config {
91 c.Access = access
92 return c
93}