-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbus-http-handler.go
57 lines (53 loc) · 1.25 KB
/
bus-http-handler.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
package bifrost_http
import (
"context"
"net/http"
"net/url"
"github.com/aperturerobotics/controllerbus/bus"
)
// NewBusHTTPHandlerBuilder constructs a HTTPHandlerBuilder which looks up the handler on the bus.
//
// baseMethod can be empty to allow any
func NewBusHTTPHandlerBuilder(b bus.Bus, baseMethod string, baseURL *url.URL, clientID string, notFoundIfIdle bool) HTTPHandlerBuilder {
return func(ctx context.Context, released func()) (http.Handler, func(), error) {
handler, _, handlerRef, err := ExLookupFirstHTTPHandler(
ctx,
b,
baseMethod,
baseURL,
clientID,
notFoundIfIdle,
released,
)
if err != nil {
return nil, nil, err
}
if handlerRef == nil {
return nil, nil, nil
}
return handler, handlerRef.Release, nil
}
}
// NewBusHTTPHandler constructs a HTTPHandler which looks up the HTTP
// handler on the bus when at least one request is active.
//
// baseMethod can be empty to allow any
// baseURL is the URL to use for the client lookup.
func NewBusHTTPHandler(
ctx context.Context,
b bus.Bus,
baseMethod string,
baseURL *url.URL,
clientID string,
notFoundIfIdle bool,
) *HTTPHandler {
return NewHTTPHandler(
ctx,
NewBusHTTPHandlerBuilder(
b,
baseMethod,
baseURL,
clientID,
notFoundIfIdle,
))
}