1package config
2
3import (
4 glog "log"
5 "path/filepath"
6
7 "github.com/caarlos0/env/v6"
8 "github.com/charmbracelet/log"
9)
10
11// Callbacks provides an interface that can be used to run callbacks on different events.
12type Callbacks interface {
13 Tui(action string)
14 Push(repo string)
15 Fetch(repo string)
16}
17
18// Config is the configuration for Soft Serve.
19type Config struct {
20 BindAddr string `env:"SOFT_SERVE_BIND_ADDRESS" envDefault:""`
21 Host string `env:"SOFT_SERVE_HOST" envDefault:"localhost"`
22 Port int `env:"SOFT_SERVE_PORT" envDefault:"23231"`
23 KeyPath string `env:"SOFT_SERVE_KEY_PATH"`
24 RepoPath string `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
25 Debug bool `env:"SOFT_SERVE_DEBUG" envDefault:"false"`
26 InitialAdminKeys []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
27 Callbacks Callbacks
28 ErrorLog *glog.Logger
29}
30
31// DefaultConfig returns a Config with the values populated with the defaults
32// or specified environment variables.
33func DefaultConfig() *Config {
34 cfg := &Config{ErrorLog: log.StandardLog(log.StandardLogOptions{ForceLevel: log.ErrorLevel})}
35 if err := env.Parse(cfg); err != nil {
36 log.Fatal(err)
37 }
38 if cfg.Debug {
39 log.SetLevel(log.DebugLevel)
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 *glog.Logger) *Config {
56 c.ErrorLog = logger
57 return c
58}