mercure: github.com/dunglas/mercure/hub Index | Files

package hub

import "github.com/dunglas/mercure/hub"

Index

Package Files

authorization.go bolt_transport.go config.go demo.go event.go hub.go log.go pipe.go publish.go server.go subscribe.go subscriber.go transport.go update.go

Variables

var ErrClosedPipe = errors.New("hub: read/write on closed Pipe")

ErrClosedPipe is returned by the Pipe's Write and Read methods after a call to Close.

var ErrClosedTransport = errors.New("hub: read/write on closed Transport")

ErrClosedTransport is returned by the Transport's Write and CreatePipe methods after a call to Close.

func Demo Uses

func Demo(w http.ResponseWriter, r *http.Request)

Demo exposes INSECURE Demo endpoints to test discovery and authorization mechanisms add a query parameter named "body" to define the content to return in the response's body add a query parameter named "jwt" set a "mercureAuthorization" cookie containing this token the Content-Type header will automatically be set according to the URL's extension

func InitConfig Uses

func InitConfig(v *viper.Viper)

InitConfig reads in config file and ENV variables if set.

func InitLogrus Uses

func InitLogrus()

InitLogrus configures the global logger

func SetConfigDefaults Uses

func SetConfigDefaults(v *viper.Viper)

SetConfigDefaults sets defaults on a Viper instance

func SetFlags Uses

func SetFlags(fs *pflag.FlagSet, v *viper.Viper)

SetFlags creates flags and bind them to Viper

func Start Uses

func Start()

Start is an helper method to start the Mercure Hub

func ValidateConfig Uses

func ValidateConfig(v *viper.Viper) error

ValidateConfig validates a Viper instance

type BoltTransport Uses

type BoltTransport struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

BoltTransport implements the TransportInterface using the Bolt database

func NewBoltTransport Uses

func NewBoltTransport(u *url.URL) (*BoltTransport, error)

NewBoltTransport create a new BoltTransport

func (*BoltTransport) Close Uses

func (t *BoltTransport) Close() error

Close closes the Transport

func (*BoltTransport) CreatePipe Uses

func (t *BoltTransport) CreatePipe(fromID string) (*Pipe, error)

CreatePipe returns a pipe fetching updates from the given point in time

func (*BoltTransport) Write Uses

func (t *BoltTransport) Write(update *Update) error

Write pushes updates in the Transport

type Event Uses

type Event struct {
    // The updates' data, encoded in the sever-sent event format: every line starts with the string "data: "
    // https://www.w3.org/TR/eventsource/#dispatchMessage
    Data string

    // The globally unique identifier corresponding to update
    ID  string

    // The event type, will be attached to the "event" field
    Type string

    // The reconnection time
    Retry uint64
}

Event is the actual Server Sent Event that will be dispatched

func (*Event) String Uses

func (e *Event) String() string

String serializes the event in a "text/event-stream" representation

type Hub Uses

type Hub struct {
    // contains filtered or unexported fields
}

Hub stores channels with clients currently subscribed and allows to dispatch updates

func NewHub Uses

func NewHub(v *viper.Viper) (*Hub, error)

NewHub creates a hub using the Viper configuration

func NewHubWithTransport Uses

func NewHubWithTransport(v *viper.Viper, t Transport) *Hub

NewHubWithTransport creates a hub

func (*Hub) PublishHandler Uses

func (h *Hub) PublishHandler(w http.ResponseWriter, r *http.Request)

PublishHandler allows publisher to broadcast updates to all subscribers

func (*Hub) Serve Uses

func (h *Hub) Serve()

Serve starts the HTTP server

func (*Hub) Stop Uses

func (h *Hub) Stop() error

Stop stops disconnect all connected clients

func (*Hub) SubscribeHandler Uses

func (h *Hub) SubscribeHandler(w http.ResponseWriter, r *http.Request)

SubscribeHandler create a keep alive connection and send the events to the subscribers

type LocalTransport Uses

type LocalTransport struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

LocalTransport implements the TransportInterface without database and simply broadcast the live Updates

func NewLocalTransport Uses

func NewLocalTransport() *LocalTransport

NewLocalTransport create a new LocalTransport

func (*LocalTransport) Close Uses

func (t *LocalTransport) Close() error

Close closes the Transport

func (*LocalTransport) CreatePipe Uses

func (t *LocalTransport) CreatePipe(fromID string) (*Pipe, error)

CreatePipe returns a pipe fetching updates from the given point in time

func (*LocalTransport) Write Uses

func (t *LocalTransport) Write(update *Update) error

Write pushes updates in the Transport

type Pipe Uses

type Pipe struct {
    // contains filtered or unexported fields
}

Pipe convey Update to reader in a closable chan

func NewPipe Uses

func NewPipe() *Pipe

NewPipe creates pipes

func (*Pipe) Close Uses

func (p *Pipe) Close()

Close closes the pipe

func (*Pipe) IsClosed Uses

func (p *Pipe) IsClosed() bool

IsClosed returns true if the pipe is closed

func (*Pipe) Read Uses

func (p *Pipe) Read(ctx context.Context) (*Update, error)

Read returns the next unfetch update from the pipe with a context

func (*Pipe) Write Uses

func (p *Pipe) Write(update *Update) bool

Write pushes updates in the pipe. Returns true is the update is pushed, false otherwise.

type Subscriber Uses

type Subscriber struct {
    AllTargets     bool
    Targets        map[string]struct{}
    Topics         []string
    RawTopics      []string
    TemplateTopics []*uritemplate.Template
    LastEventID    string
    // contains filtered or unexported fields
}

Subscriber represents a client subscribed to a list of topics

func NewSubscriber Uses

func NewSubscriber(allTargets bool, targets map[string]struct{}, topics []string, rawTopics []string, templateTopics []*uritemplate.Template, lastEventID string) *Subscriber

NewSubscriber creates a subscriber

func (*Subscriber) IsAuthorized Uses

func (s *Subscriber) IsAuthorized(u *Update) bool

IsAuthorized checks if the subscriber can access to at least one of the update's intended targets Don't forget to also call IsSubscribed

func (*Subscriber) IsSubscribed Uses

func (s *Subscriber) IsSubscribed(u *Update) bool

IsSubscribed checks if the subscriber has subscribed to this update Don't forget to also call IsAuthorized

type Transport Uses

type Transport interface {
    // Write pushes updates in the Transport
    Write(update *Update) error

    // CreatePipe returns a pipe fetching updates from the given point in time
    CreatePipe(fromID string) (*Pipe, error)

    // Close closes the Transport
    Close() error
}

Transport provides methods to read and write updates

func NewTransport Uses

func NewTransport(config *viper.Viper) (Transport, error)

NewTransport create a transport using the backend matching the given TransportURL

type Update Uses

type Update struct {
    // The target audience
    Targets map[string]struct{}

    // The topics' Internationalized Resource Identifier (RFC3987) (will most likely be URLs)
    // The first one is the canonical IRI, while next ones are alternate IRIs
    Topics []string

    // The Server-Sent Event to send
    Event
}

Update represents an update to send to subscribers

Package hub imports 36 packages (graph) and is imported by 1 packages. Updated 2020-04-05. Refresh now. Tools for package owners.