mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-30 19:06:33 +02:00
69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package mux
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pomerium/pomerium/pkg/zero/connect"
|
|
)
|
|
|
|
type config struct {
|
|
onConnected func(ctx context.Context)
|
|
onDisconnected func(ctx context.Context)
|
|
onBundleUpdated func(ctx context.Context, key string)
|
|
onBootstrapConfigUpdated func(ctx context.Context)
|
|
onTelemetryRequested func(ctx context.Context, req *connect.TelemetryRequest)
|
|
}
|
|
|
|
// WatchOption allows to specify callbacks for various events
|
|
type WatchOption func(*config)
|
|
|
|
// WithOnConnected sets the callback for when the connection is established
|
|
func WithOnConnected(onConnected func(context.Context)) WatchOption {
|
|
return func(cfg *config) {
|
|
cfg.onConnected = onConnected
|
|
}
|
|
}
|
|
|
|
// WithOnDisconnected sets the callback for when the connection is lost
|
|
func WithOnDisconnected(onDisconnected func(context.Context)) WatchOption {
|
|
return func(cfg *config) {
|
|
cfg.onDisconnected = onDisconnected
|
|
}
|
|
}
|
|
|
|
// WithOnBundleUpdated sets the callback for when the bundle is updated
|
|
func WithOnBundleUpdated(onBundleUpdated func(ctx context.Context, key string)) WatchOption {
|
|
return func(cfg *config) {
|
|
cfg.onBundleUpdated = onBundleUpdated
|
|
}
|
|
}
|
|
|
|
// WithOnBootstrapConfigUpdated sets the callback for when the bootstrap config is updated
|
|
func WithOnBootstrapConfigUpdated(onBootstrapConfigUpdated func(context.Context)) WatchOption {
|
|
return func(cfg *config) {
|
|
cfg.onBootstrapConfigUpdated = onBootstrapConfigUpdated
|
|
}
|
|
}
|
|
|
|
func WithOnTelemetryRequested(onTelemetryRequested func(context.Context, *connect.TelemetryRequest)) WatchOption {
|
|
return func(cfg *config) {
|
|
cfg.onTelemetryRequested = onTelemetryRequested
|
|
}
|
|
}
|
|
|
|
func newConfig(opts ...WatchOption) *config {
|
|
cfg := &config{}
|
|
for _, opt := range []WatchOption{
|
|
WithOnConnected(func(_ context.Context) {}),
|
|
WithOnDisconnected(func(_ context.Context) {}),
|
|
WithOnBundleUpdated(func(_ context.Context, _ string) {}),
|
|
WithOnBootstrapConfigUpdated(func(_ context.Context) {}),
|
|
WithOnTelemetryRequested(func(_ context.Context, _ *connect.TelemetryRequest) {}),
|
|
} {
|
|
opt(cfg)
|
|
}
|
|
for _, opt := range opts {
|
|
opt(cfg)
|
|
}
|
|
return cfg
|
|
}
|