-
Notifications
You must be signed in to change notification settings - Fork 12
/
routing_strategy.go
52 lines (43 loc) · 1.12 KB
/
routing_strategy.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
package fiber
import "context"
// RoutingStrategy picks up primary route and zero or more fallbacks
// from the map of router routes
type RoutingStrategy interface {
Type
// req - Incoming request (so the route can be selected based on the request)
// routes - map of all possible routes
SelectRoute(ctx context.Context,
req Request,
routes map[string]Component,
) (route Component, fallbacks []Component, labels Labels, err error)
}
type baseRoutingStrategy struct {
RoutingStrategy
BaseFiberType
}
type routesOrderResponse struct {
Components []Component
Labels Labels
Err error
}
func (s *baseRoutingStrategy) getRoutesOrder(
ctx context.Context,
req Request,
routes map[string]Component,
) <-chan routesOrderResponse {
out := make(chan routesOrderResponse)
go func() {
route, fallbacks, labels, err := s.SelectRoute(ctx, req, routes)
// Combine preferred route with the fallbacks
routes := fallbacks
if route != nil {
routes = append([]Component{route}, routes...)
}
out <- routesOrderResponse{
Components: routes,
Err: err,
Labels: labels,
}
}()
return out
}