-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
60 lines (48 loc) · 1.05 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
package main
import (
"log"
"net/http"
"os"
"github.com/sendinblue/dpe-insights/api"
"github.com/sendinblue/dpe-insights/core/logger"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
// app Application .
type app struct {
Router *mux.Router
}
// Initialize the application.
func (a *app) Initialize() {
setConfiguration()
// Configure application log file
logger.Configure()
a.Router = api.Router()
}
// setConfiguration set env and configuration values.
func setConfiguration() {
viper.SetDefault("APP_PORT", "8080")
if _, err := os.Stat(".env"); err == nil {
viper.SetConfigFile(".env")
// Find and read the config file
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error while reading config file %s", err)
}
}
// Enable VIPER to read Environment Variables
viper.AutomaticEnv()
}
// Run the application.
func (a *app) Run(port string) {
err := http.ListenAndServe(":"+port, a.Router)
if err != nil {
log.Fatal()
}
}
func main() {
a := app{}
a.Initialize()
port := viper.GetString("APP_PORT")
a.Run(port)
}