-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (91 loc) · 2.83 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
package main
import (
"errors"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
"strconv"
"surface_attack/consts"
"surface_attack/handlers/global/logger"
"surface_attack/handlers/jsonHandler"
"surface_attack/middleware"
"surface_attack/providers/crudProvider"
"surface_attack/webHandlers"
)
func main() {
startLogger()
loadEnvFile()
crudProvider := crudProviderStart()
defer crudProvider.EndConn()
handleOnBootJsonData(crudProvider)
httpServerStart(crudProvider)
}
func startLogger() {
logger := logger.HandlerLogging{}
logger.Init()
log.Println("begin boot-up")
}
func loadEnvFile() {
// loads values from .env into the system
e := godotenv.Load()
mainErrorHandler(e)
log.Println(".env file loaded succesfully")
}
func crudProviderStart() *crudProvider.Implamintation {
redis_addr, exists := os.LookupEnv(consts.REDIS_ADDR_ENV_KEY)
mainExistHandler(exists, errors.New("value for key REDIS_ADDR does not exist"))
crudProvider := crudProvider.Implamintation{}
e := crudProvider.InitConn(redis_addr)
mainErrorHandler(e)
return &crudProvider
}
func handleOnBootJsonData(crudProvider *crudProvider.Implamintation) {
loadJsonToCrudProvider, exists := os.LookupEnv(consts.ON_BOOT_JSON_LOAD)
mainExistHandler(exists, errors.New("value for key ON_BOOT_JSON_LOAD does not exist"))
b, e := strconv.ParseBool(loadJsonToCrudProvider)
mainErrorHandler(e)
// b true is relevant only for first worker that loades the map into the crud-provider.
// any workers that we spin-up for scale purpuses and is working on same data will have
//b = false.
log.Println(consts.ON_BOOT_JSON_LOAD, " env param set to: ", b)
if b {
jsonHandler := jsonHandler.InputJsonHandler{
CrudProvider: crudProvider,
}
e = jsonHandler.OnBoot(consts.DATA_FILE_LOCATION_NAME)
mainErrorHandler(e)
}
}
func httpServerStart(crudProvider *crudProvider.Implamintation) {
//start http server
timerWrapper := middleware.TimerWrapper{
CrudProvider: crudProvider,
}
statsHandler := webHandlers.StatsHandler{
CrudProvider: crudProvider,
}
attackHandler := webHandlers.AttackHandler{
CrudProvider: crudProvider,
}
port, exists := os.LookupEnv(consts.PORT_ENV_KEY)
mainExistHandler(exists, errors.New("value for key: "+consts.PORT_ENV_KEY+" does not exist"))
http.Handle(consts.STATS_END_POINT, timerWrapper.Timer(http.HandlerFunc(statsHandler.Handle)))
log.Println("stats end point available on: ", consts.STATS_END_POINT)
http.Handle(consts.ATTACK_END_POINT, timerWrapper.Timer(http.HandlerFunc(attackHandler.Handle)))
log.Println("attack end point available on: ", consts.ATTACK_END_POINT)
log.Println("HTTP server is listening on port ", port)
http.ListenAndServe(port, nil)
}
func mainErrorHandler(e error) {
if e != nil {
log.Println(e)
os.Exit(1)
}
}
func mainExistHandler(exists bool, e error) {
if !exists {
log.Println(e)
os.Exit(1)
}
}