/
config.go
96 lines (77 loc) · 3.36 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
package main
import (
"flag"
"log"
"os"
"strconv"
"time"
"github.com/caTUstrophy/backend/db"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator"
"github.com/joho/godotenv"
)
func InitAndConfig() *App {
// Define an initialization flag.
initFlag := flag.Bool("init", false, "Set this flag to true to initialize a fresh database with default data.")
flag.Parse()
// Load .env configuration files.
err := godotenv.Load()
if err != nil {
log.Fatal("[InitAndConfig] Failed to load .env files. Terminating.")
}
// Make space for a application struct containing our global context.
app := new(App)
// Read IP and port to run on from environment.
app.IP = os.Getenv("BACKEND_IP")
app.Port = os.Getenv("BACKEND_PORT")
// Create new gin instance with default functionalities and add it to app struct.
app.Router = gin.Default()
// Open connection to database and insert middleware.
app.DB = db.InitDB()
// If init flag was set to true, add default data to database.
if *initFlag {
db.AddDefaultData(app.DB)
}
// Set cost factor of bcrypt password hashing to the one loaded from environment.
app.HashCost, err = strconv.Atoi(os.Getenv("PASSWORD_HASHING_COST"))
if err != nil {
log.Fatal("[InitAndConfig] Could not load PASSWORD_HASHING_COST from .env file. Missing or not an integer?")
}
// Set JWT session token validity to the duration in minutes loaded from environment.
validFor, err := strconv.Atoi(os.Getenv("JWT_VALID_FOR"))
if err != nil {
log.Fatal("[InitAndConfig] Could not load JWT_VALID_FOR from .env file. Missing or not an integer?")
}
app.SessionValidFor = time.Duration(validFor) * time.Minute
// Initialize the validator instance to validate fields with tag 'validate'
validatorConfig := &validator.Config{TagName: "validate"}
app.Validator = validator.New(validatorConfig)
// Set sleep offset for offers and requests reaper.
offReqSleepOffset, err := strconv.Atoi(os.Getenv("OFFERS_REQUESTS_SLEEP_OFFSET"))
if err != nil {
log.Fatal("[InitAndConfig] Could not load OFFERS_REQUESTS_SLEEP_OFFSET from .env file. Missing or not an integer?")
}
app.OffReqSleepOffset = time.Duration(offReqSleepOffset) * time.Minute
// Set offsets for notification reaper to delete old notifications.
notifExpOffset, err := strconv.Atoi(os.Getenv("NOTIFICATION_EXPIRY_OFFSET"))
if err != nil {
log.Fatal("[InitAndConfig] Could not load NOTIFICATION_EXPIRY_OFFSET from .env file. Missing or not an integer?")
}
app.NotifExpOffset = time.Duration(notifExpOffset) * (time.Hour * 24)
notifSleepOffset, err := strconv.Atoi(os.Getenv("NOTIFICATION_SLEEP_OFFSET"))
if err != nil {
log.Fatal("[InitAndConfig] Could not load NOTIFICATION_SLEEP_OFFSET from .env file. Missing or not an integer?")
}
app.NotifSleepOffset = time.Duration(notifSleepOffset) * time.Minute
// Set weight for tags similarity in matching score calculation.
app.TagsWeightAlpha, err = strconv.ParseFloat(os.Getenv("TAGS_WEIGHT_ALPHA"), 64)
if err != nil {
log.Fatal("[InitAndConfig] Could not load TAGS_WEIGHT_ALPHA from .env file. Missing or not an integer?")
}
// Set weight for free text description similarity in matching score calculation.
app.DescWeightBeta, err = strconv.ParseFloat(os.Getenv("DESCRIPTIONS_WEIGHT_BETA"), 64)
if err != nil {
log.Fatal("[InitAndConfig] Could not load DESCRIPTIONS_WEIGHT_BETA from .env file. Missing or not an integer?")
}
return app
}