-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (65 loc) · 1.74 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
package main
import (
"flag"
"log"
"strings"
"time"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/Sadathossain/mitnotes/notedb"
"github.com/mcuadros/go-gin-prometheus"
)
var (
appVersion string
showVersion bool
database notedb.NoteDB
)
func main() {
configFile := flag.String("config-file", "./default.config", "Path to the configuration file")
flag.BoolVar(&showVersion, "version", false, "Shows the version")
flag.Parse()
if showVersion {
log.Printf("Version: %s\n", appVersion)
return
}
config, err := readConfig(*configFile)
if err != nil {
log.Println(err)
}
gin.SetMode(config.ReleaseMode)
if strings.ToLower(config.DBDriver) == "mysql" {
database = notedb.NewMySQLDB(config.DBConfig, appVersion)
} else if strings.ToLower(config.DBDriver) == "redis" {
database = notedb.NewRedisDB(config.DBConfig, appVersion)
}
p := ginprometheus.NewPrometheus("gin")
database.RegisterMetrics()
// Iniitialize metrics
quit := make(chan struct{})
defer close(quit)
if config.HealthCheckTime > 0 {
healthCheckTimer := time.NewTicker(time.Duration(config.HealthCheckTime) * time.Second)
go func() {
for {
select {
case <-healthCheckTimer.C:
log.Println("Called Health check")
database.GetHealthStatus()
case <-quit:
healthCheckTimer.Stop()
return
}
}
}()
}
router := gin.Default()
p.Use(router)
router.GET("/read/note", readNoteHandler)
router.GET("/insert/note/:value", insertNoteHandler)
router.GET("/delete/note/:value", deleteNoteHandler)
router.GET("/health", healthCheckHandler)
router.GET("/whoami", whoAmIHandler)
router.GET("/version", versionHandler)
router.Use(static.Serve("/", static.LocalFile("./public", true)))
router.Run(":3000")
}