Documentation
¶
Overview ¶
Package web serves the pages people use in a browser, in which they make requests, approve them and see their own. In front of the server's API, it also serves the A2UI screens filled with the server's data and the actions of their buttons, ritsu's catalog and functions, and the files people attach. All of it is behind one set of guards: the person, the protection against cross-site requests, the limits of size, and the headers a browser is held to.
Package serve uses it. https://i2y.github.io/ringi-server/screens/ says how to add routes and screens of your own, README.md what each route answers, and DESIGN.md how it works.
Index ¶
- Constants
- Variables
- type CatalogConflict
- type Config
- type DiskFiles
- func (d *DiskFiles) Delete(_ context.Context, id string) error
- func (d *DiskFiles) Open(ctx context.Context, id string) (io.ReadCloser, server.FileInfo, error)
- func (d *DiskFiles) Put(_ context.Context, info server.FileInfo, r io.Reader) (string, error)
- func (d *DiskFiles) Stat(_ context.Context, id string) (server.FileInfo, error)
- func (d *DiskFiles) URL(ctx context.Context, id string) (string, error)
- type Handler
- func (h *Handler) Handle(pattern string, handler http.Handler)
- func (h *Handler) Keep(ctx context.Context, p server.Person, fh *multipart.FileHeader) (string, error)
- func (h *Handler) Keeper(ctx context.Context, p server.Person, post page.Post) page.Keeper
- func (h *Handler) Lookup(ctx context.Context, p server.Person, ticket string) *Lookup
- func (h *Handler) Person(r *http.Request) (server.Person, error)
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type Look
- type Lookup
- func (l *Lookup) FileNames(ids []string) (map[string]string, error)
- func (l *Lookup) FileURLs(ids []string) (map[string]string, error)
- func (l *Lookup) MasterNames(lang, master string, keys []string) (map[string]string, error)
- func (l *Lookup) PeopleNames(lang string, ids []string) (map[string]string, error)
- type ProxyHeader
Constants ¶
const ( PagesPath = "/pages/" PageAssets = "/pages/assets/" )
The addresses of the pages and of their files.
const ( Policy = "default-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'" PagePolicy = "" /* 152-byte string literal not displayed */ )
The policies of the Content-Security-Policy header: Policy for every answer of this package, PagePolicy for an HTML page that loads its style sheet and script from the server.
const DefaultMaxFile = 10 << 20
DefaultMaxFile is the largest attachment when Config.MaxFile is 0.
const FindPath = "/a2ui/find"
FindPath is where a picker finds the values it has too many of to list (DESIGN 5.1), the address the screens' /find holds (ringi's DESIGN 13.2): a renderer's PersonPicker and MasterPicker, and the pages' script, find under it, at "/people", "/teams" or "/masters/<file>/<master>".
const LanguageCookie = "ringi-lang"
LanguageCookie keeps the language a person chose on a page (with ?lang=).
const MaxLogo = 256 << 10
MaxLogo is the most bytes of Look.Logo.
const MaxLookName = 60
MaxLookName is the most characters of Look.Name.
const MinProxySecret = 32
MinProxySecret is the shortest Secret ProxyHeader takes.
const OpenAPIPath = "/openapi.json"
OpenAPIPath is where the description of the whole API is served (server.Server.OpenAPI): the kinds' operations, the ones no kind owns, the administration's and the attachments', as OpenAPI 3.1, to a person signed in.
const PicksListed = 1000
PicksListed is the most people, teams or rows of a master a picker lists: with more, a person finds the value by a name (FindPath, server.Find), or types its ID or key.
const UploadPath = "/files"
UploadPath is where a file is uploaded (DESIGN 6.2): the address the screens' /upload holds (ringi's DESIGN 13.2), which a FileAttachment posts its file to.
Variables ¶
var ( ErrTooLarge = errors.New("web: the file is larger than the server takes") ErrFileType = errors.New("web: the server does not take files of this type") ErrFileContent = errors.New("web: the file is not of the type it is sent as") ErrNoFiles = errors.New("web: the server keeps no files") )
The errors of keeping a file: one larger than Config.MaxFile, one of a media type it does not take, one that does not begin as a file of its media type does, and a server with no Config.Files.
var DefaultFileTypes = []string{
"application/pdf",
"image/png", "image/jpeg", "image/gif", "image/webp",
"text/plain", "text/csv",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.oasis.opendocument.text",
"application/vnd.oasis.opendocument.spreadsheet",
"application/vnd.oasis.opendocument.presentation",
}
DefaultFileTypes are the media types an attachment may have when Config.FileTypes is nil: PDF, images, text, CSV, and the documents of word processors, spreadsheets and presentations.
var DefaultInline = []string{"application/pdf", "image/png", "image/jpeg", "image/gif", "image/webp"}
DefaultInline are the media types a browser shows rather than saves when Config.Inline is nil.
var ErrShortSecret = errors.New("web: ProxyHeader needs a Secret of at least 32 characters")
ErrShortSecret is ProxyHeader's failure with no Secret, or one shorter than 32 characters.
Functions ¶
This section is empty.
Types ¶
type CatalogConflict ¶
type CatalogConflict struct {
First, Second int // the packages, from 1, in the order of Config.Packages
FirstKinds, SecondKinds []string // the kinds of each, as "<name> <version>"
ID string // the catalog's ID
}
CatalogConflict is New's error for two packages whose screens name one catalog by its ID, each with a catalog of other files (catalog.json or functions.js): a renderer keeps one catalog by its ID (A2UI 1.0), so it would draw the screens of one of them with the functions of the other. Two packages that name one ID with the same files are one catalog, and are taken.
func (*CatalogConflict) Error ¶
func (e *CatalogConflict) Error() string
func (*CatalogConflict) In ¶
func (e *CatalogConflict) In(lang string) string
In says the error in a language: "ja" in Japanese, any other in English.
type Config ¶
type Config struct {
// MaxFile is the largest attachment, in bytes (10 MiB when 0).
MaxFile int64
// FileTypes are the media types an attachment may have (DefaultFileTypes when nil).
FileTypes []string
// Inline are the media types a browser shows rather than saves (DefaultInline when nil): PDF and
// the images of DefaultInline at most, whose first bytes the handler checks (New refuses others).
Inline []string
// TrustedOrigins are origins ("https://host[:port]") besides the server's own that may send
// what changes things: a host of the screens on an origin of its own.
TrustedOrigins []string
Logger *slog.Logger
// Require wraps each route of the plain HTML pages: a sign-in's guard, which sends a browser with
// no session to sign in and come back (oidc's SignIn.Require). With none, a page asked for with
// no person is a notice (401).
Require func(http.Handler) http.Handler
// LoginURL answers the address of signing in that comes back to a path, for the link of that
// notice (oidc's SignIn.LoginURL); nil shows none.
LoginURL func(returnTo string) string
// SignOut is the path the pages' bar posts to, to sign out (oidc's LogoutPath); "" shows no
// button.
SignOut string
// Look is how the pages look for the organization: its logo and name in the bar, and the colour
// of the links and the buttons. The zero Look is the framework's.
Look Look
}
Config is what the handler is made with. The rest (the person, the files, the largest body) is the server's.
type DiskFiles ¶
type DiskFiles struct {
// contains filtered or unexported fields
}
DiskFiles keeps the attachments in a directory: each file at <dir>/<id[:2]>/<id>, and what is kept of it at <id>.json beside it, each written to a temporary file and renamed into place. An ID is 32 lowercase hexadecimal digits; any other is not looked up.
func NewDiskFiles ¶
NewDiskFiles makes the store of a directory, making the directory when it is not there.
func (*DiskFiles) Delete ¶
Delete deletes a file: its record first, so that it is not there from then on, then its bytes. Bytes left by a failure are deleted when it is deleted again.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the pages, the screens, the attachments and the server's API.
func New ¶
New makes the handler of a server. It loads the screens of each package the server serves, and refuses one whose screens do not load, and a Look that Look.Check refuses.
func (*Handler) Handle ¶
Handle adds a route (a ServeMux pattern) behind the guards: a page of the plain HTML screens, or a route of signing in. It finds the person with Person.
func (*Handler) Keep ¶
func (h *Handler) Keep(ctx context.Context, p server.Person, fh *multipart.FileHeader) (string, error)
Keep keeps a file a person posted with a form, as an upload keeps it: ErrTooLarge past Config.MaxFile, ErrFileType for a media type Config.FileTypes does not have, ErrFileContent for a file that does not begin as its type does, *server.TooManyUploads for a person who has as many uploads that no ticket holds as the server keeps. It answers the file's ID. It keeps the file anew each time; a post of a page keeps its files with Keeper.
func (*Handler) Keeper ¶
Keeper keeps the files a post of a page (of package page) attaches as the person's, as an upload does, each once: the same post sent again (pressed twice with no script, sent again from a page gone back to) names the files it named the first time, and is the same operation. A file the server does not take is said at its field.
func (*Handler) Lookup ¶
Lookup makes the lookup of a screen the person is shown: of a ticket, or of no ticket ("") for a new request's form and a list.
type Look ¶
type Look struct {
// Name is the organization's name, or the pages', at the start of the bar: at most MaxLookName
// characters, with no control character and no space at its ends. "" for none: the bar says
// "Home" where there is no logo either.
Name string
// Logo is the organization's logo, before the name: an image of SVG, PNG, JPEG, GIF or WebP, of
// at most MaxLogo bytes, served at PageAssets+"logo". The bar shows it 1.75rem high (28 pixels at
// the browser's usual size). nil for none.
Logo []byte
// Accent is the colour of the links, the buttons and the stage a request is at on the light
// pages, as #rrggbb: it reads at 4.5:1 or more on their paper and tint (WCAG 1.4.3). "" for the
// framework's.
Accent string
// AccentDark is the same on the dark pages, for a browser set to the dark scheme: it reads at
// 4.5:1 or more on their dark paper and tint. "" makes it of Accent, mixed with white in steps of
// 5 % until it reads; with no Accent either, it is the framework's.
AccentDark string
}
Look is how the pages look for an organization: its logo and name at the start of the pages' bar, where they lead to the start page, and the colour of the links, the buttons and the stage a request is at. The zero Look is the framework's. It is the look of the plain HTML pages, and of the pages of signing in that load their style sheet; a host of the A2UI screens shows them in its own. New refuses a Look that Check refuses.
func (Look) Check ¶
Check says what is wrong with a look: a name too long, with a control or format character, or with a space at its ends; a logo too large, or of no image type the pages show; a colour that is not #rrggbb, or does not read at 4.5:1 on the pages it is for. The package serve asks it before it makes or reaches anything, and New asks it too.
type Lookup ¶
type Lookup struct {
// contains filtered or unexported fields
}
Lookup answers the names a screen shows to a person (screens.Lookup): the names of the people and teams and of the masters' rows in the screen's language, and the addresses and the names of the files the person may open. One serves a screen: the A2UI screens and the plain HTML pages alike.
func (*Lookup) FileNames ¶
FileNames answers the names the files were given when they were uploaded, of those FileURLs answers an address for: a file the ticket holds, and one the person uploaded. Any other ID gets none, and the screen shows it by its ID.
func (*Lookup) FileURLs ¶
FileURLs answers the addresses the handler serves the files at: a file the ticket holds through the ticket, a file the person uploaded at /files/{id}. Any other ID gets none.
func (*Lookup) MasterNames ¶
MasterNames answers the names of a master's rows that may be chosen today, named by its path (company_masters/vendors): the column the kinds' descriptions name a row by (the master's name_field, ringi's DESIGN 12.2), the same in every language.
type ProxyHeader ¶
type ProxyHeader struct {
// Secret is what the proxy sends in SecretHeader: at least 32 characters.
Secret string
// The headers; by default X-Ringi-Proxy-Secret, X-Ringi-Person and X-Ringi-Roles (the roles
// separated by commas).
SecretHeader, PersonHeader, RolesHeader string
}
ProxyHeader is the Authenticator of a server behind a proxy that signs people in: it trusts the proxy's headers naming the person and their roles only on a request that carries Secret, which only the proxy knows and sends, in a header of its own. A request without it, or with another, has no person (401), even when it names one.
The proxy sends each of the three headers once on every request, the roles header too when the person has no role (empty), and takes away the headers of the same names a client sent. A request with two or more of any of them, or with no roles header, has no person: a client's header the proxy added to instead of replacing would otherwise be read for the proxy's.
func (ProxyHeader) Authenticate ¶
Authenticate answers the person the proxy names. With a Secret shorter than MinProxySecret (a server made with New stops before; this is for one that wraps ProxyHeader in an Authenticator of its own), it answers ErrShortSecret and no person.
func (ProxyHeader) Check ¶
func (a ProxyHeader) Check() error
Check reports what is wrong with the configuration, for a server to stop before it takes requests (server.New asks it, as a server.Checker): no Secret, or one shorter than MinProxySecret; a Secret with a character a header does not carry as it is (a space at its ends, a control character, a character past ASCII), which no request would then match; a header name that is not one; two of the three headers named alike. nil when nothing is wrong.