/
web.go
53 lines (41 loc) · 1.14 KB
/
web.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
package web
import (
"github.com/b2wdigital/restQL-golang/v6/internal/platform/web/middleware"
"github.com/b2wdigital/restQL-golang/v6/pkg/restql"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
type appOptions struct {
MiddlewareDecorator *middleware.Decorator
}
type handler func(ctx *fasthttp.RequestCtx) error
type app struct {
router *router.Router
log restql.Logger
options appOptions
}
func newApp(log restql.Logger, o appOptions) app {
r := router.New()
r.NotFound = func(ctx *fasthttp.RequestCtx) { ctx.Response.SetBodyString("There is nothing here. =/") }
return app{router: r, log: log, options: o}
}
func (a app) Handle(method, url string, handler handler) {
fn := func(ctx *fasthttp.RequestCtx) {
err := handler(ctx)
if err != nil {
a.log.Error("handler has an error", err)
if err := RespondError(ctx, err, errToStatusCode); err != nil {
a.log.Error("failed to send error response", err)
}
}
}
a.router.Handle(method, url, fn)
}
func (a app) RequestHandler() fasthttp.RequestHandler {
h := a.router.Handler
md := a.options.MiddlewareDecorator
if md != nil {
h = md.Apply(h)
}
return h
}