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 KeyPath string `env:"SOFT_SERVE_KEY_PATH"`
23 RepoPath string `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
24 InitialAdminKeys []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
25 Callbacks Callbacks
26 ErrorLog *log.Logger
27}
28
29// DefaultConfig returns a Config with the values populated with the defaults
30// or specified environment variables.
31func DefaultConfig() *Config {
32 cfg := &Config{ErrorLog: log.Default()}
33 if err := env.Parse(cfg); err != nil {
34 log.Fatalln(err)
35 }
36 if cfg.KeyPath == "" {
37 // NB: cross-platform-compatible path
38 cfg.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
39 }
40 return cfg.WithCallbacks(nil)
41}
42
43// WithCallbacks applies the given Callbacks to the configuration.
44func (c *Config) WithCallbacks(callbacks Callbacks) *Config {
45 c.Callbacks = callbacks
46 return c
47}
48
49// WithErrorLogger sets the error logger for the configuration.
50func (c *Config) WithErrorLogger(logger *log.Logger) *Config {
51 c.ErrorLog = logger
52 return c
53}