-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathmain.go
416 lines (335 loc) · 10.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"database/sql"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/Tanibox/tania-core/src/eventbus"
"github.com/asaskevich/EventBus"
"golang.org/x/crypto/ssh/terminal"
"github.com/go-sql-driver/mysql"
"github.com/Tanibox/tania-core/config"
assetsserver "github.com/Tanibox/tania-core/src/assets/server"
assetsstorage "github.com/Tanibox/tania-core/src/assets/storage"
growthserver "github.com/Tanibox/tania-core/src/growth/server"
growthstorage "github.com/Tanibox/tania-core/src/growth/storage"
locationserver "github.com/Tanibox/tania-core/src/location/server"
tasksserver "github.com/Tanibox/tania-core/src/tasks/server"
taskstorage "github.com/Tanibox/tania-core/src/tasks/storage"
userserver "github.com/Tanibox/tania-core/src/user/server"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
colorable "github.com/mattn/go-colorable"
_ "github.com/mattn/go-sqlite3"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
)
func init() {
if terminal.IsTerminal(int(os.Stdout.Fd())) {
log.SetFormatter(&log.TextFormatter{ForceColors: true})
// We need this for Windows to get coloured
// https://github.com/sirupsen/logrus#formatters
log.SetOutput(colorable.NewColorableStdout())
} else {
log.SetFormatter(&log.JSONFormatter{})
}
config.InitViperConfig()
}
func main() {
e := echo.New()
// Initialize DB.
log.Print("Using " + *config.Config.TaniaPersistenceEngine + " persistance engine")
// InMemory DB will always be initialized.
inMem := initInMemory()
var db *sql.DB
switch *config.Config.TaniaPersistenceEngine {
case config.DB_SQLITE:
db = initSqlite()
case config.DB_MYSQL:
db = initMysql()
}
// Initialize Event Bus
bus := eventbus.NewSimpleEventBus(EventBus.New())
// Initialize Server
farmServer, err := assetsserver.NewFarmServer(
db,
inMem.farmEventStorage,
inMem.farmReadStorage,
inMem.areaEventStorage,
inMem.areaReadStorage,
inMem.reservoirEventStorage,
inMem.reservoirReadStorage,
inMem.materialEventStorage,
inMem.materialReadStorage,
inMem.cropReadStorage,
bus,
)
if err != nil {
e.Logger.Fatal(err)
}
taskServer, err := tasksserver.NewTaskServer(
db,
bus,
inMem.cropReadStorage,
inMem.areaReadStorage,
inMem.materialReadStorage,
inMem.reservoirReadStorage,
inMem.taskEventStorage,
inMem.taskReadStorage,
)
if err != nil {
e.Logger.Fatal(err)
}
growthServer, err := growthserver.NewGrowthServer(
db,
bus,
inMem.cropEventStorage,
inMem.cropReadStorage,
inMem.cropActivityStorage,
inMem.areaReadStorage,
inMem.materialReadStorage,
inMem.farmReadStorage,
inMem.taskReadStorage,
)
if err != nil {
e.Logger.Fatal(err)
}
userServer, err := userserver.NewUserServer(db, bus)
if err != nil {
e.Logger.Fatal(err)
}
authServer, err := userserver.NewAuthServer(db, bus)
if err != nil {
e.Logger.Fatal(err)
}
locationServer, err := locationserver.NewLocationServer()
if err != nil {
e.Logger.Fatal(err)
}
// Initialize user
err = initUser(authServer)
// Initialize Echo Middleware
e.Use(middleware.Recover())
e.Use(headerNoCache)
e.Use(logrusMiddleware())
e.Use(middleware.RequestID())
APIMiddlewares := []echo.MiddlewareFunc{}
if !*config.Config.DemoMode {
APIMiddlewares = append(APIMiddlewares, tokenValidationWithConfig(db))
}
// HTTP routing
API := e.Group("api")
API.Use(middleware.CORS())
// AuthServer is used for endpoint that doesn't need authentication checking
authGroup := API.Group("/")
authServer.Mount(authGroup)
locationGroup := API.Group("/locations", APIMiddlewares...)
locationServer.Mount(locationGroup)
farmGroup := API.Group("/farms", APIMiddlewares...)
farmServer.Mount(farmGroup)
growthServer.Mount(farmGroup)
taskGroup := API.Group("/tasks", APIMiddlewares...)
taskServer.Mount(taskGroup)
userGroup := API.Group("/user", APIMiddlewares...)
userServer.Mount(userGroup)
e.Static("/", "public")
// Start Server
e.Logger.Fatal(e.Start(":" + *config.Config.AppPort))
}
func initUser(authServer *userserver.AuthServer) error {
defaultUsername := "tania"
defaultPassword := "tania"
_, _, err := authServer.RegisterNewUser(defaultUsername, defaultPassword, defaultPassword)
if err != nil {
log.Print("User ", defaultUsername, " has already created")
return err
}
log.Print("User created with default username and password")
return nil
}
// MIDDLEWARES
func headerNoCache(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
c.Response().Header().Set("Pragma", "no-cache") // HTTP 1.0.
c.Response().Header().Set("Expires", "0") // Proxies.
return next(c)
}
}
type InMemory struct {
farmEventStorage *assetsstorage.FarmEventStorage
farmReadStorage *assetsstorage.FarmReadStorage
areaEventStorage *assetsstorage.AreaEventStorage
areaReadStorage *assetsstorage.AreaReadStorage
reservoirEventStorage *assetsstorage.ReservoirEventStorage
reservoirReadStorage *assetsstorage.ReservoirReadStorage
materialEventStorage *assetsstorage.MaterialEventStorage
materialReadStorage *assetsstorage.MaterialReadStorage
cropEventStorage *growthstorage.CropEventStorage
cropReadStorage *growthstorage.CropReadStorage
cropActivityStorage *growthstorage.CropActivityStorage
taskEventStorage *taskstorage.TaskEventStorage
taskReadStorage *taskstorage.TaskReadStorage
}
func initInMemory() *InMemory {
return &InMemory{
farmEventStorage: assetsstorage.CreateFarmEventStorage(),
farmReadStorage: assetsstorage.CreateFarmReadStorage(),
areaEventStorage: assetsstorage.CreateAreaEventStorage(),
areaReadStorage: assetsstorage.CreateAreaReadStorage(),
reservoirEventStorage: assetsstorage.CreateReservoirEventStorage(),
reservoirReadStorage: assetsstorage.CreateReservoirReadStorage(),
materialEventStorage: assetsstorage.CreateMaterialEventStorage(),
materialReadStorage: assetsstorage.CreateMaterialReadStorage(),
cropEventStorage: growthstorage.CreateCropEventStorage(),
cropReadStorage: growthstorage.CreateCropReadStorage(),
cropActivityStorage: growthstorage.CreateCropActivityStorage(),
taskEventStorage: taskstorage.CreateTaskEventStorage(),
taskReadStorage: taskstorage.CreateTaskReadStorage(),
}
}
func initMysql() *sql.DB {
host := *config.Config.MysqlHost
port := *config.Config.MysqlPort
dbname := *config.Config.MysqlDbname
user := *config.Config.MysqlUsername
pwd := *config.Config.MysqlPassword
dsn := user + ":" + pwd + "@(" + host + ":" + port + ")/" + dbname + "?parseTime=true&clientFoundRows=true"
db, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
log.Print("Using MySQL at ", host, ":", port, "/", dbname)
ddl, err := ioutil.ReadFile("db/mysql/ddl.sql")
if err != nil {
panic(err)
}
sqls := string(ddl)
// We need to split the DDL query by `;` and execute it one by one.
// Because sql.DB.Exec() from mysql driver cannot executes multiple query at once
// and it will give weird syntax error messages.
splitted := strings.Split(sqls, ";")
for _, v := range splitted {
trimmed := strings.TrimSpace(v)
if len(trimmed) > 0 {
_, err = db.Exec(v)
if err != nil {
me, ok := err.(*mysql.MySQLError)
if !ok {
panic("Error executing DDL query")
}
// http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
// We will skip error duplicate key name in database (code: 1061),
// because CREATE INDEX doesn't have IF NOT EXISTS clause,
// otherwise we will stop the loop and print the error
if me.Number == 1061 {
} else {
log.Print(err)
return db
}
}
}
}
log.Print("DDL file executed")
return db
}
func initSqlite() *sql.DB {
if _, err := os.Stat(*config.Config.SqlitePath); os.IsNotExist(err) {
log.Print("Creating database file ", *config.Config.SqlitePath)
}
db, err := sql.Open("sqlite3", *config.Config.SqlitePath)
if err != nil {
panic(err)
}
log.Print("Using SQLite at ", *config.Config.SqlitePath)
log.Print("Executing DDL file for ", *config.Config.SqlitePath)
ddl, err := ioutil.ReadFile("db/sqlite/ddl.sql")
if err != nil {
panic(err)
}
sql := string(ddl)
_, err = db.Exec(sql)
if err != nil {
panic(err)
}
log.Print("DDL file executed")
return db
}
func tokenValidationWithConfig(db *sql.DB) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
authorization := c.Request().Header.Get("Authorization")
if authorization == "" {
return c.JSON(http.StatusUnauthorized, map[string]string{"data": "Unauthorized"})
}
splitted := strings.Split(authorization, " ")
if len(splitted) <= 1 {
return c.JSON(http.StatusUnauthorized, map[string]string{"data": "Unauthorized"})
}
var uid interface{}
err := db.QueryRow(`SELECT USER_UID
FROM USER_AUTH WHERE ACCESS_TOKEN = ?`, splitted[1]).Scan(&uid)
if err != nil {
return c.JSON(http.StatusUnauthorized, map[string]string{"data": "Unauthorized"})
}
ubyte, ok := uid.([]byte)
if !ok {
return c.JSON(http.StatusInternalServerError, map[string]string{"data": "Error user UID type assertion"})
}
var userUID uuid.UUID
if *config.Config.TaniaPersistenceEngine == config.DB_SQLITE {
userUID, err = uuid.FromString(string(ubyte))
} else if *config.Config.TaniaPersistenceEngine == config.DB_MYSQL {
ubyte := uid.([]byte)
userUID, err = uuid.FromBytes(ubyte)
}
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]error{"data": err})
}
c.Set("USER_UID", userUID)
return next(c)
}
}
}
func logrusMiddleware() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
start := time.Now()
if err := next(c); err != nil {
c.Error(err)
}
res := c.Response()
stop := time.Now()
fields := map[string]interface{}{
"request_id": res.Header().Get(echo.HeaderXRequestID),
"ip": c.RealIP(),
"host": req.Host,
"uri": req.RequestURI,
"method": req.Method,
"user_agent": req.UserAgent(),
"status": res.Status,
"roundtrip": strconv.FormatInt(stop.Sub(start).Nanoseconds()/1000, 10),
"roundtrip_human": stop.Sub(start).String(),
}
// We will add log Form Values and Query String if...
if res.Status == http.StatusInternalServerError {
if !strings.HasPrefix(req.Header.Get(echo.HeaderContentType), echo.MIMEMultipartForm) {
qs := c.QueryString()
forms, err := c.FormParams()
if err != nil {
c.Error(err)
}
fields["query_string"] = qs
fields["form_values"] = forms
}
}
log.WithFields(fields).Info("Response Info")
return nil
}
}
}