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// DefaultConfig returns a Config with the values populated with the defaults
113// or specified environment variables.
114func DefaultConfig() *Config {
115	dataPath := os.Getenv("SOFT_SERVE_DATA_PATH")
116	if dataPath == "" {
117		dataPath = "data"
118	}
119
120	cfg := &Config{
121		Name:     "Soft Serve",
122		DataPath: dataPath,
123		SSH: SSHConfig{
124			ListenAddr:      ":23231",
125			PublicURL:       "ssh://localhost:23231",
126			KeyPath:         filepath.Join("ssh", "soft_serve"),
127			InternalKeyPath: filepath.Join("ssh", "soft_serve_internal"),
128			MaxTimeout:      0,
129			IdleTimeout:     120,
130		},
131		Git: GitConfig{
132			ListenAddr:     ":9418",
133			MaxTimeout:     0,
134			IdleTimeout:    3,
135			MaxConnections: 32,
136		},
137		HTTP: HTTPConfig{
138			ListenAddr: ":8080",
139			PublicURL:  "http://localhost:8080",
140		},
141		Stats: StatsConfig{
142			ListenAddr: ":8081",
143		},
144	}
145	cp := filepath.Join(cfg.DataPath, "config.yaml")
146	f, err := os.Open(cp)
147	if err == nil {
148		defer f.Close() // nolint: errcheck
149		if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
150			log.Error("failed to decode config", "err", err)
151		}
152	} else {
153		defer func() {
154			os.WriteFile(cp, []byte(newConfigFile(cfg)), 0o600) // nolint: errcheck
155		}()
156	}
157
158	if err := env.Parse(cfg, env.Options{
159		Prefix: "SOFT_SERVE_",
160	}); err != nil {
161		log.Fatal(err)
162	}
163
164	return cfg
165}
166
167// WithBackend sets the backend for the configuration.
168func (c *Config) WithBackend(backend backend.Backend) *Config {
169	c.Backend = backend
170	return c
171}