push

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package push sends Web Push notifications (RFC 8030, 8291 and 8292) to the phones and browsers that turned them on; it owns push_subscriptions (schema.sql) and calls the app's backoffice for the session. A notification is a nudge, never the only place something is said: the app shows the same thing.

Index

Constants

This section is empty.

Variables

View Source
var Hosts = []string{".push.apple.com", "fcm.googleapis.com", ".push.services.mozilla.com", ".notify.windows.com"}

Hosts are the push services a subscription may point at, by host or host suffix: the server posts to whatever endpoint a phone sends, so no other host is reached.

Functions

This section is empty.

Types

type Config

type Config struct {
	DB         *sql.DB
	Backoffice *backoffice.Backoffice
	// Keys is the zero value when notifications are off.
	Keys Keys
	// Subject is the contact the push services may reach the sender at: a mailto: or https: URL.
	Subject string
	// Welcome is sent to a phone as it subscribes; the subscription is kept only once it went through.
	Welcome Message
}

Config is what New needs; the app copies schema.sql into its migrations.

type Keys

type Keys struct {

	// Public is the uncompressed P-256 point, base64url: the applicationServerKey the phones subscribe with.
	Public string
	// contains filtered or unexported fields
}

Keys is the VAPID key pair (RFC 8292) the push services know this server by.

func NewKeys

func NewKeys() (Keys, string, error)

NewKeys makes a fresh VAPID key pair, and the private key as the -push.vapidPrivateKey flag holds it.

func ParseKeys

func ParseKeys(private string) (Keys, error)

ParseKeys reads a VAPID private key: the 32 octet P-256 scalar, base64url.

type Message

type Message struct {
	Title string `json:"title"`
	Body  string `json:"body"`
	URL   string `json:"url"`
	Tag   string `json:"tag,omitempty"`
}

Message is what the kit's service worker shows; Tag replaces an earlier notification with the same tag instead of stacking.

type Service

type Service struct {
	Client *http.Client
	Hosts  []string
	// contains filtered or unexported fields
}

Service sends the notifications and serves the subscription routes.

func New

func New(cfg Config) *Service

New makes the service; Client and Hosts may be replaced before use (tests point them at a local push service).

func (*Service) Mount

func (svc *Service) Mount(mux *http.ServeMux)

Mount adds GET /api/v1/push (the key) and PUT and DELETE /api/v1/push/subscription, behind the session check.

func (*Service) On

func (svc *Service) On() bool

On reports whether a VAPID key is set: without one, nothing is sent and subscriptions are refused.

func (*Service) Public

func (svc *Service) Public() string

Public returns the key the phones subscribe with, "" when notifications are off.

func (*Service) Send

func (svc *Service) Send(ctx context.Context, userID string, m Message) (int, error)

Send notifies every phone of a user and returns how many took the message; a failed phone is logged and skipped, a phone its push service forgot is deleted.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL