mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-10-05 16:57:01 +08:00
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package signaling
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"go.uber.org/zap"
|
|
"riasc.eu/wice/pkg/crypto"
|
|
"riasc.eu/wice/pkg/pb"
|
|
)
|
|
|
|
var (
|
|
Backends = map[BackendType]*BackendPlugin{}
|
|
)
|
|
|
|
type BackendType string // URL schemes
|
|
|
|
type BackendFactory func(*BackendConfig, chan *pb.Event, *zap.Logger) (Backend, error)
|
|
|
|
type BackendPlugin struct {
|
|
New BackendFactory
|
|
Description string
|
|
}
|
|
|
|
type BackendConfig struct {
|
|
URI *url.URL
|
|
Community *crypto.Key
|
|
}
|
|
|
|
type Backend interface {
|
|
io.Closer
|
|
|
|
Publish(ctx context.Context, kp *crypto.KeyPair, msg *pb.SignalingMessage) error
|
|
Subscribe(ctx context.Context, kp *crypto.KeyPair) (chan *pb.SignalingMessage, error)
|
|
}
|
|
|
|
func NewBackend(cfg *BackendConfig, events chan *pb.Event) (Backend, error) {
|
|
typs := strings.SplitN(cfg.URI.Scheme, "+", 2)
|
|
typ := BackendType(typs[0])
|
|
|
|
p, ok := Backends[typ]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unknown backend type: %s", typ)
|
|
}
|
|
|
|
if len(typs) > 1 {
|
|
cfg.URI.Scheme = typs[1]
|
|
}
|
|
|
|
loggerName := fmt.Sprintf("backend.%s", typ)
|
|
logger := zap.L().Named(loggerName).With(zap.Any("backend", cfg.URI))
|
|
|
|
be, err := p.New(cfg, events, logger)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return be, nil
|
|
}
|