-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (84 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
102
103
104
105
106
107
108
109
110
package main
import (
"log"
"time"
"github.com/go-playground/validator/v10"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/philipp-mlr/al-id-maestro/handler"
"github.com/philipp-mlr/al-id-maestro/model"
"github.com/philipp-mlr/al-id-maestro/service"
)
func main() {
db, allowedList := initSetup()
initHttpServer(db, allowedList)
}
func initSetup() (*sqlx.DB, *model.AllowedList) {
log.Println("Initializing database...")
db, err := service.InitDB("al-id-maestro")
if err != nil {
log.Fatal(err)
}
log.Println("Loading configuration...")
config, err := service.NewConfig(db)
if err != nil {
log.Fatal(err)
}
log.Println("Calculating allowed ID ranges...")
allowedList, err := service.NewAllowList(config)
if err != nil {
log.Fatal(err)
}
err = service.InitRepos(config)
if err != nil {
log.Fatal(err)
}
log.Println("Starting cron jobs...")
go service.StartCronJob(30*time.Second, db, config)
log.Print("Starting server...\n\n")
return db, allowedList
}
func initHttpServer(db *sqlx.DB, allowedList *model.AllowedList) {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Pre(middleware.RemoveTrailingSlash())
e.Validator = &handler.CustomValidator{Validator: validator.New(validator.WithRequiredStructEnabled())}
e.Static("/static", "./public")
indexHandler := handler.IndexHandler{
DB: db,
}
e.GET("/", indexHandler.HandleIndexShow)
claimHandler := handler.ClaimHandler{
DB: db,
AllowedList: allowedList,
}
e.GET("/claim", claimHandler.HandlePageShow)
historyHandler := handler.HistoryHandler{
DB: db,
}
e.GET("/history", historyHandler.HandleHistoryShow)
e.POST("/history", historyHandler.HandlePostQuery)
duplicatesHandler := handler.DuplicatesHandler{
DB: db,
}
e.GET("/duplicates", duplicatesHandler.HandleDuplicatesShow)
remoteHandler := handler.RemoteHandler{
DB: db,
}
e.GET("/remote", remoteHandler.HandleRemoteShow)
usedHandler := handler.UsedHandler{
DB: db,
}
e.GET("/used", usedHandler.HandleUsedShow)
aboutHandler := handler.AboutHandler{}
e.GET("/about", aboutHandler.HandleAboutShow)
e.POST("/claim/query-type", claimHandler.HandleObjectTypeQuery)
e.POST("/claim/request-id", claimHandler.HandleNewObjectClaim)
e.POST("/api/request-id", claimHandler.HandleNewObjectClaimAPI)
// Start server
e.Logger.Fatal(e.Start(":5000"))
}