forked from techrail/bark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (88 loc) · 2.42 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
package main
import (
"fmt"
"log"
"github.com/fasthttp/router"
"github.com/joho/godotenv"
"github.com/valyala/fasthttp"
`github.com/techrail/bark/controllers`
`github.com/techrail/bark/resources`
`github.com/techrail/bark/services/dbLogWriter`
)
func Index(ctx *fasthttp.RequestCtx) {
_, _ = ctx.WriteString("Welcome to Bark!")
}
func Hello(ctx *fasthttp.RequestCtx) {
_, _ = fmt.Fprintf(ctx, "Hello, %s!\n", ctx.UserValue("name"))
}
// Init performs prerequisite tasks - like loading env variables
func Init() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
Init()
r := router.New()
r.GET("/", Index)
r.GET("/hello/{name}", Hello)
r.POST("/insertSingle", controllers.SendSingleToChannel)
r.POST("/insertMultiple", controllers.SendMultipleToChannel)
err := resources.InitDatabase()
if err != nil {
log.Fatal("E#1KDZRP - " + err.Error())
}
go dbLogWriter.StartWritingLogs()
log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler))
// =========== TEST CASE (To be refactored) ===========
// NOTE: We will write the tests later, separately
// more_data is a JSONB field in the db, in the BarkLog struct its stored as a json.RawMessage ([]byte) field.
// So we need to Marshal it to json before inserting
// moreData, _ := json.Marshal(map[string]interface{}{
// "a": "apple",
// "b": "banana",
// },
// )
// sampleLog := []models.BarkLog{
// // Id: 1234,
// {LogTime: time.Now(),
// LogLevel: 0,
// ServiceName: "test",
// Code: "1234",
// Message: "Test",
// MoreData: moreData},
// {LogTime: time.Now(),
// LogLevel: 0,
// ServiceName: "test",
// Code: "1234",
// Message: "Test",
// MoreData: moreData},
// {LogTime: time.Now(),
// LogLevel: 0,
// ServiceName: "test",
// Code: "1234",
// Message: "Test",
// MoreData: moreData},
// {LogTime: time.Now(),
// LogLevel: 0,
// ServiceName: "test",
// Code: "1234",
// Message: "Test",
// MoreData: moreData},
// }
// err = barkDb.InsertBatch(sampleLog)
// if err != nil {
// log.Fatal(err)
// }
//
// // 2.Fetch n number of logs
// logs, err := barkDb.FetchLimitedLogs(4)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Print(logs)
// =============================================================
// ctx.Response.SetStatusCode(200)
// go batchCommit(logChannel)
}