-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (49 loc) · 1.67 KB
/
main.go
File metadata and controls
63 lines (49 loc) · 1.67 KB
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
package main
import (
"log"
"github.com/caarlos0/env/v6"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/cors"
"github.com/gofiber/fiber/v3/middleware/recover"
"github.com/italia/publiccode-validator-api/internal/common"
"github.com/italia/publiccode-validator-api/internal/handlers"
"github.com/italia/publiccode-validator-api/internal/jsondecoder"
)
var (
Version = "dev" //nolint:gochecknoglobals // We need this to be set at build
Commit = "-" //nolint:gochecknoglobals // We need this to be set at build
)
func main() {
app := Setup()
if err := app.Listen(":3000"); err != nil {
log.Fatal(err)
}
}
func Setup() *fiber.App {
if err := env.Parse(&common.EnvironmentConfig); err != nil {
panic(err)
}
methods := append(fiber.DefaultMethods, "QUERY") //nolint:gocritic // We want a new slice here
app := fiber.New(fiber.Config{
ErrorHandler: common.CustomErrorHandler,
// Fiber doesn't set DisallowUnknownFields by default
// (https://github.com/gofiber/fiber/issues/2601)
JSONDecoder: jsondecoder.UnmarshalDisallowUnknownFields,
RequestMethods: methods,
})
// Automatically recover panics in handlers
app.Use(recover.New())
app.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "HEAD", "PUT", "DELETE", "PATCH", "QUERY"},
}))
setupHandlers(app)
return app
}
func setupHandlers(app *fiber.App) {
validateHandler := handlers.NewPubliccodeymlValidatorHandler()
statusHandler := handlers.NewStatus(Version, Commit)
v1 := app.Group("/v1")
v1.Get("/status", statusHandler.GetStatus)
v1.Add([]string{"QUERY"}, "/validate", validateHandler.Query)
v1.Add([]string{"POST"}, "/validate", validateHandler.Query)
}