-
Notifications
You must be signed in to change notification settings - Fork 1
/
fiber.go
60 lines (53 loc) · 1.79 KB
/
fiber.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
package gosimplerest
import (
"database/sql"
"net/http"
"strings"
"github.com/franciscoescher/gosimplerest/handlers"
"github.com/franciscoescher/gosimplerest/resource"
"github.com/go-playground/validator/v10"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
)
func AddFiberHandlers(r *fiber.App, d *sql.DB, l *logrus.Logger, v *validator.Validate, resources []resource.Resource) *fiber.App {
h := AddRouteFunctions{
Post: FiberAddRouteFunc(r.Post),
Get: FiberAddRouteFunc(r.Get),
Put: FiberAddRouteFunc(r.Put),
Patch: FiberAddRouteFunc(r.Patch),
Delete: FiberAddRouteFunc(r.Delete),
Head: FiberAddRouteFunc(r.Head),
}
apf := func(name string, param string) string {
var sb strings.Builder
sb.WriteString(name)
sb.WriteString("/:")
sb.WriteString(param)
return sb.String()
}
AddHandlers(d, l, v, h, apf, resources)
return r
}
// FiberAddRouteType is the type of the function that fiber.App uses to add routes to the router.
// Example: r.Post, r.Get...
type FiberAddRouteType func(path string, handlers ...fiber.Handler) fiber.Router
// FiberAddRouteFunc uses the f function to add a route to the router,
// wrapping the handler to add params to request context.
func FiberAddRouteFunc(f FiberAddRouteType) AddRouteFunc {
return func(name string, h http.HandlerFunc) {
f(name, FiberHandlerWrapper(h))
}
}
// FiberHandlerWrapper wraps the handler function to a fiber handler.
// It adds params to request context.
func FiberHandlerWrapper(h http.HandlerFunc) fiber.Handler {
return func(c *fiber.Ctx) error {
params := make(map[string]string, 0)
for key, value := range c.AllParams() {
params[key] = value
}
hWithParams := handlers.AddParamsToHandlerFunc(h, params)
return adaptor.HTTPHandlerFunc(hWithParams)(c)
}
}