-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
168 lines (133 loc) · 3.39 KB
/
server.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"github.com/nettica-com/nettica-admin/model"
log "github.com/sirupsen/logrus"
)
var (
Servers = make(map[string]*Server)
ServersMutex = &sync.Mutex{}
)
func NewServer(name string, config model.Message) *Server {
ServersMutex.Lock()
defer ServersMutex.Unlock()
server := &Server{
Name: CleanupName(name),
Path: GetServerPath(name),
Config: config,
Running: make(chan bool),
Shutdown: false,
}
Servers[server.Path] = server
return server
}
func LoadServers() error {
// If we already have servers (from environment variables), don't load from directory
if len(Servers) > 0 {
return nil
}
ServersMutex.Lock()
defer ServersMutex.Unlock()
// Find all the .json files in the config directory (/etc/nettica)
dir := GetDataPath()
files, err := os.ReadDir(dir)
if err != nil {
log.Errorf("Failed to read config directory: %v", err)
return err
}
for _, file := range files {
if file.IsDir() {
continue
}
if filepath.Ext(file.Name()) == ".json" {
if file.Name() == "nettica.json" {
continue
}
if file.Name() == "keys.json" {
continue
}
if strings.HasSuffix(file.Name(), "-service-host.json") {
continue
}
path := filepath.Join(dir, file.Name())
data, err := os.ReadFile(path)
if err != nil {
log.Printf("Failed to read file %s: %v", path, err)
continue
}
var server Server
if err := json.Unmarshal(data, &server.Config); err != nil {
log.Printf("Failed to unmarshal JSON from file %s: %v", path, err)
continue
}
if server.Config.Device != nil && server.Config.Device.Server != "" {
server.Name = server.Config.Device.Server
} else {
server.Name = strings.TrimSuffix(file.Name(), ".json")
}
if server.Config.Device != nil {
switch server.Config.Device.Logging {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.FatalLevel)
}
}
server.Path = path
server.Body = data
server.Running = make(chan bool)
Servers[path] = &server
}
}
return nil
}
func SaveServer(server *Server) {
ServersMutex.Lock()
defer ServersMutex.Unlock()
path := server.Path
// Although we have a Body field, the JSON is
// the source of truth for the config.
data, err := json.Marshal(server.Config)
if err != nil {
log.Printf("Failed to marshal JSON: %v", err)
return
}
if err := os.WriteFile(path, data, 0644); err != nil {
log.Printf("Failed to write file %s: %v", path, err)
}
server.Body = data
}
func CleanupName(name string) string {
// remove the https:// or http:// from the name if it exists
if strings.ToLower(name[:8]) == "https://" {
name = name[8:]
} else if strings.ToLower(name[:7]) == "http://" {
name = name[7:]
}
name = strings.ToLower(name)
return Sanitize(name)
}
func GetServerPath(name string) string {
name = CleanupName(name)
return filepath.Join(GetDataPath(), name+".json")
}
func RemoveServer(server *Server) {
ServersMutex.Lock()
defer ServersMutex.Unlock()
log.Infof("Removing server %s (%s)", server.Name, server.Path)
err := os.Remove(server.Path)
if err != nil {
log.Errorf("Failed to remove file %s: %v", server.Path, err)
}
delete(Servers, server.Path)
server.Shutdown = true
log.Info("Server removed")
}