|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | + "maunium.net/go/mautrix/id" |
| 11 | +) |
| 12 | + |
| 13 | +// The mandatory SFU configuration. |
| 14 | +type Config struct { |
| 15 | + // The Matrix ID (MXID) of the SFU. |
| 16 | + UserID id.UserID |
| 17 | + // The ULR of the homeserver that SFU talks to. |
| 18 | + HomeserverURL string |
| 19 | + // The access token for the Matrix SDK. |
| 20 | + AccessToken string |
| 21 | + // Keep-alive timeout for WebRTC connections. If no keep-alive has been received |
| 22 | + // from the client for this duration, the connection is considered dead. |
| 23 | + KeepAliveTimeout int |
| 24 | +} |
| 25 | + |
| 26 | +// Tries to load a config from the `CONFIG` environment variable. |
| 27 | +// If the environment variable is not set, tries to load a config from the |
| 28 | +// provided path to the config file (YAML). Returns an error if the config could |
| 29 | +// not be loaded. |
| 30 | +func loadConfig(path string) (*Config, error) { |
| 31 | + config, err := loadConfigFromEnv() |
| 32 | + if err != nil { |
| 33 | + if !errors.Is(err, ErrNoConfigEnvVar) { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + return loadConfigFromPath(path) |
| 38 | + } |
| 39 | + |
| 40 | + return config, nil |
| 41 | +} |
| 42 | + |
| 43 | +// ErrNoConfigEnvVar is returned when the CONFIG environment variable is not set. |
| 44 | +var ErrNoConfigEnvVar = errors.New("environment variable not set or invalid") |
| 45 | + |
| 46 | +// Tries to load the config from environment variable (`CONFIG`). |
| 47 | +// Returns an error if not all environment variables are set. |
| 48 | +func loadConfigFromEnv() (*Config, error) { |
| 49 | + configEnv := os.Getenv("CONFIG") |
| 50 | + if configEnv == "" { |
| 51 | + return nil, ErrNoConfigEnvVar |
| 52 | + } |
| 53 | + |
| 54 | + return loadConfigFromString(configEnv) |
| 55 | +} |
| 56 | + |
| 57 | +// Tries to load a config from the provided path. |
| 58 | +func loadConfigFromPath(path string) (*Config, error) { |
| 59 | + logrus.WithField("path", path).Info("loading config") |
| 60 | + |
| 61 | + file, err := os.ReadFile(path) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("failed to read file: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + return loadConfigFromString(string(file)) |
| 67 | +} |
| 68 | + |
| 69 | +// Load config from the provided string. |
| 70 | +// Returns an error if the string is not a valid YAML. |
| 71 | +func loadConfigFromString(configString string) (*Config, error) { |
| 72 | + logrus.Info("loading config from string") |
| 73 | + |
| 74 | + var config Config |
| 75 | + if err := yaml.Unmarshal([]byte(configString), &config); err != nil { |
| 76 | + return nil, fmt.Errorf("failed to unmarshal YAML file: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return &config, nil |
| 80 | +} |
0 commit comments