config.go

  1package config
  2
  3import (
  4	"os"
  5	"path/filepath"
  6
  7	"github.com/caarlos0/env/v7"
  8	"github.com/charmbracelet/log"
  9	"github.com/charmbracelet/soft-serve/server/backend"
 10	"gopkg.in/yaml.v3"
 11)
 12
 13// SSHConfig is the configuration for the SSH server.
 14type SSHConfig struct {
 15	// ListenAddr is the address on which the SSH server will listen.
 16	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 17
 18	// PublicURL is the public URL of the SSH server.
 19	PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
 20
 21	// KeyPath is the path to the SSH server's private key.
 22	KeyPath string `env:"KEY_PATH" yaml:"key_path"`
 23
 24	// InternalKeyPath is the path to the SSH server's internal private key.
 25	InternalKeyPath string `env:"INTERNAL_KEY_PATH" yaml:"internal_key_path"`
 26
 27	// MaxTimeout is the maximum number of seconds a connection can take.
 28	MaxTimeout int `env:"MAX_TIMEOUT" yaml:"max_timeout`
 29
 30	// IdleTimeout is the number of seconds a connection can be idle before it is closed.
 31	IdleTimeout int `env:"IDLE_TIMEOUT" yaml:"idle_timeout"`
 32}
 33
 34// GitConfig is the Git daemon configuration for the server.
 35type GitConfig struct {
 36	// ListenAddr is the address on which the Git daemon will listen.
 37	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 38
 39	// MaxTimeout is the maximum number of seconds a connection can take.
 40	MaxTimeout int `env:"MAX_TIMEOUT" yaml:"max_timeout"`
 41
 42	// IdleTimeout is the number of seconds a connection can be idle before it is closed.
 43	IdleTimeout int `env:"IDLE_TIMEOUT" yaml:"idle_timeout"`
 44
 45	// MaxConnections is the maximum number of concurrent connections.
 46	MaxConnections int `env:"MAX_CONNECTIONS" yaml:"max_connections"`
 47}
 48
 49// HTTPConfig is the HTTP configuration for the server.
 50type HTTPConfig struct {
 51	// ListenAddr is the address on which the HTTP server will listen.
 52	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 53
 54	// TLSKeyPath is the path to the TLS private key.
 55	TLSKeyPath string `env:"TLS_KEY_PATH" yaml:"tls_key_path"`
 56
 57	// TLSCertPath is the path to the TLS certificate.
 58	TLSCertPath string `env:"TLS_CERT_PATH" yaml:"tls_cert_path"`
 59
 60	// PublicURL is the public URL of the HTTP server.
 61	PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
 62}
 63
 64// StatsConfig is the configuration for the stats server.
 65type StatsConfig struct {
 66	// ListenAddr is the address on which the stats server will listen.
 67	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 68}
 69
 70// Config is the configuration for Soft Serve.
 71type Config struct {
 72	// Name is the name of the server.
 73	Name string `env:"NAME" yaml:"name"`
 74
 75	// SSH is the configuration for the SSH server.
 76	SSH SSHConfig `envPrefix:"SSH_" yaml:"ssh"`
 77
 78	// Git is the configuration for the Git daemon.
 79	Git GitConfig `envPrefix:"GIT_" yaml:"git"`
 80
 81	// HTTP is the configuration for the HTTP server.
 82	HTTP HTTPConfig `envPrefix:"HTTP_" yaml:"http"`
 83
 84	// Stats is the configuration for the stats server.
 85	Stats StatsConfig `envPrefix:"STATS_" yaml:"stats"`
 86
 87	// InitialAdminKeys is a list of public keys that will be added to the list of admins.
 88	InitialAdminKeys []string `env:"INITIAL_ADMIN_KEY" envSeparator:"\n" yaml:"initial_admin_keys"`
 89
 90	// DataPath is the path to the directory where Soft Serve will store its data.
 91	DataPath string `env:"DATA_PATH" envDefault:"data" yaml:"-"`
 92
 93	// Backend is the Git backend to use.
 94	Backend backend.Backend `yaml:"-"`
 95}
 96
 97// ParseConfig parses the configuration from the given file.
 98func ParseConfig(path string) (*Config, error) {
 99	cfg := &Config{}
100	f, err := os.Open(path)
101	if err != nil {
102		return nil, err
103	}
104	defer f.Close() // nolint: errcheck
105	if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
106		return nil, err
107	}
108
109	return cfg, nil
110}
111
112// WriteConfig writes the configuration to the given file.
113func WriteConfig(path string, cfg *Config) error {
114	return os.WriteFile(path, []byte(newConfigFile(cfg)), 0o600) // nolint: errcheck
115}
116
117// DefaultConfig returns a Config with the values populated with the defaults
118// or specified environment variables.
119func DefaultConfig() *Config {
120	dataPath := os.Getenv("SOFT_SERVE_DATA_PATH")
121	if dataPath == "" {
122		dataPath = "data"
123	}
124
125	cfg := &Config{
126		Name:     "Soft Serve",
127		DataPath: dataPath,
128		SSH: SSHConfig{
129			ListenAddr:      ":23231",
130			PublicURL:       "ssh://localhost:23231",
131			KeyPath:         filepath.Join("ssh", "soft_serve_host"),
132			InternalKeyPath: filepath.Join("ssh", "soft_serve_internal"),
133			MaxTimeout:      0,
134			IdleTimeout:     120,
135		},
136		Git: GitConfig{
137			ListenAddr:     ":9418",
138			MaxTimeout:     0,
139			IdleTimeout:    3,
140			MaxConnections: 32,
141		},
142		HTTP: HTTPConfig{
143			ListenAddr: ":8080",
144			PublicURL:  "http://localhost:8080",
145		},
146		Stats: StatsConfig{
147			ListenAddr: ":8081",
148		},
149	}
150	cp := filepath.Join(cfg.DataPath, "config.yaml")
151	f, err := os.Open(cp)
152	if err == nil {
153		defer f.Close() // nolint: errcheck
154		if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
155			log.Error("failed to decode config", "err", err)
156		}
157	} else {
158		defer func() {
159			os.WriteFile(cp, []byte(newConfigFile(cfg)), 0o600) // nolint: errcheck
160		}()
161	}
162
163	if err := env.Parse(cfg, env.Options{
164		Prefix: "SOFT_SERVE_",
165	}); err != nil {
166		log.Fatal(err)
167	}
168
169	return cfg
170}
171
172// WithBackend sets the backend for the configuration.
173func (c *Config) WithBackend(backend backend.Backend) *Config {
174	c.Backend = backend
175	return c
176}