Help us shape the future of Go! Take the 2026 Go Developer Survey

server

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 45 Imported by: 0

Documentation

Overview

Package server exposes the CMS core over a JSON HTTP API.

Index

Constants

View Source
const (
	DefaultListPageSize    = 20
	DefaultListPageCap     = 100
	DefaultPickerPageSize  = 50
	DefaultToastDuration   = 6 * time.Second
	DefaultToastNameLength = 45
	DefaultFormatLocale    = "es-ES"
)

The list settings served when the environment names none.

View Source
const DefaultAssetCacheMaxAge = time.Hour

DefaultAssetCacheMaxAge is how long a client may keep a site asset when nothing else applies.

View Source
const DefaultContentSharedMaxAge = time.Minute

DefaultContentSharedMaxAge is how long a shared cache may serve a public read by default.

View Source
const DefaultContentStaleWhileRevalidate = 5 * time.Minute

DefaultContentStaleWhileRevalidate is how long a shared cache may serve a stale read by default.

View Source
const DefaultDefinitionsImportCap int64 = 256 << 10

DefaultDefinitionsImportCap is how large a definitions file an import takes when nothing else is named.

View Source
const DefaultMediaCacheMaxAge = time.Hour

DefaultMediaCacheMaxAge is how long a client may keep a served upload when nothing else applies.

View Source
const DefaultUploadTimeout = 5 * time.Minute

DefaultUploadTimeout is how long a media or theme upload has to arrive when nothing else is named.

View Source
const MaxDefinitionsImportCap int64 = authkit.MaxRequestBodyBytes

MaxDefinitionsImportCap is the largest definitions cap the request body reader can carry.

Variables

View Source
var ErrCronClosed = errors.New("server: cron runs closed")

ErrCronClosed reports a run asked for once the cron runs began to close.

View Source
var ErrPublicURL = errors.New("must be an http or https address naming only a host, such as https://example.com")

ErrPublicURL reports a public address naming more or less than a scheme and a host.

Functions

func DefaultListPageSizes added in v0.19.0

func DefaultListPageSizes() []int

DefaultListPageSizes returns the page sizes a list offers when the environment names none.

func DefaultPickerPageSizeAmong added in v0.19.0

func DefaultPickerPageSizeAmong(sizes []int, opening int) int

DefaultPickerPageSizeAmong returns DefaultPickerPageSize when the sizes offer it, else the opening size.

func NewServer

func NewServer(cfg Config) http.Handler

NewServer returns the HTTP handler serving the CMS API. Every route requires a login session except login, logout, and each plugin's declared public paths.

func ParsePublicURL added in v0.18.0

func ParsePublicURL(raw string) (*url.URL, error)

ParsePublicURL returns the scheme and host of the public address raw names, or none when raw is empty.

Types

type CachePolicy added in v0.11.0

type CachePolicy struct {
	// AssetMaxAge is how long a client may keep a site asset.
	AssetMaxAge time.Duration
	// MediaMaxAge is how long a client may keep a served upload.
	MediaMaxAge time.Duration
	// ContentSharedMaxAge is how long a shared cache may serve a public read.
	ContentSharedMaxAge time.Duration
	// ContentStaleWhileRevalidate is how long a shared cache may serve a stale read.
	ContentStaleWhileRevalidate time.Duration
}

CachePolicy is how long each kind of public answer may be kept. Zero applies the default.

type Config

type Config struct {
	Users UserStore
	// Content persists the content the CMS serves.
	Content content.Store
	// Types persists the content type registry. Nil leaves the registry routes unhandled.
	Types content.TypeStore
	// Registry is the content registry the server shares with the host, built over Types when nil.
	Registry *content.Registry
	// Plugins maps a plugin id to its HTTP handler.
	Plugins map[string]http.Handler
	// PluginPublicPaths maps a plugin id to its session-exempt paths.
	PluginPublicPaths map[string][]string
	// Web serves the single-page app under the admin base path. Nil leaves the admin paths unhandled.
	Web fs.FS
	// SiteTitle names the public site in its chrome.
	SiteTitle string
	// TrustedProxies lists the CIDR ranges trusted to set X-Forwarded-For.
	TrustedProxies []string
	// PublicURL is the address visitors reach the site at. Nil judges each write by the request itself.
	PublicURL *url.URL
	// Logger receives the writes the server refuses and the upload files it cannot remove. Nil discards it.
	Logger *slog.Logger
	// Theme serves the public site while it is healthy. Nil leaves the built-in renderer serving.
	Theme Theme
	// Themes is the managed themes directory. Nil leaves the theme routes unhandled.
	Themes Themes
	// Media is the media library uploads land in. Nil leaves the media routes unhandled.
	Media MediaLibrary
	// MediaStore persists the media library. Nil leaves the media routes unhandled.
	MediaStore media.Store
	// MediaFiles serves the stored uploads publicly. Nil leaves the media prefix reserved but empty.
	MediaFiles fs.FS
	// Cache is how long each kind of public answer may be kept. A zero window applies its default.
	Cache CachePolicy
	// ThemeTimeout is how long a theme has to begin answering. Zero applies the default.
	ThemeTimeout time.Duration
	// Version is the application version reported at /api/version.
	Version string
	// Settings persists the values the site chooses for itself. Nil leaves the site default unread.
	Settings SiteSettings
	// Readers persists the values one reader chooses. Nil leaves the locale preference unhandled.
	Readers ReaderSettings
	// DefinitionsImportCap is the largest definitions file an import takes. Zero applies its default.
	DefinitionsImportCap int64
	// UploadTimeout is how long a media or theme upload has to arrive. Zero applies its default.
	UploadTimeout time.Duration
	// Declarations is what every plugin declared at the last boot, by the plugin that declared it.
	Declarations definitions.Walked
	// Lists is what the admin lists read once. A zero value serves its default.
	Lists ListSettings
	// Roles holds the roles accounts take and the capabilities each carries. Nil holds the core's roles alone.
	Roles *goncierge.Registry
	// Cron owns the cron runs that visits and the cron address start. Nil leaves both off.
	Cron *CronRuns
	// CronSecret is the bearer secret the cron address asks for. Empty leaves the address unserved.
	CronSecret string
	// CronOnVisits lets each answered request start the cron jobs.
	CronOnVisits bool
	// Avatars is how the site draws the author avatars the admin shows. A zero value draws initials.
	Avatars avatar.Options
}

Config carries the stores and plugin surfaces the server serves.

type CronRunner added in v0.19.0

type CronRunner interface {
	RunDue(ctx context.Context) (cron.Report, error)
}

CronRunner runs the site's cron jobs once and reports what it did.

type CronRuns added in v0.19.0

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

CronRuns owns every cron run of one server, starting visit runs in the background and cutting all at Close.

func NewCronRuns added in v0.19.0

func NewCronRuns(runner CronRunner, pace time.Duration, clock func() time.Time) *CronRuns

NewCronRuns returns the runs over runner, a visit checking at most once per pace on clock.

func (*CronRuns) Close added in v0.19.0

func (c *CronRuns) Close(ctx context.Context) error

Close cuts every run and waits for them until ctx ends.

func (*CronRuns) Cut added in v0.19.0

func (c *CronRuns) Cut()

Cut refuses later runs and cancels every running one without waiting.

func (*CronRuns) RunNow added in v0.19.0

func (c *CronRuns) RunNow() (cron.Report, error)

RunNow runs the cron jobs in the caller's goroutine under the runs' own context, refusing once Close has begun.

func (*CronRuns) Visited added in v0.19.0

func (c *CronRuns) Visited()

Visited starts a background run when the pace allows and no visit run is going, and returns at once.

type ListSettings added in v0.19.0

type ListSettings struct {
	// PageSizes lists the page sizes a list offers, from the smallest up. Empty serves the defaults.
	PageSizes []int
	// PageSize is the page a list opens on and the size a request naming none is answered at.
	PageSize int
	// PickerPageSize is the page the media picker opens on. Zero serves its default among the page sizes.
	PickerPageSize int
	// PageCap is the most items one admin page carries, a larger request answered at the cap.
	PageCap int
	// ToastDuration is how long a confirmation toast stays on screen.
	ToastDuration time.Duration
	// ToastNameLength is how many characters of an item's name a toast shows.
	ToastNameLength int
	// FormatLocale is the locale dates and numbers are written in, whatever the interface language.
	FormatLocale string
}

ListSettings carries what the admin lists read once, as the environment named it.

type MediaLibrary added in v0.3.0

type MediaLibrary interface {
	// Cap returns the most bytes one upload may carry.
	Cap() int64
	// Ingest validates an upload, stores its files, and returns the media item they make.
	Ingest(ctx context.Context, name string, data []byte, authorID uuid.UUID) (media.Media, error)
	// Remove deletes the item's stored file and every sub-size it owns.
	Remove(m media.Media) error
	// Finish marks the upload of a saved item finished.
	Finish(m media.Media)
}

MediaLibrary validates uploads and owns the media directory.

type ReaderSettings added in v0.6.0

type ReaderSettings interface {
	Lookup(ctx context.Context, userID uuid.UUID, key string) (string, bool, error)
	Save(ctx context.Context, userID uuid.UUID, key, value string) error
}

ReaderSettings persists the values one reader chooses for themselves.

type SiteSettings added in v0.6.0

type SiteSettings interface {
	Lookup(ctx context.Context, key string) (string, bool, error)
	Save(ctx context.Context, values map[string]string) error
}

SiteSettings persists the values a site chooses for itself.

type Theme

type Theme interface {
	// Healthy reports whether the theme is serving.
	Healthy() bool
	// Target returns the address the theme serves on.
	Target() string
}

Theme is the running theme the public site is served through.

type Themes added in v0.2.0

type Themes interface {
	// List returns the installed themes, marking the one that is active.
	List(ctx context.Context) ([]themehost.Installed, error)
	// Install unpacks the archive as the named theme.
	Install(ctx context.Context, name string, archive io.ReaderAt, size int64) error
	// Activate serves the public site through the named theme.
	Activate(ctx context.Context, name string) error
	// Deactivate returns the public site to the built-in renderer.
	Deactivate(ctx context.Context) error
	// Rollback returns the public site to the choice before the current one, naming it.
	Rollback(ctx context.Context) (string, error)
	// Previous returns the choice a rollback would return to, and whether one is offered.
	Previous(ctx context.Context) (string, bool, error)
}

Themes is the managed themes directory the admin lists and installs into.

type UserStore added in v0.19.0

type UserStore interface {
	authkit.AdminStore
	// UsersByIDs returns the accounts holding the ids in one read.
	UsersByIDs(ctx context.Context, ids []uuid.UUID) ([]gouncer.User, error)
}

UserStore persists the accounts the server signs in and administers.

Jump to

Keyboard shortcuts

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