Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (64 loc) · 2.16 KB
/
Copy pathmain.go
File metadata and controls
80 lines (64 loc) · 2.16 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"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 := listen(app, ":3000"); err != nil {
log.Fatal(err)
}
}
func listen(app *fiber.App, address string) error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := app.Listen(address, fiber.ListenConfig{GracefulContext: ctx}); err != nil {
return fmt.Errorf("listen: %w", err)
}
return nil
}
func Setup() *fiber.App {
methods := append(fiber.DefaultMethods, "QUERY") //nolint:gocritic // We want a new slice here
app := fiber.New(fiber.Config{
ErrorHandler: common.CustomErrorHandler,
// Allow 30 seconds to upload the default maximum body of 4 MiB.
ReadTimeout: 30 * time.Second,
// This bounds response writes after the handler, including external checks, returns.
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
// 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)
}