mailbus

package module
v0.0.0-...-8164673 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 5 Imported by: 0

README

Mailbus

Mailbus is a self-hosted email newsletter that allows:

  • A blog visitor to subscribe to the newsletter
  • Blog author to send an email to all subscribers
  • A subscriber to unsubscribe from the newsletter

API Design

  • POST /subscriptions: sign up a new subscriber
  • GET /subscriptions/confirm: subscriber consent
  • GET /unsubscribe: unsubscribe from the newsletter

Data Schema

CREATE TABLE subscriptions (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    email         TEXT NOT NULL UNIQUE,
    subscribed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE subscription_tokens (
    subscription_token TEXT NOT NULL,
    subscriber_id      INTEGER NOT NULL REFERENCES subscriptions (id),

    PRIMARY KEY (subscription_token)
);

Sequence Diagram

sequenceDiagram
    Visitor ->> Blog: Enter an email address
    Blog ->> Blog: Validate email address
    Blog ->> Mailbus: POST /subscriptions
    Mailbus ->> Visitor: Send confirmation email
    Visitor ->> Mailbus: GET /subscriptions/confirm 

Documentation

Index

Constants

View Source
const (
	ErrInvalid      = "invalid"
	ErrUnauthorized = "unauthorized"
	ErrForbidden    = "forbidden"
	ErrNotFound     = "not_found"
	ErrConflict     = "conflict"
	ErrInternal     = "internal"
)
View Source
const (
	StatusPendingConfirmation = "pending_confirmation"
	StatusActive              = "active"
	StatusUnsubscribed        = "unsubscribed"
)

Subscribe status

Variables

This section is empty.

Functions

func ErrorCode

func ErrorCode(err error) string

func ErrorMessage

func ErrorMessage(err error) string

Types

type Config

type Config struct {
	DB struct {
		Type string // "bolt", "sqlite", etc.
		Path string
	}

	HTTP struct {
		Addr string
	}

	SMTP struct {
		Host     string
		Port     int
		Username string
		Password string
	}

	Newsletter struct {
		From      string
		Frequency int
		Cron      struct {
			Spec string
		}
		Product struct {
			Name string
		}
		HMAC struct {
			Secret string
		}
	}

	Sentry struct {
		DSN string
	}

	AMQP struct {
		URL string
	}
}

Config represents the main config

type Database

type Database interface {
	Open() error
	Close() error
}

type EmailNewsletterRequest

type EmailNewsletterRequest struct {
	Subject string `json:"subject"`
	Body    string `json:"body"`
}

type Error

type Error struct {
	Code    string
	Message string
	Op      string
	Err     error
}

func (*Error) Error

func (e *Error) Error() string

type NewsletterService

type NewsletterService interface {
	SendConfirmationEmail(to, url, token string) error
	SendThankYouEmail(to string) error
	SendNewsletter(subscribers []Subscriber, subject, body string)
	GenerateNewUUID() string
	GetHMACSecret() string
}

NewsletterService is the interface that wraps methods related to SMTP

type QueueService

type QueueService interface {
	Consume(ctx context.Context, topic string) (<-chan []byte, error)
}

type Subscriber

type Subscriber struct {
	ID           int    `storm:"id,increment"`
	Email        string `storm:"unique"`
	Status       string `storm:"index"`
	SubscribedAt time.Time
}

Subscriber represents a subscriber

type Subscription

type Subscription struct {
	Email  string
	Status string
	Token  string
}

func NewSubscription

func NewSubscription(email, status, token string) *Subscription

NewSubscription returns new subscriber

type SubscriptionRequest

type SubscriptionRequest struct {
	URL   string `json:"url"`
	Email string `json:"email"`
}

type SubscriptionService

type SubscriptionService interface {
	FindByEmail(email string) (*Subscriber, error)
	Insert(s *Subscription) error
	Update(email, token string) error
	FindByStatus(status string) ([]Subscriber, error)
	Confirm(token string) (string, error)
	Unsubscribe(email string) error
}

SubscriptionService is the interface that wraps methods related to subscribe function

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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