config.go

 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}
26
27// DefaultConfig returns a Config with the values populated with the defaults
28// or specified environment variables.
29func DefaultConfig() *Config {
30	var cfg Config
31	if err := env.Parse(&cfg); err != nil {
32		log.Fatalln(err)
33	}
34	if cfg.KeyPath == "" {
35		// NB: cross-platform-compatible path
36		cfg.KeyPath = filepath.Join(".ssh", "soft_serve_server_ed25519")
37	}
38	return cfg.WithCallbacks(nil)
39}
40
41// WithCallbacks applies the given Callbacks to the configuration.
42func (c *Config) WithCallbacks(callbacks Callbacks) *Config {
43	c.Callbacks = callbacks
44	return c
45}