-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
400 lines (334 loc) · 9.72 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Sencer Nerede - Main Server
// December 2023, Sencer Yucel
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"sort"
"time"
"github.com/dgrijalva/jwt-go"
mqtt "github.com/eclipse/paho.mqtt.golang"
redis "github.com/go-redis/redis/v8"
"github.com/gorilla/websocket"
"golang.org/x/crypto/bcrypt"
)
var signKey = []byte(os.Getenv("JWT_SECRET"))
type Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
type Config struct {
HttpPort string `json:"http_port"`
}
var ctx = context.Background()
var rdb = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "",
DB: 0,
})
// get current date and time
func getCurrentDateTime() string {
loc, err := time.LoadLocation("Europe/Istanbul")
if err != nil {
fmt.Println("Error:", err)
return "01_01_23-00:00:01"
}
return time.Now().In(loc).Format("01_02_06-15:04:05")
}
func generateJWT() (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["authorized"] = true
claims["user"] = os.Getenv("JWT_USER")
claims["exp"] = time.Now().Add(time.Minute * 30).Unix()
tokenString, err := token.SignedString(signKey)
if err != nil {
log.Println("Error in JWT token generation")
return "", err
}
return tokenString, nil
}
func setCorsHeaders(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", os.Getenv("DOMAIN"))
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
/*
********************************
WEBSOCKET OPERATIONS
********************************
*/
var clients = make(map[*websocket.Conn]bool)
var broadcast = make(chan []byte)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// handle incoming ws connection
func handleConnections(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil) // Upgrades HTTP connection to ws
if err != nil {
log.Fatal(err)
return
}
defer ws.Close()
clients[ws] = true
log.Printf("New connection established from %s\n", r.RemoteAddr)
// Fetch all keys
keys, err := rdb.Keys(ctx, "*").Result()
if err != nil {
log.Fatal("Error fetching keys from Redis:", err)
return
}
// Sort the keys
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
// Iterate over sorted keys and send to ws
for _, key := range keys {
data, err := getList(rdb, key)
if err != nil {
log.Fatal("Error fetching data from Redis for key", key, ":", err)
continue
}
// Each set of three items in the list are lat, lon
for i := 0; i < len(data); i += 3 {
if i+1 < len(data) {
locationJSON, _ := json.Marshal(map[string]string{
"lat": data[i],
"lon": data[i+1],
"timestamp": key,
})
if err := ws.WriteMessage(websocket.TextMessage, locationJSON); err != nil {
log.Fatal("Error sending message:", err)
return
}
}
}
}
// Read messages from ws
for {
_, _, err := ws.ReadMessage()
if err != nil {
delete(clients, ws)
break
}
}
}
// send message to the broadcast channel
func broadcastToWebSockets(message []byte) {
broadcast <- message
}
// handle messages received on the broadcast channel
func handleMessages() {
for {
msg := <-broadcast
for client := range clients {
err := client.WriteMessage(websocket.TextMessage, msg)
if err != nil {
log.Fatal("websocket err:", err)
client.Close()
delete(clients, client)
}
}
}
}
/*
********************************
END OF WEBSOCKET OPERATIONS
********************************
*/
/*
********************************
MQTT OPERATIONS
********************************
*/
// MQTT message handler. (equivalent to on_message in Python)
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
// fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
re := regexp.MustCompile(`"latitude":([\d.]+),"longitude":([\d.]+)`)
matches := re.FindStringSubmatch(string(msg.Payload()))
fmt.Println(len(matches))
if len(matches) != 3 {
log.Fatal("Invalid message format")
return
}
currentDateTime := getCurrentDateTime()
// Inserting latitude, longitude into Redis as a list with key datetime.
insertList(rdb, currentDateTime, []string{matches[1], matches[2]})
// Create a map with the parsed location data and include the timestamp
location := map[string]string{
"lat": matches[1],
"lon": matches[2],
"timestamp": currentDateTime,
}
locationJSON, _ := json.Marshal(location)
broadcastToWebSockets(locationJSON)
}
// MQTT connect handler (equivalent to on_connect in Python).
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
log.Printf("MQTT Connection successful") // Prints a message on successful connection.
}
// MQTT connection lost handler (equivalent to on_disconnect in Python).
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
log.Printf("MQTT connection lost: %v", err)
}
// subscribe to an MQTT topic
func sub(client mqtt.Client, topic string, qos byte) {
token := client.Subscribe(topic, qos, nil)
token.Wait()
log.Printf("Subscribed to topic %s with QoS %d\n", topic, qos)
}
func sendLocationViaMQTT(client mqtt.Client, loc Location) {
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Println(token.Error())
return
}
// Construct the message (example: simple JSON)
msg, err := json.Marshal(loc)
if err != nil {
log.Println("Error marshaling location data:", err)
return
}
// Publish
token := client.Publish(os.Getenv("MQTT_TOPIC"), 0, false, msg)
token.Wait()
}
/*
********************************
END OF MQTT OPERATIONS
********************************
*/
/*
********************************
REDIS DB OPERATIONS
********************************
*/
func insertList(rdb *redis.Client, key string, values []string) error {
err := rdb.RPush(ctx, key, values).Err()
if err != nil {
return err
}
return nil
}
func getList(rdb *redis.Client, key string) ([]string, error) {
val, err := rdb.LRange(ctx, key, 0, -1).Result()
if err != nil {
return nil, err
}
return val, nil
}
/*
********************************
END OF REDIS DB OPERATIONS
********************************
*/
func main() {
logFile, err := os.OpenFile("/app/all.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal("Failed to open log file:", err)
}
defer logFile.Close()
log.SetOutput(logFile)
log.Printf("\n------Starting Server------")
configFile, err := ioutil.ReadFile("/app/config.json")
if err != nil {
log.Fatal("Error reading config file:", err)
os.Exit(1)
}
var config Config
err = json.Unmarshal(configFile, &config)
if err != nil {
log.Fatal("Error parsing config file:", err)
os.Exit(1)
}
go handleMessages()
http.HandleFunc("/ws", handleConnections)
opts := mqtt.NewClientOptions()
opts.AddBroker(os.Getenv("MQTT_HOST"))
opts.SetClientID(os.Getenv("MQTT_CLIENT_NAME"))
opts.SetUsername(os.Getenv("MQTT_USERNAME"))
opts.SetPassword(os.Getenv("MQTT_PASSWORD"))
opts.SetDefaultPublishHandler(messagePubHandler)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
os.Exit(1)
}
sub(client, os.Getenv("MQTT_TOPIC"), 2) // client, topic, qos
http.HandleFunc("/api/authenticate", func(w http.ResponseWriter, r *http.Request) {
setCorsHeaders(&w)
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
var creds Credentials
err := json.NewDecoder(r.Body).Decode(&creds)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ADMIN_USERNAME := os.Getenv("HASHED_ADMIN_USERNAME")
ADMIN_PASSWORD := os.Getenv("HASHED_ADMIN_PASSWORD")
errUsername := bcrypt.CompareHashAndPassword([]byte(ADMIN_USERNAME), []byte(creds.Username))
errPassword := bcrypt.CompareHashAndPassword([]byte(ADMIN_PASSWORD), []byte(creds.Password))
if errUsername == nil && errPassword == nil {
fmt.Println("Valid credentials")
tokenString, err := generateJWT()
if err != nil {
http.Error(w, "Error generating token", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(map[string]string{"token": tokenString})
} else {
fmt.Println("Invalid credentials")
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
}
})
http.HandleFunc("/api/location", func(w http.ResponseWriter, r *http.Request) {
setCorsHeaders(&w)
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
var loc Location
err := json.NewDecoder(r.Body).Decode(&loc)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sendLocationViaMQTT(client, loc)
json.NewEncoder(w).Encode(map[string]string{"status": "received"})
})
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/var/www/html"))))
http.HandleFunc("/nerede", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "/var/www/html/nerede.html")
})
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "/var/www/html/update.html")
})
// starts a new goroutine.
go func() {
var http_port = config.HttpPort
log.Print("Starting server on :" + http_port)
err := http.ListenAndServe(":"+http_port, nil)
if err != nil {
log.Fatal("Failed to start HTTP server:", err)
}
}()
fmt.Println("Server is up and running")
select {}
}