-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (61 loc) · 1.52 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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
a "github.com/openhealthalgorithms/service/actions"
c "github.com/openhealthalgorithms/service/config"
d "github.com/openhealthalgorithms/service/database"
m "github.com/openhealthalgorithms/service/models"
t "github.com/openhealthalgorithms/service/tools"
)
var (
dbFile = filepath.Join(t.GetCurrentDirectory(), "logs.db")
currentSettings c.Settings
sqlite *d.SqliteDb
)
func init() {
currentSettings = c.CurrentSettings()
dbFile = currentSettings.LogFile
}
func main() {
var err error
// Check if the DB file exists
if _, err := os.Stat(dbFile); os.IsNotExist(err) {
f, err := os.Create(dbFile)
if err != nil {
fmt.Println("Error:", err)
}
f.Close()
}
sqlite, err = d.InitDb(dbFile)
if err != nil {
fmt.Printf("Error in DB: %v\n", err)
os.Exit(1)
}
err = sqlite.Migrate()
if err != nil {
fmt.Printf("Error in DB: %v\n", err)
os.Exit(1)
}
defer sqlite.Closer()
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.Use(a.ServerHeader())
e.Use(a.CurrentConfig())
e.Validator = &m.CustomValidator{Validator: validator.New()}
// Routes
e.GET("/", a.HomeHandler)
e.GET("/api", a.HomeHandler)
e.GET("/api/version", a.VersionHandler)
e.POST("/api/algorithm", a.AlgorithmHandler)
// Start server
e.Logger.Fatal(e.Start(":" + currentSettings.Port))
}