-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
286 lines (202 loc) · 8.76 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Global configuration Parameters
package main
import (
"encoding/json"
"fmt"
"os"
"time"
)
// TTNMQTTMode defines the MQTT vs mq-over-HTTP operating mode.
// As of 2017-02 we're now operating in "HTTP Integration" TTN mode, largely
// so that we can serve incoming requests through our load balancer rather than
// having a single server that pulls MQTT requests.
const TTNMQTTMode = false
// TTN service info
const ttnAppID string = "ttserve"
const ttnServer string = "tcp://eu.thethings.network:1883"
const ttnTopic string = "+/devices/+/up"
// Google Sheets ID of published (File/Publish to Web) doc, as CSV
const sheetsSolarcastTracker = "https://docs.google.com/spreadsheets/d/1lvB_0XFFSwON4PQFoC8NdDv6INJTCw2f_KBZuMTZhZA/export?format=csv"
// Safecast service info
// SafecastV1UploadDomainDev is developer API server
const SafecastV1UploadDomainDev = "dev.safecast.cc"
// SafecastV1UploadDomain is production API server
const SafecastV1UploadDomain = "api.safecast.cc"
// SafecastV1UploadPattern is the pattern of the URL for both
const SafecastV1UploadPattern = "https://%s/measurements.json?%s"
// SafecastV1SolarcastUploadURL is the url where to upload solarcast data
const SafecastV1SolarcastUploadURL = "https://api.safecast.cc/measurements.json?api_key=z3sHhgousVDDrCVXhzMT"
// SafecastUploadURLs are the places we should upload V2 measurements
var SafecastUploadURLs = [...]string{
"http://ingest.safecast.cc/v1/measurements",
}
// Slack service info
// SlackOpsNone means "no ops channel specified"
const SlackOpsNone = -1
// SlackOpsSafecast means the #ops channel in the safecast slack
const SlackOpsSafecast = 0
// SlackCommandTime is the time a slack command was issued
var SlackCommandTime time.Time
// SlackCommandSource means the ID of the channel that originated a command
var SlackCommandSource = SlackOpsNone
// SlackMsgUnsolicitedAll means to send a message to all safecast channels
const SlackMsgUnsolicitedAll = 0
// SlackMsgUnsolicitedOps means to reply just to the safecast ops channel
const SlackMsgUnsolicitedOps = 1
// SlackMsgReply means to reply to the SlackCommandSource
const SlackMsgReply = 2
// ServiceConfig is our configuration, read out of a file for security reasons
var ServiceConfig TTServeConfig
// Paths for the file system shared among all TTSERVE instances
// TTConfigPath (here for golint)
const TTConfigPath = "/config/config.json"
// TTDeviceLogPath (here for golint)
const TTDeviceLogPath = "/device-log"
// TTFilePath (here for golint)
const TTFilePath = "/files"
// TTDeviceStampPath (here for golint)
const TTDeviceStampPath = "/device-stamp"
// TTDeviceStatusPath (here for golint)
const TTDeviceStatusPath = "/device-status"
// TTQueryPath (here for golint)
const TTQueryPath = "/query"
// TTServerLogPath (here for golint)
const TTServerLogPath = "/server-log"
// TTServerStatusPath (here for golint)
const TTServerStatusPath = "/server-status"
// TTGatewayStatusPath (here for golint)
const TTGatewayStatusPath = "/gateway-status"
// TTServerControlPath (here for golint)
const TTServerControlPath = "/control"
// TTServerBuildPath (here for golint)
const TTServerBuildPath = "/build"
// TTServerSlackCommandControlFile (here for golint)
const TTServerSlackCommandControlFile = "slack_command.txt"
// TTServerRestartAllControlFile (here for golint)
const TTServerRestartAllControlFile = "restart_all.txt"
// TTServeInstanceID is the AWS instance ID for the current instance
var TTServeInstanceID = ""
// TTSERVE's address and ports
// TTServerHTTPAddress (here for golint)
const TTServerHTTPAddress = "tt.safecast.cc"
// TTServerUDPAddress (here for golint)
const TTServerUDPAddress = "tt-udp.safecast.cc"
// TTServerUDPAddressIPv4 (here for golint)
var TTServerUDPAddressIPv4 = "" // Looked up dynamically
// TTServerHTTPPort (here for golint)
const TTServerHTTPPort string = ":80"
// TTServerHTTPPortAlternate (here for golint)
const TTServerHTTPPortAlternate string = ":8080"
// TTServerUDPPort (here for golint)
const TTServerUDPPort string = ":8081"
// TTServerTCPPort (here for golint)
const TTServerTCPPort string = ":8082"
// TTServerTopicID (here for golint) [DEPRECATED]
const TTServerTopicID string = "/id/"
// TTServerTopicDashboard (here for golint)
const TTServerTopicDashboard string = "/dashboard/"
// TTServerTopicProfile (here for golint)
const TTServerTopicProfile string = "/profile/"
// TTServerTopicMap (here for golint)
const TTServerTopicMap string = "/map/"
// TTServerTopicDevices (here for golint)
const TTServerTopicDevices string = "/devices"
// TTServerTopicSend (here for golint)
const TTServerTopicSend string = "/send"
// TTServerTopicRoot1 (here for golint)
const TTServerTopicRoot1 string = "/index.html"
// TTServerTopicRoot2 (here for golint)
const TTServerTopicRoot2 string = "/index.htm"
// TTServerTopicDeviceLog (here for golint)
const TTServerTopicDeviceLog string = "/device-log/"
// TTServerTopicFile (here for golint)
const TTServerTopicFile string = "/file/"
// TTServerTopicDeviceCheck (here for golint)
const TTServerTopicDeviceCheck string = "/check/"
// TTServerTopicDeviceStatus (here for golint)
const TTServerTopicDeviceStatus string = "/device/"
// TTServerTopicServerLog (here for golint)
const TTServerTopicServerLog string = "/server-log/"
// TTServerTopicServerStatus (here for golint)
const TTServerTopicServerStatus string = "/server/"
// TTServerTopicGatewayUpdate (here for golint)
const TTServerTopicGatewayUpdate string = "/gateway"
// TTServerTopicGatewayStatus (here for golint)
const TTServerTopicGatewayStatus string = "/gateway/"
// TTServerTopicGithub (here for golint)
const TTServerTopicGithub string = "/github"
// TTServerTopicSlack (here for golint)
const TTServerTopicSlack string = "/slack"
// TTServerTopicTTN (here for golint)
const TTServerTopicTTN string = "/ttn"
// TTServerTopicRedirect1 (here for golint)
const TTServerTopicRedirect1 string = "/scripts/"
// TTServerTopicRedirect2 (here for golint)
const TTServerTopicRedirect2 string = "/"
// TTServerTopicNote (here for golint)
const TTServerTopicNote string = "/note"
// TTServerTopicNoteTest (here for golint)
const TTServerTopicNoteTest string = "/notetest"
// ThisServerAddressIPv4 is looked up dynamically
var ThisServerAddressIPv4 = ""
// Dynamically computed state about this particular server
// ThisServerServesUDP (here for golint)
var ThisServerServesUDP = false
// ThisServerServesMQTT (here for golint)
var ThisServerServesMQTT = false
// ThisServerIsMonitor (here for golint)
var ThisServerIsMonitor = false
// ThisServerBootTime (here for golint)
var ThisServerBootTime time.Time
// AllServersSlackRestartRequestTime (here for golint)
var AllServersSlackRestartRequestTime time.Time
// AllServersGithubRestartRequestTime (here for golint)
var AllServersGithubRestartRequestTime time.Time
// BuffFormatPBArray is the payload buffer format
const BuffFormatPBArray byte = 0
// Log-related
const logDateFormat string = "2006-01-02 15:04:05"
// TTServeCounts is our global statistics structure
type TTServeCounts struct {
Restarts uint32 `json:"restarts,omitempty"`
UDP uint32 `json:"received_device_udp,omitempty"`
TCP uint32 `json:"received_device_tcp,omitempty"`
HTTP uint32 `json:"received_all_http,omitempty"`
HTTPSlack uint32 `json:"received_slack_http,omitempty"`
HTTPGithub uint32 `json:"received_github_http,omitempty"`
HTTPGUpdate uint32 `json:"received_gateway_update_http,omitempty"`
HTTPDevice uint32 `json:"received_device_msg_http,omitempty"`
HTTPGateway uint32 `json:"received_gateway_msg_http,omitempty"`
HTTPRelay uint32 `json:"received_udp_to_http,omitempty"`
HTTPRedirect uint32 `json:"received_redirect_http,omitempty"`
HTTPTTN uint32 `json:"received_ttn_http,omitempty"`
MQTTTTN uint32 `json:"received_ttn_mqtt,omitempty"`
}
// TTServeStatus is our global status
type TTServeStatus struct {
Started time.Time `json:"started,omitempty"`
AddressIPv4 string `json:"publicIp,omitempty"`
Services string `json:"services,omitempty"`
AWSInstance AWSInstanceIdentity `json:"aws,omitempty"`
Count TTServeCounts `json:"counts,omitempty"`
}
var stats TTServeStatus
// ServiceReadConfig gets the current value of the service config
func ServiceReadConfig() TTServeConfig {
// Read the file and unmarshall if no error
contents, err := os.ReadFile(SafecastDirectory() + TTConfigPath)
if err != nil {
fmt.Printf("Can't start service: %s %v\n", TTConfigPath, err)
os.Exit(0)
}
value := TTServeConfig{}
err = json.Unmarshal(contents, &value)
if err != nil {
fmt.Printf("Can't parse JSON: %s %v\n", TTConfigPath, err)
os.Exit(0)
}
return value
}