app

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package app is the application layer of the quickstart service: its commands, its queries, the task_list Projection and the notify_assignee Reactor. The same App runs in memory and on Postgres; only the Store, UnitOfWork, Runtime and ReadModel passed to New differ.

Index

Constants

View Source
const TrackerSource = "tracker"

TrackerSource is the Inbox Source of the issue tracker's webhooks.

Variables

View Source
var ErrInvalidAddress = errors.New("app: invalid mail address")

ErrInvalidAddress is returned by a Mailer for an address it can never deliver to.

Functions

func Codec

func Codec() *eventstore.Codec

Codec registers the service's events. Every service owns its Codec.

func FormatPosition

func FormatPosition(c eventstore.Cursor) string

FormatPosition encodes a CommandResult.Position for an API client.

func Metadata

func Metadata(tenant string, next http.Handler) http.Handler

Metadata puts the request metadata into the request ctx: the fixed Tenant of a single-tenant service, the Actor and the request ID, which becomes the Correlation of the events the request writes (ADR-0010).

The X-User header stands in for real authentication. With pkg/connectrpc's jwtauth interceptor, Claims.UserID is the Actor and Claims.TenantID the Tenant without any of this.

func NotifyAssignee

func NotifyAssignee(codec *eventstore.Codec, tasks *Tasks, mailer Mailer) *consumer.Router

NotifyAssignee builds the handler of the notify_assignee Reactor: it mails the assignee of an opened Task, then records that with a follow-up command. Delivery is at least once, so the mail carries the event ID as its idempotency key and MarkNotified is idempotent.

func ParsePosition

func ParsePosition(s string) (eventstore.Cursor, error)

ParsePosition decodes a position from FormatPosition. The empty string is the zero Cursor.

func ReceiveTrackerWebhook

func ReceiveTrackerWebhook(store *inbox.Store) http.Handler

ReceiveTrackerWebhook stores a tracker webhook in the Inbox and answers 202 at once; the Worker of TrackerSource handles it. A redelivered webhook is deduplicated by its delivery ID.

func TaskListV1

func TaskListV1(codec *eventstore.Codec, rm ReadModel) *consumer.Router

TaskListV1 builds version 1 of the task_list Projection. Every write notifies subscribers, so a live UI can refetch the task.

Types

type App

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

App runs the service's commands and queries.

func New

func New(cfg Config) (*App, error)

New wires the Repository, the task_list Projection and the notify_assignee Reactor.

func (*App) CompleteTask

func (a *App) CompleteTask(ctx context.Context, id todo.TaskID) (eventstore.CommandResult, error)

CompleteTask marks a Task as done.

func (*App) Consumers

func (a *App) Consumers() consumer.Group

Consumers returns every Consumer of the App, to run in one Group.

func (*App) HandleTrackerWebhook

func (a *App) HandleTrackerWebhook(ctx context.Context, d inbox.Delivery) error

HandleTrackerWebhook is the inbox.Handler of TrackerSource: it completes the Task the webhook names.

func (*App) ListTasks

func (a *App) ListTasks(ctx context.Context, after eventstore.Cursor) ([]Task, consumer.Freshness, error)

ListTasks returns the Tenant's tasks once the task_list Projection has caught up to after, the Position of the caller's last command. When it has not caught up within the budget, the tasks are returned anyway, marked Stale.

func (*App) OpenTask

func (a *App) OpenTask(ctx context.Context, in OpenTask) (todo.TaskID, eventstore.CommandResult, error)

OpenTask opens a new Task.

func (*App) Routes

func (a *App) Routes(mux *http.ServeMux)

Routes registers the App's HTTP API on mux.

type Config

type Config struct {
	Codec      *eventstore.Codec
	Store      eventstore.Store
	UnitOfWork eventstore.UnitOfWork
	Runtime    consumer.Runtime
	ReadModel  ReadModel
	Mailer     Mailer
}

Config wires an App to one backend.

type InMemoryReadModel

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

InMemoryReadModel keeps the task_list read model in memory. Its writes go through postgres.Stage, so they commit or roll back with the Cursor transaction of an InMemoryRuntime.

func NewInMemoryReadModel

func NewInMemoryReadModel() *InMemoryReadModel

NewInMemoryReadModel returns an empty InMemoryReadModel.

func (*InMemoryReadModel) Clear

func (m *InMemoryReadModel) Clear(ctx context.Context) error

Clear removes every task.

func (*InMemoryReadModel) Insert

func (m *InMemoryReadModel) Insert(ctx context.Context, tenant string, task Task) error

Insert adds task.

func (*InMemoryReadModel) List

func (m *InMemoryReadModel) List(_ context.Context, tenant string) ([]Task, error)

List returns the tenant's tasks in the order they were opened.

func (*InMemoryReadModel) SetDone

func (m *InMemoryReadModel) SetDone(ctx context.Context, tenant string, id uuid.UUID) error

SetDone marks the task id as done.

func (*InMemoryReadModel) SetNotified

func (m *InMemoryReadModel) SetNotified(ctx context.Context, tenant string, id uuid.UUID) error

SetNotified marks the task id as notified.

type Mail

type Mail struct {
	To      string
	Subject string
	// IdempotencyKey lets the mail provider drop a repeated send.
	IdempotencyKey string
}

Mail is one outbound mail.

type Mailer

type Mailer interface {
	Send(ctx context.Context, m Mail) error
}

Mailer sends mails.

type OpenTask

type OpenTask struct {
	Title    string
	Assignee string
}

OpenTask is the input of App.OpenTask.

type PgReadModel

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

PgReadModel keeps the task_list read model in the task_list_v1 table and reads it through the task_list view (migrations/000002_task_list.up.sql). Its writes join the Cursor transaction through postgres.Conn.

func NewPgReadModel

func NewPgReadModel(pool *pgxpool.Pool) *PgReadModel

NewPgReadModel returns a PgReadModel on pool.

func (*PgReadModel) Clear

func (m *PgReadModel) Clear(ctx context.Context) error

Clear removes every task.

func (*PgReadModel) Insert

func (m *PgReadModel) Insert(ctx context.Context, tenant string, task Task) error

Insert adds task.

func (*PgReadModel) List

func (m *PgReadModel) List(ctx context.Context, tenant string) ([]Task, error)

List returns the tenant's tasks in the order they were opened.

func (*PgReadModel) SetDone

func (m *PgReadModel) SetDone(ctx context.Context, tenant string, id uuid.UUID) error

SetDone marks the task id as done.

func (*PgReadModel) SetNotified

func (m *PgReadModel) SetNotified(ctx context.Context, tenant string, id uuid.UUID) error

SetNotified marks the task id as notified.

type ReadModel

type ReadModel interface {
	Insert(ctx context.Context, tenant string, task Task) error
	SetDone(ctx context.Context, tenant string, id uuid.UUID) error
	SetNotified(ctx context.Context, tenant string, id uuid.UUID) error
	List(ctx context.Context, tenant string) ([]Task, error)
	// Clear empties the read model; only projection.Reset needs it.
	Clear(ctx context.Context) error
}

ReadModel stores the task_list read model. Its writes run in the Cursor transaction in ctx, so they commit together with the Projection's Cursor.

type Task

type Task struct {
	ID       uuid.UUID `json:"id"`
	Title    string    `json:"title"`
	Assignee string    `json:"assignee"`
	Done     bool      `json:"done"`
	Notified bool      `json:"notified"`
	OpenedAt time.Time `json:"opened_at"`
}

Task is one row of the task_list read model.

type Tasks

Tasks is the Repository of Task Aggregates.

type TrackerWebhook

type TrackerWebhook struct {
	DeliveryID string    `json:"delivery_id"`
	TaskID     uuid.UUID `json:"task_id"`
}

TrackerWebhook is the body of a tracker webhook: a task was closed in the tracker.

Jump to

Keyboard shortcuts

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