/
main.go
100 lines (91 loc) · 2.73 KB
/
main.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"time"
"./jlog"
"./types"
"./utils"
"gopkg.in/yaml.v2"
)
const usage = `
./dp config.yaml
`
func main() {
if len(os.Args) < 2 {
jlog.Fatal(fmt.Sprintf("ERROR: Missing required argument to the yaml config file, example usage: %s", usage))
}
// TODO: Add options for:
// -v --version CLI
// --no-version API
ex, err := os.Executable()
if err != nil {
jlog.Fatal(err.Error())
}
BINARY_SHA256, err = utils.GetFileSha256(ex)
if err != nil {
jlog.Fatal(err.Error())
}
configBytes, err := ioutil.ReadFile(os.Args[1])
if err != nil {
jlog.Fatal(fmt.Sprintf("ERROR: Reading the config file %s returned error: %s", os.Args[1], err.Error()))
}
err, yamlConfig, azureMaps := loadAndValidateYamlConfig(configBytes)
if err != nil {
jlog.Fatal(fmt.Sprintf("Error loading config file %s. %s", os.Args[1], err.Error()))
}
listenIP := "0.0.0.0"
if yamlConfig.ListenIP != "" {
listenIP = yamlConfig.ListenIP
}
listenPort := uint16(4000)
if yamlConfig.ListenPort != 0 {
listenPort = yamlConfig.ListenPort
}
apiData := &ApiData{
AzureMaps: azureMaps,
Stats: &types.JsonStats{
StartTime: time.Now().UTC().Format(time.RFC3339),
},
}
serveRestEndpoints(fmt.Sprintf("%s:%d", listenIP, listenPort), apiData)
}
func loadAndValidateYamlConfig(configBytes []byte) (err error, yamlConfig *types.YamlConfig, azureMaps *types.AzureMaps) {
yamlConfig = &types.YamlConfig{}
err = yaml.Unmarshal(configBytes, &yamlConfig)
if err != nil {
return err, nil, nil
}
azureMaps = &types.AzureMaps{
WksIdMap: make(map[string]*types.YamlWorkspace, 0),
WksNameMap: make(map[string]*types.YamlWorkspace, 0),
}
if len(yamlConfig.Workspaces) == 0 {
return errors.New("must have at least one workspace, aborting"), nil, nil
}
for _, workspaceYaml := range yamlConfig.Workspaces {
workspace := workspaceYaml
if workspace.Id == "" {
return fmt.Errorf("missing id for workspace %+v", workspace), nil, nil
} else if !regexp.MustCompile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$").MatchString(workspace.Id) {
return fmt.Errorf("%s is not a valid workspace ID", workspace.Id), nil, nil
}
if _, ok := azureMaps.WksIdMap[workspace.Id]; ok {
return fmt.Errorf("found duplicate workspace id (%s)", workspace.Id), nil, nil
}
if workspace.Secret == "" {
return fmt.Errorf("missing secret for workspace %s", workspace.Id), nil, nil
}
azureMaps.WksIdMap[workspace.Id] = &workspace
if workspace.Name != "" {
if _, ok := azureMaps.WksNameMap[workspace.Name]; ok {
return fmt.Errorf("found duplicate workspace name (%s)", workspace.Name), nil, nil
}
azureMaps.WksNameMap[workspace.Name] = &workspace
}
}
return nil, yamlConfig, azureMaps
}