-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
151 lines (126 loc) · 5.08 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"log"
"os"
"time"
"github.com/caarlos0/env/v6"
"gorm.io/gorm"
"github.com/italia/developers-italia-api/internal/common"
"github.com/italia/developers-italia-api/internal/database"
"github.com/italia/developers-italia-api/internal/handlers"
"github.com/italia/developers-italia-api/internal/jsondecoder"
"github.com/italia/developers-italia-api/internal/middleware"
"github.com/italia/developers-italia-api/internal/models"
"github.com/italia/developers-italia-api/internal/webhooks"
"github.com/ansrivas/fiberprometheus/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/recover"
)
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)
}
gormDB, err := database.NewDatabase(common.EnvironmentConfig.Database)
if err != nil {
panic(err)
}
// Setup a goroutine acting as a worker for events sent to the
// EventChan channel.
//
// It dispatches the webhooks related to the event that occurred
// (es. Publisher creation, Software delete, etc.)
go func() {
for event := range models.EventChan {
if err := webhooks.DispatchWebhooks(event, gormDB); err != nil {
log.Println(err)
}
}
}()
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,
})
// Automatically recover panics in handlers
app.Use(recover.New())
// Use Fiber Rate API Limiter
if !common.EnvironmentConfig.IsTest() && common.EnvironmentConfig.MaxRequests != 0 {
app.Use(limiter.New(limiter.Config{
Max: common.EnvironmentConfig.MaxRequests,
LimiterMiddleware: limiter.SlidingWindow{},
KeyGenerator: func(ctx *fiber.Ctx) string {
return ctx.IP() + ctx.Get(fiber.HeaderXForwardedFor, "")
},
}))
}
app.Use(cache.New(cache.Config{
Next: func(ctx *fiber.Ctx) bool {
// Don't cache /status
return ctx.Route().Path == "/v1/status"
},
Methods: []string{fiber.MethodGet, fiber.MethodHead},
CacheControl: true,
Expiration: 10 * time.Second, //nolint:gomnd
KeyGenerator: func(ctx *fiber.Ctx) string {
return ctx.Path() + string(ctx.Context().QueryArgs().QueryString())
},
}))
if common.EnvironmentConfig.PasetoKey == nil {
log.Printf("PASETO_KEY not set, API will run in read-only mode")
common.EnvironmentConfig.PasetoKey = middleware.NewRandomPasetoKey()
}
prometheus := fiberprometheus.New(os.Args[0])
prometheus.RegisterAt(app, "/metrics")
app.Use(prometheus.Middleware)
app.Use(middleware.NewPasetoMiddleware(common.EnvironmentConfig))
setupHandlers(app, gormDB)
return app
}
func setupHandlers(app *fiber.App, gormDB *gorm.DB) {
publisherHandler := handlers.NewPublisher(gormDB)
softwareHandler := handlers.NewSoftware(gormDB)
statusHandler := handlers.NewStatus(gormDB)
logHandler := handlers.NewLog(gormDB)
publisherWebhookHandler := handlers.NewWebhook[models.Publisher](gormDB)
softwareWebhookHandler := handlers.NewWebhook[models.Software](gormDB)
//nolint:varnamelen
v1 := app.Group("/v1")
v1.Get("/publishers/webhooks", publisherWebhookHandler.GetResourceWebhooks)
v1.Post("/publishers/webhooks", publisherWebhookHandler.PostResourceWebhook)
v1.Get("/publishers/:id/webhooks", publisherWebhookHandler.GetSingleResourceWebhooks)
v1.Post("/publishers/:id/webhooks", publisherWebhookHandler.PostSingleResourceWebhook)
v1.Get("/publishers", publisherHandler.GetPublishers)
v1.Get("/publishers/:id", publisherHandler.GetPublisher)
v1.Post("/publishers", publisherHandler.PostPublisher)
v1.Patch("/publishers/:id", publisherHandler.PatchPublisher)
v1.Delete("/publishers/:id", publisherHandler.DeletePublisher)
v1.Get("/software/webhooks", softwareWebhookHandler.GetResourceWebhooks)
v1.Post("/software/webhooks", softwareWebhookHandler.PostResourceWebhook)
v1.Get("/software/:id/webhooks", softwareWebhookHandler.GetSingleResourceWebhooks)
v1.Post("/software/:id/webhooks", softwareWebhookHandler.PostSingleResourceWebhook)
v1.Get("/software", softwareHandler.GetAllSoftware)
v1.Get("/software/:id", softwareHandler.GetSoftware)
v1.Post("/software", softwareHandler.PostSoftware)
v1.Patch("/software/:id", softwareHandler.PatchSoftware)
v1.Delete("/software/:id", softwareHandler.DeleteSoftware)
v1.Get("/logs", logHandler.GetLogs)
v1.Get("/logs/:id<guid>", logHandler.GetLog)
v1.Post("/logs", logHandler.PostLog)
v1.Patch("/logs/:id<guid>", logHandler.PatchLog)
v1.Delete("/logs/:id<guid>", logHandler.DeleteLog)
v1.Get("/software/:id/logs", logHandler.GetSoftwareLogs)
v1.Post("/software/:id/logs", logHandler.PostSoftwareLog)
v1.Get("/status", statusHandler.GetStatus)
v1.Get("/webhooks/:id<guid>", publisherWebhookHandler.GetWebhook)
v1.Patch("/webhooks/:id<guid>", publisherWebhookHandler.PatchWebhook)
v1.Delete("/webhooks/:id<guid>", publisherWebhookHandler.DeleteWebhook)
}