Documentation
¶
Overview ¶
Package serve runs a server of the framework from its command line: the flags every server takes (the address, where the tickets and the attachments are kept, how people sign in, a certificate), the checks of the flags and the environment before anything is read or reached, the stores the flags name, and the HTTP server with the pages in front, until the process is told to stop. A project's command adds what is its own: its package, gate, organization, notices, flows, agents and books.
https://i2y.github.io/ringi-server/operations/ says how to run such a server for the people of an organization, and https://i2y.github.io/ringi-server/reference/ lists its flags.
var f serve.Flags
f.Register(flag.CommandLine)
flag.Parse()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
st, err := serve.Open(ctx, f, log) // checks the flags first
…
defer st.Close()
st.Handle("/mcp", agentsTools) // a route of the project's, behind the same guards (optional)
err = st.Serve(ctx, server.Config{Packages: …, Gate: …, Organization: …, Books: …}, nil)
A project adds a route of its own with Handle: the agents' tools of package mcp, say. The route is behind the guards of the pages and the API, and serves only a request whose person the sign-in finds (Person reads them); it may not take a path of the framework's.
Who sends a request is found in one of three ways, one at a time:
- By default, the header X-Person (and X-Roles their roles), for trying the API with a client such as curl (devlogin.Header). It checks no one, so it works only on a loopback address (the default -addr), and answers only requests sent to one.
- With -dev-login, a page that lists the people of the organization: the one chosen is who uses the pages from then on (devlogin.SignIn). It checks no one either, and works the same way.
- With the -oidc-… flags, signing in through the organization's OpenID Connect provider (package oidc), over https (-tls-cert and -tls-key, or a proxy in front that serves https). A person's roles are those of the claim -oidc-roles-claim names; without it they have none. The server reaches the provider with the client of Flags.ProviderClient, when the project's command sets one.
Whichever of the three, -api-keys names a file of keys for programs that use the API as a person of the organization, such as another system that makes requests (package apikeys): a request that sends one (Authorization: Bearer <key>) is the person of its line while the organization in effect today has them, a key no line has is no one (401), and a request with no key is the sign-in's.
The two that check no one do not run with the settings of a server that serves people: the provider's (the -oidc-… flags, or RINGI_OIDC_CLIENT_SECRET or RINGI_SESSION_KEYS in the environment) or a certificate (-tls-cert). Open stops before it reads or reaches anything, saying what is wrong, with those, with two ways of signing in at once and with the provider's settings given in part; then it stops on a database it cannot reach.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Flags ¶
type Flags struct {
Addr string // -addr, 127.0.0.1:8080
DB string // -db
Files string // -files
DevLogin bool // -dev-login
Issuer string // -oidc-issuer
ClientID string // -oidc-client-id
ClientSecret string // -oidc-client-secret, or RINGI_OIDC_CLIENT_SECRET
RedirectURL string // -oidc-redirect-url
SessionKeys string // -session-keys, or RINGI_SESSION_KEYS
RolesClaim string // -oidc-roles-claim
Scopes string // -oidc-scopes
TLSCert string // -tls-cert
TLSKey string // -tls-key
APIKeys string // -api-keys
Bundles string // -bundles
BundleKeys string // -bundle-keys
// Getenv reads the environment; os.Getenv when nil.
Getenv func(string) string
// ProviderClient makes the server's calls to the provider of the -oidc-… flags: its discovery
// document, its keys and its tokens (oidc.Config.HTTPClient). It is not a flag: a project's
// command sets it, for a provider reached through a proxy, or one whose certificate is signed by
// the organization's own authority, say. nil is a client with a 10-second timeout. Without the
// provider, nothing uses it.
ProviderClient *http.Client
}
Flags are the settings of a server's command line that every server takes, how it reads the environment, and how it reaches the provider.
func (Flags) Check ¶
Check checks the flags and the environment before anything is read or reached, and says what is wrong: -tls-cert without -tls-key; -db with -files, as -db keeps the attachments in the database; a file of -api-keys that cannot be read, has a line apikeys.Parse does not take, or lists no key; a file of -bundle-keys that is no PEM of ed25519 public keys; a directory of -bundles that cannot be read, or holds a file that is no bundle (.zip) or a bundle's signature (.zip.sig), or a signature of no bundle; two ways of signing in; a way that checks no one where it may be reached from another computer or with the settings of a server that serves people (the provider's, a certificate); the provider's settings given in part, or wrong in a way that can be told without reaching the provider (oidc.Config.Check).
type Stores ¶
type Stores struct {
Store server.Store
Files server.Files
// Pool is the PostgreSQL of -db, for what else of the project is kept there (the books of the
// examples); nil without -db.
Pool *pgxpool.Pool
// Look is how the pages look for the organization (web.Look): its logo and name in their bar,
// and the colour of their links and buttons. Set it before Serve, which refuses one that
// web.Look.Check refuses before it makes or reaches anything. The zero Look is the framework's.
Look web.Look
// contains filtered or unexported fields
}
Stores are where the flags say the tickets and the attachments are kept, opened.
func Open ¶
Open checks the flags (Check), then opens where the tickets and the attachments are kept: with -db, PostgreSQL (the schema ringi; the attachments there too); without it, memory (the attachments in the directory of -files when it is given, else in memory). It logs where they are kept to log (slog.Default() when nil).
func (*Stores) Handle ¶
Handle adds a route to the server Serve makes, for what the project serves besides the pages and the API: the agents' tools of package mcp at /mcp, say. pattern is a pattern of http.ServeMux ("/mcp", "POST /hooks/books"). handler makes the route's handler of the server once Serve has made it (after made is given it), before anything is served.
The route is behind the guards of the pages and the API: its answers carry the same headers (a handler may set its own Content-Security-Policy), and what another site sends to change things is refused (403). It serves a person only: a request the sign-in the flags say finds no one in is answered 401, as the API answers it ({"error":"no one is signed in"}), and the handler is not called. The handler reads the person with Person; what it does as them through the server (Server.Do) is asked of the gate, as every operation is.
A route may not take a path of the framework's: / itself, and the paths under /pages, /a2ui and /files (the pages, the screens and the attachments), /requests, /waiting, /mine, /tickets, /organization, /masters and /find (the API), /admin (the administration's API), /openapi.json (the description of the API), and /login and /logout (signing in), with the provider's callback (the path of -oidc-redirect-url). Serve stops before it makes or reaches anything, saying why, on a pattern that is empty, does not read as a pattern of http.ServeMux, takes a request on one of those paths, or conflicts with one added before (some request matches both, and neither is more specific), and on a nil handler; it stops before it serves anything on an error of handler, or a nil http.Handler. Call Handle before Serve, from one goroutine.
func (*Stores) Serve ¶
Serve makes the server of cfg with the Store, the Files and the Authenticator the flags say, and cfg's Logger (else the one Open was given). It refuses a cfg that sets Store, Files or Authenticator: the flags say them; the routes Handle added that it does not take (Handle says which); and a Look that web.Look.Check refuses, before it makes or reaches anything. It gives the server to made, when made is not nil, once it is made and before anything is served; imports the organization; puts the pages in front (web), in the Look, with the sign-in the flags say (the header X-Person, -dev-login, or the provider's) mounted behind the same guards; makes the handlers of the routes Handle added, and puts them behind the guards too; runs the timers and the things to do (Server.Run); and serves HTTP, or https with -tls-cert, until ctx is done, then shuts down within 10 seconds: what is still being answered then is cut off. The HTTP server reads a request's headers within 5 seconds and the request within 30, writes its answer within 30, keeps an idle connection 2 minutes, and takes headers of 64 KiB at most. Serve returns once the server and Run have stopped: nil when ctx ended them.