Documentation
¶
Index ¶
- Variables
- func Logger() zerolog.Logger
- type Hub
- func (h *Hub) AddSniffer(topic string, sniffer HubTopicSnifferFunc)
- func (h *Hub) AddValidator(validator HubSubValidatorFunc)
- func (h *Hub) GetTopics() (topics []string)
- func (h Hub) HubUrl() string
- func (h *Hub) Publish(topic, contentType string, content []byte)
- func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type HubOption
- func HubAllowPostBodyAsContent(enable bool) HubOption
- func HubExposeTopics(enable bool) HubOption
- func HubWithCleanupInterval(interval time.Duration) HubOption
- func HubWithHashFunction(hashFunction string) HubOption
- func HubWithLeaseSettings(minLease, maxLease, defaultLease time.Duration) HubOption
- func HubWithRetryLimits(retryLimit int, retryInterval time.Duration) HubOption
- func HubWithUserAgent(userAgent string) HubOption
- type HubSubValidatorFunc
- type HubSubscription
- type HubTopicSnifferFunc
- type Publisher
- type PublisherOption
- type SubscribeCallback
- type Subscriber
- type SubscriberOption
- type SubscriberSubscription
Constants ¶
This section is empty.
Variables ¶
var ( // a non 2xx status code was returned when getting topic content ErrNon2xxGettingContent = errors.New( "a non 2xx status code was returned when getting topic content") // a non 2xx status code was returned when posting content to a subscriber ErrNon2xxPostingContent = errors.New( "a non 2xx status code was returned when posting content to a subscriber") )
var ( // topic not discoverable ErrTopicNotDiscoverable = errors.New("topic not discoverable") // hub returned an invalid status code on subscription or unsubscription request ErrNon2xxOnSubReq = errors.New("hub returned an invalid status code on subscription or unsubscription request") )
var ( // hub returned a non 2xx status code on publish request ErrNon2xxOnPubReq = errors.New("hub returned a non 2xx status code on publish request") )
Functions ¶
Types ¶
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
A Hub is a websub hub.
func (*Hub) AddSniffer ¶
func (h *Hub) AddSniffer(topic string, sniffer HubTopicSnifferFunc)
AddSniffer allows one to "sniff" publishes, receiving events as if they were subscribers.
If an emptry string is provided as the topic, all publishes are sniffed.
func (*Hub) AddValidator ¶
func (h *Hub) AddValidator(validator HubSubValidatorFunc)
AddValidator adds a validator for subscription requests. Multiple validators can exist on one hub.
All subscriptions are accepted by default.
func (*Hub) GetTopics ¶
Returns an array of all topics.
Includes topics with no subscribers, or no publishes.
type HubOption ¶
type HubOption func(*Hub)
A HubOption specifies an option for a hub.
func HubAllowPostBodyAsContent ¶ added in v0.2.0
HubAllowPostBodyAsContent allows publishers to post content as the body of the POST request if they provide hub.content = "body" and hub.mode = "publish". In this case, the Content-Type of the post request is used when distributing publish events.
NOTE: Because of the lack of authentication for publishers, this allows any machine with internet access to the hub to publish any content under any topic. Use with caution.
func HubExposeTopics ¶ added in v0.2.0
HubExposeTopics enables a /topics endpoint that lists all available/active topics.
func HubWithCleanupInterval ¶ added in v0.2.0
HubWithCleanupInterval sets the interval expired subscriptions are removed from the memory
func HubWithHashFunction ¶ added in v0.2.0
HubWithHashFunction sets the hash function used to compute hub signatures for subscriptions with a secret.
One of "sha1", "sha256", "sha384", "sha512"
Default is "sha1" for compatability, however it is insecure.
func HubWithLeaseSettings ¶ added in v0.2.0
HubWithLeaseSettings sets the minimum, maximum, and default lease for a subscription on a hub
When a requested lease is outside of the allowed range, the lease becomes pinned to the minimum or maximum value. If a subscriber doesn't provide a lease length, the default lease length is used.
- Default minimum lease is 5 minutes
- Default maximum lease is 720 hours (30 days)
- Default default lease is 240 hours (10 days)
func HubWithRetryLimits ¶ added in v0.2.0
HubWithRetryLimits sets the retry limits for a hub.
Defaults to 5 retries, and a one minute interval.
func HubWithUserAgent ¶ added in v0.2.0
HubWithUserAgent sets the user-agent for a hub
Default user agent is "go-websub-hub"
type HubSubValidatorFunc ¶ added in v0.2.0
type HubSubValidatorFunc func(sub *HubSubscription) (ok bool, reason string)
HubSubValidatorFunc validates subscription requests.
The validation stops as soon as one validator returns ok=false. The provided reason is sent to the subscriber telling them their subscription request was denied.
The expiry date will not be set by the time the validators are called.
type HubSubscription ¶ added in v0.2.0
type HubSubscription struct { // The HTTP callback to the subscriber. Callback string // The topic URL the subscriber is subscribing to. Topic string // The number of seconds the subscription will be active. LeaseLength int // The date/time the subscription will expire. // Is not set at validation time. Expires time.Time // The secret provided by the subscriber. Empty string means none. Secret string }
an HubSubscription is a subscription used in the context of a Hub.
type HubTopicSnifferFunc ¶ added in v0.2.0
A topic sniffer sniffs on topics as if it was a subscriber.
type Publisher ¶
type Publisher struct {
// contains filtered or unexported fields
}
func NewPublisher ¶
func NewPublisher(baseUrl, hubUrl string, options ...PublisherOption) *Publisher
func (Publisher) BaseUrl ¶
BaseUrl returns the base URL of this publisher (with any trailing slash trimmed)
func (*Publisher) Publish ¶
Publish will send a publish request to the hub.
If the topic URL starts with this publisher's base URL, the publisher will return the content on HTTP GET requests to that url.
func (*Publisher) ServeHTTP ¶
func (p *Publisher) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the content that has been published to this publisher, and advertises topic and hub urls in Link headers.
Only topics published with a URL that starts with the base URL are advertised.
type PublisherOption ¶
type PublisherOption func(p *Publisher)
func PublisherAdvertiseInvalidTopics ¶ added in v0.2.0
func PublisherAdvertiseInvalidTopics(enabled bool) PublisherOption
PublisherAdvertiseInvalidTopics will advertise all topics with Link headers and return a 200 OK status as if they have already been published to with blank content.
func PublisherWithPostBodyAsContent ¶ added in v0.2.0
func PublisherWithPostBodyAsContent(enabled bool) PublisherOption
PublisherWithPostBodyAsContent sends what is normally the body as the query parameters, and sends the content as the body. Also adds hub.content="body" in the query parameters.
Important: If the hub does not have this enabled, you will be unable to publish.
type SubscribeCallback ¶
type SubscribeCallback func(sub *SubscriberSubscription, contentType string, body io.Reader)
a SubscribeCallback is called when a subscriber receives a publish to the related topic.
type Subscriber ¶
type Subscriber struct {
// contains filtered or unexported fields
}
func NewSubscriber ¶
func NewSubscriber(baseUrl string, options ...SubscriberOption) *Subscriber
NewSubscriber creates a new subscriber with the specified options.
func (Subscriber) BaseUrl ¶
func (s Subscriber) BaseUrl() string
Returns the base URL for this subscribers callback URLs.
func (*Subscriber) ServeHTTP ¶
func (s *Subscriber) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles all incoming HTTP requests, following websub spec.
func (*Subscriber) Subscribe ¶
func (s *Subscriber) Subscribe(topicUrl, secret string, callback SubscribeCallback) (*SubscriberSubscription, error)
subscribes to updates to the topicUrl, verifying using the secret
If the secret is an empty string, it is omitted.
When updates happen, the callback is called.
func (*Subscriber) Unsubscribe ¶
func (s *Subscriber) Unsubscribe(sub *SubscriberSubscription) error
Unsubscribe requests the hub to stop sending updates. Already pending unsubscriptions are ignored.
All events received in the meantime will not be fulfilled.
type SubscriberOption ¶
type SubscriberOption func(*Subscriber)
func SubscriberWithBaseUrl ¶ added in v0.2.0
func SubscriberWithBaseUrl(baseUrl string) SubscriberOption
SubscriberWithBaseUrl sets the baseUrl for a subscriber
func SubscriberWithLeaseLength ¶ added in v0.2.0
func SubscriberWithLeaseLength(LeaseLength time.Duration) SubscriberOption
SubscriberWithLeaseLength sets the LeaseLength for a subscriber
Default lease length is 10 days
type SubscriberSubscription ¶ added in v0.2.0
type SubscriberSubscription struct { // Topic URL for this subscription. // Not always equal to the passed topic url. Topic string // Hub URL this subscription is from. Hub string // Secret string used for verifying the hub is the // sender of the subscription. Secret string // The date/time this subscription expires. Expires time.Time // Internal ID for this subscription. Part of the callback URL. Id string // contains filtered or unexported fields }
a SubscriberSubscription is a subscription in the context of a Subscriber.
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
publisher
Interactive example of publishing content using go-websub.
|
Interactive example of publishing content using go-websub. |
sniffer
Expanded upon single-port, added sniffer
|
Expanded upon single-port, added sniffer |
topic-announcement
Adapted from sniffer and added topic announcements
|
Adapted from sniffer and added topic announcements |