-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (81 loc) · 2.03 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
package main
import (
"assignment2/database"
"assignment2/handler"
"assignment2/stubs"
"assignment2/util"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
// Initialize build configurations
err := util.InitializeConfig()
if err != nil {
log.Fatalf("Error initializing config: %v", err)
} else {
log.Println("Initialized config")
}
// Initialize database
if util.Config.Stubs.Database == true {
// Database stub
go stubs.DatabaseStub()
} else {
// Live database
defer func() {
err := database.CloseDatabase()
if err != nil {
log.Fatal("Unable to close the database connection")
} else {
log.Println("Closed database connection")
}
}()
dbError := database.InitializeDatabase()
if dbError != nil {
log.Fatalf("Failed to initialize the database connection: %v", dbError)
} else {
log.Println("Database initialized")
}
}
// Start stub service if it's environment variable is present.
if util.Config.Stubs.Weather == true {
go stubs.Weather_stub()
}
if util.Config.Stubs.Currencies == true {
go stubs.Currency_stub()
}
if util.Config.Stubs.RestCountries == true {
go stubs.Country_stub()
}
// Run the main server
go func() {
port := os.Getenv("PORT")
if port == "" { //sets port to 8080 as a default
log.Println(("$PORT has not been set. Default 8080"))
port = "8080"
}
http.HandleFunc(util.REGISTRATION_PATH, handler.RegistrationHandler)
http.HandleFunc(util.DASHBOARD_PATH, handler.DashboardHandler)
http.HandleFunc(util.NOTIFICATION_PATH, handler.NotificationHandler)
http.HandleFunc(util.STATUS_PATH, handler.StatusHandler)
log.Println("Service is listening on port: " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}()
// https://stackoverflow.com/a/66834066
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
for {
sig := <-signalChannel
switch sig {
case os.Interrupt:
fmt.Println("sigint")
return
case syscall.SIGTERM:
fmt.Println("sigterm")
return
}
}
}