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	// Backend is the Git backend to use.
55	Backend backend.Backend
56
57	// Access is the access control backend to use.
58	Access backend.AccessMethod
59}
60
61// DefaultConfig returns a Config with the values populated with the defaults
62// or specified environment variables.
63func DefaultConfig() *Config {
64	cfg := &Config{}
65	if err := env.Parse(cfg, env.Options{
66		Prefix: "SOFT_SERVE_",
67	}); err != nil {
68		log.Fatal(err)
69	}
70	fb, err := file.NewFileBackend(cfg.DataPath)
71	if err != nil {
72		log.Fatal(err)
73	}
74	return cfg.WithBackend(fb).WithAccessMethod(fb)
75}
76
77// WithBackend sets the backend for the configuration.
78func (c *Config) WithBackend(backend backend.Backend) *Config {
79	c.Backend = backend
80	return c
81}
82
83// WithAccessMethod sets the access control method for the configuration.
84func (c *Config) WithAccessMethod(access backend.AccessMethod) *Config {
85	c.Access = access
86	return c
87}