mirror of
https://github.com/jhillyerd/inbucket.git
synced 2026-10-29 13:42:30 +00:00
47239c08eb
The black-box monitor tests showed socketv1_controller.go and socketv2_controller.go were near-identical: same constants (identical values), upgrader, read/write pumps, Close, and handler bodies, differing only in the event payload shape and v1 dropping deletions. Extract the shared machinery into socket_controller.go: - msgListener[T] generic listener with toStored/toDeleted payload transforms; a nil toDeleted preserves the socketv1 behavior of ignoring deletions - socketReadPump / socketWritePump free functions (WriteJSON is payload-type agnostic) - serveMonitor shared handler body (upgrade, gauge tracking, listener registration, pump wiring) The version files shrink to their listener constructors and the four exported handlers, whose signatures are unchanged. v1 payloads are now shaped at enqueue time rather than write time; the JSON on the wire is identical, pinned by the commit-1 tests, which pass unmodified. Adds direct unit tests for the extracted listener logic: watches() filtering, Receive/Delete filtering + payload shaping, nil-toDeleted deletion dropping, and sequential Close idempotency.
50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package rest
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/inbucket/inbucket/v3/pkg/extension/event"
|
|
"github.com/inbucket/inbucket/v3/pkg/msghub"
|
|
"github.com/inbucket/inbucket/v3/pkg/rest/model"
|
|
"github.com/inbucket/inbucket/v3/pkg/server/web"
|
|
)
|
|
|
|
// newMsgListenerV2 creates a v2 API listener which relays message-stored and message-deleted
|
|
// events.
|
|
func newMsgListenerV2(hub *msghub.Hub, mailbox string) *msgListener[*model.JSONMonitorEventV2] {
|
|
return newMsgListener(hub, mailbox,
|
|
func(msg event.MessageMetadata) *model.JSONMonitorEventV2 {
|
|
return &model.JSONMonitorEventV2{
|
|
Variant: "message-stored",
|
|
Header: metadataToHeader(&msg),
|
|
}
|
|
},
|
|
func(mailbox string, id string) *model.JSONMonitorEventV2 {
|
|
return &model.JSONMonitorEventV2{
|
|
Variant: "message-deleted",
|
|
Identifier: &model.JSONMessageIDV2{
|
|
Mailbox: mailbox,
|
|
ID: id,
|
|
},
|
|
}
|
|
})
|
|
}
|
|
|
|
// MonitorAllMessagesV2 is a web handler which upgrades the connection to a websocket and notifies
|
|
// the client of all messages received.
|
|
func MonitorAllMessagesV2(
|
|
w http.ResponseWriter, req *http.Request, ctx *web.Context) error {
|
|
return serveMonitor(w, req, ctx.MsgHub, "", newMsgListenerV2)
|
|
}
|
|
|
|
// MonitorMailboxMessagesV2 is a web handler which upgrades the connection to a websocket and
|
|
// notifies the client of messages received by a particular mailbox.
|
|
func MonitorMailboxMessagesV2(
|
|
w http.ResponseWriter, req *http.Request, ctx *web.Context) error {
|
|
name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return serveMonitor(w, req, ctx.MsgHub, name, newMsgListenerV2)
|
|
}
|