Files
summercms/modules/lighthouse/centrifugo/driver.go
Jakub Zych cada7a4442 feat(11-03): add the lighthouse realtime package and its Centrifugo driver
- lighthouse: Service/From with realtime.driver selection, RegisterDriver
  registry, null/log/memory drivers, Route/Surface/Mount, users and actors
- centrifugo: HTTP API client (apikey header, 2xx success, no request
  without a key), five-generator HS256 TokenIssuer, TokenHandler with the
  WinterCMS 401/503 bodies
- module README and root modules table row
2026-09-30 12:18:11 +02:00

78 lines
2.2 KiB
Go

package centrifugo
import (
"context"
"encoding/json"
"net/http"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/lighthouse"
)
// DriverName is the realtime.driver value of this driver.
const DriverName = "centrifugo"
func init() {
lighthouse.RegisterDriver(DriverName, func(app *backpack.App, svc *lighthouse.Service) (lighthouse.Driver, error) {
var cfg Config
if app != nil {
cfg = LoadConfig(app.Config)
} else {
cfg = LoadConfig(nil)
}
return NewDriver(svc, cfg, nil), nil
})
}
// Driver is the Centrifugo lighthouse.Driver: it publishes through Client
// and declares the token route (UserAuth) and the subscribe proxy route
// (ServerToServer).
type Driver struct {
svc *lighthouse.Service
cfg Config
client *Client
issuer *TokenIssuer
}
// NewDriver builds the driver for svc from cfg. hc may be nil (see
// NewClient).
func NewDriver(svc *lighthouse.Service, cfg Config, hc *http.Client) *Driver {
c := NewClient(cfg, hc)
c.log = svc.Logger()
return &Driver{
svc: svc,
cfg: cfg,
client: c,
issuer: NewTokenIssuer(cfg.TokenSecret, cfg.TokenTTL),
}
}
// Name returns "centrifugo".
func (d *Driver) Name() string { return DriverName }
// Config returns the driver's configuration.
func (d *Driver) Config() Config { return d.cfg }
// Client returns the HTTP API client.
func (d *Driver) Client() *Client { return d.client }
// Issuer returns the token issuer.
func (d *Driver) Issuer() *TokenIssuer { return d.issuer }
// Publish sends event to one channel through Client.Publish.
func (d *Driver) Publish(ctx context.Context, channel, event string, payload json.RawMessage) error {
return d.client.Publish(ctx, channel, event, payload)
}
// Broadcast sends event to several channels through Client.Broadcast.
func (d *Driver) Broadcast(ctx context.Context, channels []string, event string, payload json.RawMessage) error {
return d.client.Broadcast(ctx, channels, event, payload)
}
// Routes returns GET token_path (UserAuth, TokenHandler).
func (d *Driver) Routes() []lighthouse.Route {
return []lighthouse.Route{
{Name: "token", Method: http.MethodGet, Path: d.cfg.TokenPath, Surface: lighthouse.UserAuth, Handler: TokenHandler(d.svc, d.issuer)},
}
}