-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
152 lines (124 loc) · 3.22 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package service
import (
"fmt"
"net/url"
"time"
"github.com/mattermost/rtcd/service/auth"
"github.com/mattermost/rtcd/logger"
"github.com/mattermost/rtcd/service/api"
"github.com/mattermost/rtcd/service/rtc"
)
type SecurityConfig struct {
// Whether or not to enable admin API access.
EnableAdmin bool `toml:"enable_admin"`
// The secret key used to authenticate admin requests.
AdminSecretKey string `toml:"admin_secret_key"`
// Whether or not to allow clients to self-register.
AllowSelfRegistration bool `toml:"allow_self_registration"`
SessionCache auth.SessionCacheConfig `toml:"session_cache"`
}
func (c SecurityConfig) IsValid() error {
if !c.EnableAdmin {
return nil
}
if c.AdminSecretKey == "" {
return fmt.Errorf("invalid AdminSecretKey value: should not be empty")
}
return nil
}
type APIConfig struct {
HTTP api.Config `toml:"http"`
Security SecurityConfig `toml:"security"`
}
type Config struct {
API APIConfig
RTC rtc.ServerConfig
Store StoreConfig
Logger logger.Config
}
func (c APIConfig) IsValid() error {
if err := c.Security.IsValid(); err != nil {
return fmt.Errorf("failed to validate admin config: %w", err)
}
if err := c.HTTP.IsValid(); err != nil {
return fmt.Errorf("failed to validate http config: %w", err)
}
return nil
}
func (c Config) IsValid() error {
if err := c.API.IsValid(); err != nil {
return err
}
if err := c.RTC.IsValid(); err != nil {
return err
}
if err := c.Store.IsValid(); err != nil {
return err
}
return c.Logger.IsValid()
}
func (c *Config) SetDefaults() {
c.API.HTTP.ListenAddress = ":8045"
c.API.Security.SessionCache.ExpirationMinutes = 1440
c.RTC.ICEPortUDP = 8443
c.RTC.ICEPortTCP = 8443
c.RTC.TURNConfig.CredentialsExpirationMinutes = 1440
c.Store.DataSource = "/tmp/rtcd_db"
c.Logger.EnableConsole = true
c.Logger.ConsoleJSON = false
c.Logger.ConsoleLevel = "INFO"
c.Logger.EnableFile = true
c.Logger.FileJSON = true
c.Logger.FileLocation = "rtcd.log"
c.Logger.FileLevel = "DEBUG"
c.Logger.EnableColor = false
}
type StoreConfig struct {
DataSource string `toml:"data_source"`
}
func (c StoreConfig) IsValid() error {
if c.DataSource == "" {
return fmt.Errorf("invalid DataSource value: should not be empty")
}
return nil
}
type ClientConfig struct {
httpURL string
wsURL string
ClientID string
AuthKey string
URL string
ReconnectInterval time.Duration
}
func (c *ClientConfig) Parse() error {
if c.URL == "" {
return fmt.Errorf("invalid URL value: should not be empty")
}
u, err := url.Parse(c.URL)
if err != nil {
return fmt.Errorf("failed to parse url: %w", err)
}
if u.Host == "" {
return fmt.Errorf("invalid url host: should not be empty")
}
switch u.Scheme {
case "http":
c.httpURL = c.URL
u.Scheme = "ws"
u.Path = "/ws"
c.wsURL = u.String()
case "https":
c.httpURL = c.URL
u.Scheme = "wss"
u.Path = "/ws"
c.wsURL = u.String()
default:
return fmt.Errorf("invalid url scheme: %q is not valid", u.Scheme)
}
if c.ReconnectInterval <= 0 {
c.ReconnectInterval = defaultReconnectInterval
}
return nil
}