-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbus-handler.go
58 lines (51 loc) · 1.24 KB
/
bus-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
58
package bifrost_http
import (
"net/http"
"github.com/aperturerobotics/controllerbus/bus"
)
// BusHandler implements http.Handler by calling LookupHTTPHandler.
type BusHandler struct {
// b is the bus to use for lookups
b bus.Bus
// clientID is the client id to use for lookups
clientID string
// notFoundIfIdle indicates to return 404 not found if the lookup is idle
notFoundIfIdle bool
}
// NewBusHandler constructs a new bus-backed HTTP handler.
func NewBusHandler(b bus.Bus, clientID string, notFoundIfIdle bool) *BusHandler {
return &BusHandler{
b: b,
clientID: clientID,
notFoundIfIdle: notFoundIfIdle,
}
}
// ServeHTTP serves the http request.
func (h *BusHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()
handler, _, handlerRef, err := ExLookupFirstHTTPHandler(
ctx,
h.b,
req.Method,
req.URL,
"",
h.notFoundIfIdle,
nil,
)
if handlerRef != nil {
defer handlerRef.Release()
}
if err != nil {
rw.WriteHeader(500)
_, _ = rw.Write([]byte(err.Error()))
return
}
if handlerRef == nil {
rw.WriteHeader(404)
_, _ = rw.Write([]byte("404 not found"))
return
}
handler.ServeHTTP(rw, req)
}
// _ is a type assertion
var _ http.Handler = ((*BusHandler)(nil))