1package config
 2
 3import (
 4	"log"
 5	"path/filepath"
 6
 7	"github.com/caarlos0/env/v6"
 8)
 9
10// Callbacks provides an interface that can be used to run callbacks on different events.
11type Callbacks interface {
12	Tui(action string)
13	Push(repo string)
14	Fetch(repo string)
15}
16
17// Config is the configuration for Soft Serve.
18type Config struct {
19	BindAddr      string `env:"SOFT_SERVE_BIND_ADDRESS" envDefault:""`
20	Host          string `env:"SOFT_SERVE_HOST" envDefault:"localhost"`
21	Port          int    `env:"SOFT_SERVE_PORT" envDefault:"23231"`
22	GitPort       int    `env:"SOFT_SERVE_GIT_PORT" envDefault:"9418"`
23	GitMaxTimeout int    `env:"SOFT_SERVE_GIT_MAX_TIMEOUT" envDefault:"300"`
24	// MaxReadTimeout is the maximum time a client can take to send a request.
25	GitMaxReadTimeout int      `env:"SOFT_SERVE_GIT_MAX_READ_TIMEOUT" envDefault:"3"`
26	GitMaxConnections int      `env:"SOFT_SERVE_GIT_MAX_CONNECTIONS" envDefault:"32"`
27	KeyPath           string   `env:"SOFT_SERVE_KEY_PATH"`
28	RepoPath          string   `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
29	InitialAdminKeys  []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
30	Callbacks         Callbacks
31	ErrorLog          *log.Logger
32}
33
34// DefaultConfig returns a Config with the values populated with the defaults
35// or specified environment variables.
36func DefaultConfig() *Config {
37	cfg := &Config{ErrorLog: log.Default()}
38	if err := env.Parse(cfg); err != nil {
39		log.Fatalln(err)
40	}
41	if cfg.KeyPath == "" {
42		// NB: cross-platform-compatible path
43		cfg.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
44	}
45	return cfg.WithCallbacks(nil)
46}
47
48// WithCallbacks applies the given Callbacks to the configuration.
49func (c *Config) WithCallbacks(callbacks Callbacks) *Config {
50	c.Callbacks = callbacks
51	return c
52}
53
54// WithErrorLogger sets the error logger for the configuration.
55func (c *Config) WithErrorLogger(logger *log.Logger) *Config {
56	c.ErrorLog = logger
57	return c
58}