ComunicRTCProxy/config.go

68 lines
1.2 KiB
Go
Raw Normal View History

2020-04-09 15:32:16 +00:00
package main
2020-04-10 06:46:34 +00:00
import (
2020-04-10 07:43:14 +00:00
"fmt"
2020-04-10 06:46:34 +00:00
"io/ioutil"
"log"
2020-04-10 07:43:14 +00:00
"net/url"
2020-04-10 06:46:34 +00:00
"gopkg.in/yaml.v2"
)
// Config stores application configuration
type Config struct {
Secure bool
Hostname string
Port int
Path string
Token string
2020-04-12 15:24:49 +00:00
2021-10-17 18:01:42 +00:00
// Optional information to inject proxy reachability (can be left empty)
AccessIP string
// Optionaly restrict Ports range (can be left to 0)
PortStart int
PortEnd int
2020-04-12 15:24:49 +00:00
// Amount of time to wait before automatically
//quitting the application when the websocket
// connection was interrupted
Restart int
2020-04-10 06:46:34 +00:00
}
2020-04-10 07:43:14 +00:00
// Get the URL associated with the configuration
func (c *Config) getURL() url.URL {
// Adapt scheme
scheme := "ws"
if c.Secure {
scheme = "wss"
}
return url.URL{
Scheme: scheme,
Host: fmt.Sprintf("%s:%d", c.Hostname, c.Port),
Path: c.Path,
RawQuery: "token=" + c.Token,
}
}
2020-04-10 06:46:34 +00:00
// Load the configuration
func loadConfig(path string) Config {
// Load data
dat, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Could not read configuration: %v", err)
}
// Decode data
conf := Config{}
err = yaml.UnmarshalStrict(dat, &conf)
if err != nil {
log.Fatalf("Could not decode configuration: %v", err)
}
return conf
2020-04-09 15:32:16 +00:00
}