api

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: BSD-3-Clause Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package api is the REST API of Clowder for desktop clients (SPEC §7.12, D42 to D45): JSON over HTTP under /api/v1/, authenticated by a bearer token.

Like internal/web it calls only the services of internal/app and never imports the store, templ or internal/web. The server of internal/web mounts the routes of a Server (Routes) on its own mux, so that the API goes through the same middleware chain as the pages: security headers, the request log, the deadline of the body and cross-origin protection.

A route is added in three places: its handler and its entry in routes (in the file of its group), its operation in openapi.json, and a test that sends a request through checkResponse. TestOpenAPIMatchesRoutes fails when the routes and the document differ.

Index

Constants

View Source
const (
	// Prefix is the path every route of the API starts with.
	Prefix = "/api/v1"
)

Variables

This section is empty.

Functions

func OpenAPI

func OpenAPI() []byte

OpenAPI returns the OpenAPI document of the API as JSON. The caller must not change it.

Types

type Options

type Options struct {
	// Services are the services the handlers call.
	Services Services
	// Logger receives the log entries of the API; nil discards them. An error
	// is logged as the attribute "err"; a token, a password or a body never
	// is.
	Logger *slog.Logger
	// NoteUser is called with the request and the ID of the user once a
	// bearer token has been accepted, so that the request log of the caller
	// can name the user. Nil does nothing.
	NoteUser func(r *http.Request, userID uint64)
	// ClientIP returns the address of the client of a request, for the login
	// limiter. Nil uses the host of the connection's address; a server behind
	// a reverse proxy passes its own function.
	ClientIP func(r *http.Request) string
	// MaxUploadSize is the largest file POST /uploads takes, in bytes; it
	// must be the limit the posts service was made with. 0 means the default
	// (config.DefaultMaxAttachmentSize). The body of an upload request may be
	// a megabyte more, for the framing of the form.
	MaxUploadSize int64
	// Markdown renders the text of a private message to the "html" of the
	// answers, as the pages do (SPEC D33). Nil makes the default renderer.
	Markdown *markdown.Renderer
	// BaseURL is the public address of the installation, without a trailing
	// slash (config.Config.BaseURL): the link of an invitation is
	// <BaseURL>/invite/<token>. Empty makes the link a path.
	BaseURL string
}

Options are the parts a Server is made of.

type Route

type Route struct {
	// Pattern is the pattern of the route.
	Pattern string
	// Handler answers the requests that match Pattern.
	Handler http.Handler
}

Route is a pattern of http.ServeMux, with the method and the whole path (POST /api/v1/auth/login), and its handler. The server of internal/web registers each Route on its mux.

type Server

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

Server is the REST API. Make it with New and mount its Routes.

func New

func New(opts Options) (*Server, error)

New makes the API from its options. It fails when the services of Options are not all there.

func (*Server) Drain

func (s *Server) Drain(ctx context.Context) bool

Drain waits until every WebSocket has ended, or ctx is done, and reports whether none is left. An upgraded connection belongs to no http.Server, so http.Server.Shutdown does not wait for it: the server closes the hub first (every connection then closes itself with 1001) and calls Drain, so that the process does not end before the clients got their close frame.

func (*Server) Routes

func (s *Server) Routes() []Route

Routes returns every route of the API, the catch-all last. The caller registers them on its mux; it must not change the list.

type Services

type Services struct {
	// Accounts is the service of login, sessions, invitations and users.
	Accounts *app.Accounts
	// Notifications is the service of notifications, for their unread count.
	Notifications *app.Notifications
	// Messages is the service of private messages, for the unread count.
	Messages *app.Messages
	// Rooms is the service of rooms and their members.
	Rooms *app.Rooms
	// Posts is the service of posts, comments, reactions, tags and uploads.
	Posts *app.Posts
	// Board is the service of the boards, their columns and their cards.
	Board *app.Board
	// Search is the service of search.
	Search *app.Search
	// Audit is the service that reads the audit log.
	Audit *app.Audit
	// Hub is the hub the services publish to; the WebSocket (SPEC §7.13)
	// subscribes to it. It must be the hub of the server of internal/web,
	// which closes it when the server stops. Nil makes a hub that nothing
	// publishes to, so that a WebSocket never gets an event.
	Hub *live.Hub
}

Services are the services the handlers call. Later steps add a field for each service they need.

Jump to

Keyboard shortcuts

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