1package config
2
3import (
4 "crypto/tls"
5 "log"
6 "path/filepath"
7
8 "github.com/caarlos0/env/v6"
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 SSHPort int `env:"SOFT_SERVE_SSH_PORT" envDefault:"23231"`
22 HTTPPort int `env:"SOFT_SERVE_HTTP_PORT" envDefault:"23232"`
23 KeyPath string `env:"SOFT_SERVE_KEY_PATH"`
24 RepoPath string `env:"SOFT_SERVE_REPO_PATH" envDefault:".repos"`
25 InitialAdminKeys []string `env:"SOFT_SERVE_INITIAL_ADMIN_KEY" envSeparator:"\n"`
26 Callbacks Callbacks
27 ErrorLog *log.Logger
28 TLSConfig *tls.Config
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.Default()}
35 if err := env.Parse(cfg); err != nil {
36 log.Fatalln(err)
37 }
38 if cfg.KeyPath == "" {
39 // NB: cross-platform-compatible path
40 cfg.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
41 }
42 return cfg.WithCallbacks(nil)
43}
44
45// WithCallbacks applies the given Callbacks to the configuration.
46func (c *Config) WithCallbacks(callbacks Callbacks) *Config {
47 c.Callbacks = callbacks
48 return c
49}
50
51// WithErrorLogger sets the error logger for the configuration.
52func (c *Config) WithErrorLogger(logger *log.Logger) *Config {
53 c.ErrorLog = logger
54 return c
55}
56
57func (c *Config) WithTLSConfig(t *tls.Config) *Config {
58 c.TLSConfig = t
59 return c
60}