-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (131 loc) · 2.97 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
userapi "github.com/kswarthout/radiate-love-api/api/user"
"github.com/kswarthout/radiate-love-api/domain"
"github.com/kswarthout/radiate-love-api/service"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/gorilla/mux"
)
var dal *service.DAL
var users *userapi.UsersService
var config *domain.Config
// Routes configures all API routes
func Routes() *mux.Router {
// INIT CONTROLLERS
uc := userapi.Controller{
Repo: users,
}
// REGISTER ROUTES
router := mux.NewRouter().StrictSlash(true)
router = uc.AddRoutes(router)
return router
}
func initConfig() {
config = &domain.Config{}
configService := service.ConfigService{
Config: config,
Location: getConfigPath(),
}
configService.Reload()
go configService.Watch(time.Second * 30)
}
func initDataAccess() {
// INIT DB CONNECTION
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
uri, err := getMongoURI()
if err != nil {
log.Fatal(err)
}
clientOptions := options.Client().ApplyURI(uri)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)
// CHECK CONNECTION
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
// INIT DATA ACCESS LAYER
dal = &service.DAL{
Client: client,
Config: config,
}
dal.Connect()
users = &userapi.UsersService{
DAL: dal,
}
users.Load()
}
func getConfigPath() string {
env := os.Getenv("ENV")
if len(env) == 0 {
env = "development"
}
filename := []string{"config.", env, ".yaml"}
return strings.Join(filename, "")
}
func getAPIPort() (string, error) {
var port = os.Getenv("PORT")
if port == "" {
apiConfig, err := config.Get("base")
if err != nil {
return "8080", err
}
if configPort, ok := apiConfig["port"].(string); ok {
port = configPort
} else {
port = "8080"
}
fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
}
return ":" + port, nil
}
func getMongoURI() (string, error) {
var uri = os.Getenv("DATABASE_URL")
if uri == "" {
var port = ""
var host = ""
dbConfig, err := config.Get("db")
if err != nil {
return "", err
}
if configPort, ok := dbConfig["port"].(string); ok {
port = configPort
} else {
port = "27017"
}
if configHost, ok := dbConfig["host"].(string); ok {
host = configHost
} else {
host = "localhost"
}
uri = "mongodb://" + host + ":" + port
fmt.Println("INFO: No DATABASE_URL environment variable detected, defaulting to " + uri)
}
return uri, nil
}
func main() {
fmt.Println("Starting the application...")
// LOAD APP CONFIG & INIT SERVICES
initConfig()
initDataAccess()
// INIT ROUTER
router := Routes()
apiPort, err := getAPIPort()
if err != nil {
log.Fatal(err)
}
log.Println("Serving application at Port :" + apiPort)
log.Fatal(http.ListenAndServe(apiPort, router))
}