Published an OpportunityMarksUpdated event when an opportunity's marks change over the API

This commit is contained in:
bwilliams committed 2026-10-16 13:19:58 +01:00
1 parent 70d62f280e
commit 848edc73e2
7 files changed
+203 -34

No files matched your search

+25 -7
View File
@@ -19,6 +19,7 @@ import (
"git.avsdev.uk/Go/job-search/internal/api"
"git.avsdev.uk/Go/job-search/internal/config"
"git.avsdev.uk/Go/job-search/internal/events"
"git.avsdev.uk/Go/job-search/internal/logging"
"git.avsdev.uk/Go/job-search/internal/middleware"
"git.avsdev.uk/Go/job-search/internal/store"
@@ -72,6 +73,16 @@ func WithStore(store *store.Store) Option {
}
}
// WithEvents connects the server to bus: every event published to it is
// pushed to the server's WebSocket clients (as websocket.TypeEvent
// messages), and the server's API publishes its changes to it (see
// api.WithEvents).
func WithEvents(bus *events.Bus) Option {
return func(s *Server) {
s.events = bus
}
}
// WithMiddleware appends middleware to a group. This is the generic hook
// (auth, metrics, tracing, custom header rules, ...).
func WithMiddleware(group Group, middlewares ...Middleware) Option {
@@ -120,6 +131,9 @@ type Server struct {
hub *websocket.Hub
// store is the opportunities' database, or nil for none.
store *store.Store
// events is where events are pushed from and the API publishes to, or
// nil for none.
events *events.Bus
// api is the REST API mounted under /api, built from the options.
api *api.API
started time.Time
@@ -142,7 +156,10 @@ func New(conf *config.JSCServer, opts ...Option) (*Server, error) {
for _, option := range opts {
option(server)
}
server.api = api.New(api.WithStore(server.store))
server.api = api.New(
api.WithStore(server.store),
api.WithEvents(server.events),
)
var err error
server.trusted, err = util.ParseNetPrefixes(conf.Proxy.TrustedProxies)
@@ -190,6 +207,13 @@ func New(conf *config.JSCServer, opts ...Option) (*Server, error) {
server.handler = logging.AccessLog(server.accessLog, true, server.handler)
}
// Subscribed last, so a server that failed to build is never pushed to.
if server.events != nil {
server.events.Subscribe(func(event events.Event) {
server.hub.PushEvent(event.Type, event.Payload)
})
}
return server, nil
}
@@ -210,12 +234,6 @@ func (s *Server) logger() *slog.Logger {
// Handler exposes the root handler (useful for httptest).
func (s *Server) Handler() http.Handler { return s.handler }
// PushEvent sends an event to every client of the server's WebSocket hub
// (see websocket.Hub.PushEvent).
func (s *Server) PushEvent(eventType string, payload any) {
s.hub.PushEvent(eventType, payload)
}
// Run listens and serves until ctx is cancelled, then shuts down gracefully.
// With TLS, it also serves an HTTP to HTTPS redirect if tls.redirect_http_port
// is set.