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