-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
128 lines (107 loc) · 3.22 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
package main
import (
"os"
"strconv"
"time"
"github.com/rameshsunkara/deferrun"
"github.com/rameshsunkara/go-rest-api-example/internal/db"
"github.com/rameshsunkara/go-rest-api-example/internal/logger"
"github.com/rameshsunkara/go-rest-api-example/internal/models"
"github.com/rameshsunkara/go-rest-api-example/internal/server"
)
const (
serviceName = "ecommerce-orders"
defaultPort = "8080"
)
// Passed while building from make file.
var version string
func main() {
if err := run(); err != nil {
os.Exit(1)
}
}
func run() error {
upTime := time.Now().UTC().Format(time.RFC3339)
sigHandler := deferrun.NewSignalHandler()
// setup : read environmental configurations
svcEnv := MustEnvConfig()
// setup : service logger
lgr := logger.Setup(svcEnv)
// setup : database connection
dbCredentials, err := db.MongoDBCredentialFromSideCar(svcEnv.MongoVaultSideCar)
if err != nil {
lgr.Fatal().Err(err).Msg("failed to fetch DB credentials")
return err
}
connOpts := &db.ConnectionOpts{
Database: svcEnv.DBName,
PrintQueries: svcEnv.PrintQueries,
}
dbConnMgr, err := db.NewMongoManager(dbCredentials, connOpts, lgr)
if err != nil {
lgr.Fatal().Err(err).Msg("unable to initialize DB connection")
return err
}
sigHandler.OnSignal(func() {
dErr := dbConnMgr.Disconnect()
if dErr != nil {
lgr.Error().Err(dErr).Msg("unable to disconnect from DB, potential connection leak")
return
}
})
lgr.Info().
Str("name", serviceName).
Str("environment", svcEnv.Name).
Str("started", upTime).
Str("version", version).
Msg("service details, starting the service")
// setup : start service
server.StartService(svcEnv, dbConnMgr, lgr)
lgr.Fatal().Msg("service stopped")
return nil
}
// MustEnvConfig reads all the environmental configurations and panics if something critical is missing.
func MustEnvConfig() models.ServiceEnv {
envName := os.Getenv("environment")
if envName == "" {
envName = "local"
}
port := os.Getenv("port")
if port == "" {
port = defaultPort
}
dbName := os.Getenv("dbName")
if dbName == "" {
panic("dbName should be defined in env configuration")
}
// printDBQueries is optional, default is false, when set to true, it will print all the queries to the console.
printDBQueries, err := strconv.ParseBool(os.Getenv("printDBQueries"))
if err != nil {
printDBQueries = false
}
mongoSideCar := os.Getenv("MongoVaultSideCar")
if mongoSideCar == "" {
panic("mongo sidecar file path should be defined in env configuration")
}
// disableAuth is optional, default is false, when set to true, it will disable authentication.
// Added for development purpose, do not use in production.
disableAuth, authEnvErr := strconv.ParseBool(os.Getenv("disableAuth"))
if authEnvErr != nil {
// do not disable authentication by default, added this flexibility just for local development purpose
disableAuth = false
}
logLevel := os.Getenv("logLevel")
if logLevel == "" {
logLevel = "info"
}
envConfigurations := models.ServiceEnv{
Name: envName,
Port: port,
PrintQueries: printDBQueries,
MongoVaultSideCar: mongoSideCar,
DisableAuth: disableAuth,
DBName: dbName,
LogLevel: logLevel,
}
return envConfigurations
}