Skip to content

Commit 9d4e6df

Browse files
config: make config more flexible
Can now be passed as a `CONFIG` environment variable in a YAML format.
1 parent fca022b commit 9d4e6df

File tree

4 files changed

+70
-77
lines changed

4 files changed

+70
-77
lines changed

docker-compose.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ services:
66
network_mode: host
77
restart: always
88
environment:
9-
USER_ID: "@sfu:shadowfax"
10-
HOMESERVER_URL: "http://localhost:8008"
11-
ACCESS_TOKEN: "..."
12-
TIMEOUT: 30
9+
CONFIG: |
10+
homeserverurl: "http://localhost:8008"
11+
userid: "@sfu:shadowfax"
12+
accesstoken: "..."
13+
timeout: 30

src/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func (c *Call) SendDataChannelMessage(msg event.SFUMessage) {
513513
}
514514

515515
func (c *Call) CheckKeepAliveTimestamp() {
516-
timeout := time.Second * time.Duration(config.Timeout)
516+
timeout := time.Second * time.Duration(config.KeepAliveTimeout)
517517
for range time.Tick(timeout) {
518518
if c.lastKeepAliveTimestamp.Add(timeout).Before(time.Now()) {
519519
if c.PeerConnection.ConnectionState() != webrtc.PeerConnectionStateClosed {

src/config.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// Load config from the provided string.
27+
// Returns an error if the string is not a valid YAML.
28+
func loadConfigFromString(configString string) (*Config, error) {
29+
logrus.Info("loading config from string")
30+
31+
var config Config
32+
if err := yaml.Unmarshal([]byte(configString), &config); err != nil {
33+
return nil, fmt.Errorf("failed to unmarshal YAML file: %w", err)
34+
}
35+
36+
return &config, nil
37+
}
38+
39+
// ErrNoConfigEnvVar is returned when the CONFIG environment variable is not set.
40+
var ErrNoConfigEnvVar = errors.New("environment variable not set or invalid")
41+
42+
// Tries to load the config from environment variables.
43+
// Returns an error if not all environment variables are set.
44+
func loadConfigFromEnv() (*Config, error) {
45+
configEnv := os.Getenv("CONFIG")
46+
if configEnv == "" {
47+
return nil, ErrNoConfigEnvVar
48+
}
49+
50+
return loadConfigFromString(configEnv)
51+
}
52+
53+
// Tries to load a config from the provided path.
54+
func loadConfigFromPath(path string) (*Config, error) {
55+
logrus.WithField("path", path).Info("loading config")
56+
57+
file, err := os.ReadFile(path)
58+
if err != nil {
59+
return nil, fmt.Errorf("failed to read file: %w", err)
60+
}
61+
62+
return loadConfigFromString(string(file))
63+
}

src/main.go

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,16 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"errors"
2120
"flag"
22-
"fmt"
2321
"os"
2422
"os/signal"
2523
"runtime"
2624
"runtime/pprof"
27-
"strconv"
28-
"strings"
2925
"syscall"
3026

3127
"github.com/sirupsen/logrus"
32-
yaml "gopkg.in/yaml.v3"
33-
"maunium.net/go/mautrix/id"
3428
)
3529

36-
type Config struct {
37-
UserID id.UserID
38-
HomeserverURL string
39-
AccessToken string
40-
Timeout int
41-
}
42-
4330
var config *Config
4431

4532
var logTime = flag.Bool("logTime", false, "whether or not to print time and date in logs")
@@ -97,64 +84,6 @@ func initLogging(logTime *bool) {
9784
logrus.SetFormatter(formatter)
9885
}
9986

100-
var ErrEnvVarNotSet = errors.New("environment variable not set or invalid")
101-
102-
func EnvVarNotSetError(variable string) error {
103-
return fmt.Errorf("%s: %w", variable, ErrEnvVarNotSet)
104-
}
105-
106-
// / Tries to load the config from environment variables.
107-
// / Returns an error if not all environment variables are set.
108-
func loadConfigFromEnv() (*Config, error) {
109-
userID := strings.TrimSpace(os.Getenv("USER_ID"))
110-
if userID == "" {
111-
return nil, EnvVarNotSetError("USER_ID")
112-
}
113-
114-
homeserverURL := strings.TrimSpace(os.Getenv("HOMESERVER_URL"))
115-
if homeserverURL == "" {
116-
return nil, EnvVarNotSetError("HOMESERVER_URL")
117-
}
118-
119-
accessToken := strings.TrimSpace(os.Getenv("ACCESS_TOKEN"))
120-
if accessToken == "" {
121-
return nil, EnvVarNotSetError("ACCESS_TOKEN")
122-
}
123-
124-
timeoutStr := strings.TrimSpace(os.Getenv("TIMEOUT"))
125-
if timeoutStr == "" {
126-
return nil, EnvVarNotSetError("TIMEOUT")
127-
}
128-
129-
timeout, err := strconv.Atoi(timeoutStr)
130-
if err != nil {
131-
return nil, EnvVarNotSetError("TIMEOUT Format")
132-
}
133-
134-
return &Config{
135-
UserID: id.UserID(userID),
136-
HomeserverURL: homeserverURL,
137-
AccessToken: accessToken,
138-
Timeout: timeout,
139-
}, nil
140-
}
141-
142-
func loadConfig(configFilePath string) (*Config, error) {
143-
logrus.WithField("path", configFilePath).Info("loading config")
144-
145-
file, err := os.ReadFile(configFilePath)
146-
if err != nil {
147-
return nil, fmt.Errorf("failed to read file: %w", err)
148-
}
149-
var config Config
150-
151-
if err := yaml.Unmarshal(file, &config); err != nil {
152-
return nil, fmt.Errorf("failed to unmarshal YAML file: %w", err)
153-
}
154-
155-
return &config, nil
156-
}
157-
15887
func killListener(c chan os.Signal, beforeExit []func()) {
15988
<-c
16089

@@ -192,7 +121,7 @@ func main() {
192121
if err != nil {
193122
logrus.WithError(err).Info("failed to load config from environment variables, trying to load from file")
194123

195-
if config, err = loadConfig(*configFilePath); err != nil {
124+
if config, err = loadConfigFromPath(*configFilePath); err != nil {
196125
logrus.WithError(err).Fatal("failed to load config file")
197126
}
198127
}

0 commit comments

Comments
 (0)