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