/
config.go
102 lines (90 loc) · 3.03 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
package worker
import (
"log"
"net"
"net/url"
"strings"
"github.com/giongto35/cloud-game/v2/pkg/config"
"github.com/giongto35/cloud-game/v2/pkg/config/emulator"
"github.com/giongto35/cloud-game/v2/pkg/config/encoder"
"github.com/giongto35/cloud-game/v2/pkg/config/monitoring"
"github.com/giongto35/cloud-game/v2/pkg/config/shared"
webrtcConfig "github.com/giongto35/cloud-game/v2/pkg/config/webrtc"
"github.com/giongto35/cloud-game/v2/pkg/environment"
flag "github.com/spf13/pflag"
)
type Config struct {
Encoder encoder.Encoder
Emulator emulator.Emulator
Environment shared.Environment
Worker Worker
Webrtc webrtcConfig.Webrtc
}
type Worker struct {
Monitoring monitoring.Config
Network struct {
CoordinatorAddress string
Endpoint string
PingEndpoint string
PublicAddress string
Secure bool
Zone string
}
Server shared.Server
}
// allows custom config path
var configPath string
func NewConfig() (conf Config) {
_ = config.LoadConfig(&conf, configPath)
conf.expandSpecialTags()
return
}
// ParseFlags updates config values from passed runtime flags.
// Define own flags with default value set to the current config param.
// Don't forget to call flag.Parse().
func (c *Config) ParseFlags() {
c.Environment.WithFlags()
c.Worker.Server.WithFlags()
flag.IntVar(&c.Worker.Monitoring.Port, "monitoring.port", c.Worker.Monitoring.Port, "Monitoring server port")
flag.StringVar(&c.Worker.Network.CoordinatorAddress, "coordinatorhost", c.Worker.Network.CoordinatorAddress, "Worker URL to connect")
flag.StringVar(&c.Worker.Network.Zone, "zone", c.Worker.Network.Zone, "Worker network zone (us, eu, etc.)")
flag.StringVarP(&configPath, "conf", "c", configPath, "Set custom configuration file path")
flag.Parse()
}
// expandSpecialTags replaces all the special tags in the config.
func (c *Config) expandSpecialTags() {
tag := "{user}"
for _, dir := range []*string{&c.Emulator.Storage, &c.Emulator.Libretro.Cores.Repo.ExtLock} {
if *dir == "" || !strings.Contains(*dir, tag) {
continue
}
userHomeDir, err := environment.GetUserHome()
if err != nil {
log.Fatalln("couldn't read user home directory", err)
}
*dir = strings.Replace(*dir, tag, userHomeDir, -1)
}
}
// GetAddr returns defined in the config server address.
func (w *Worker) GetAddr() string { return w.Server.GetAddr() }
// GetPingAddr returns exposed to clients server ping endpoint address.
func (w *Worker) GetPingAddr(address string) string {
_, srcPort, _ := net.SplitHostPort(w.GetAddr())
dstHost, _, _ := net.SplitHostPort(address)
address = net.JoinHostPort(dstHost, srcPort)
if w.Network.PublicAddress != "" {
address = w.Network.PublicAddress
if w.Network.Zone != "" {
address = w.Network.Zone + "." + address
}
port := srcPort
if port != "" && port != "80" && port != "443" && port != "0" {
address += ":" + port
}
}
pingURL := url.URL{Scheme: "http", Host: address, Path: w.Network.PingEndpoint}
if w.Server.Https {
pingURL.Scheme = "https"
}
return pingURL.String()
}