-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathrouter.go
59 lines (46 loc) · 1.23 KB
/
router.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
package server
import (
"context"
"fmt"
"net/http"
"github.com/gojektech/weaver"
"github.com/pkg/errors"
"github.com/vulcand/route"
)
type Router struct {
route.Router
loader RouteLoader
}
type apiName string
func (router *Router) Route(req *http.Request) (*weaver.ACL, error) {
rt, err := router.Router.Route(req)
if err != nil {
return nil, errors.Wrapf(err, "failed to find route with url: %s", req.URL)
}
if rt == nil {
return nil, errors.WithStack(fmt.Errorf("route not found: %s", req.URL))
}
acl, ok := rt.(*weaver.ACL)
if !ok {
return nil, errors.WithStack(fmt.Errorf("error in casting %v to acl", rt))
}
return acl, nil
}
func NewRouter(loader RouteLoader) *Router {
return &Router{
Router: route.New(),
loader: loader,
}
}
func (router *Router) WatchRouteUpdates(routeSyncCtx context.Context) {
router.loader.WatchRoutes(routeSyncCtx, router.upsertACL, router.deleteACL)
}
func (router *Router) BootstrapRoutes(ctx context.Context) error {
return router.loader.BootstrapRoutes(ctx, router.upsertACL)
}
func (router *Router) upsertACL(acl *weaver.ACL) error {
return router.UpsertRoute(acl.Criterion, acl)
}
func (router *Router) deleteACL(acl *weaver.ACL) error {
return router.RemoveRoute(acl.Criterion)
}