Skip to content

Latest commit

 

History

History
247 lines (198 loc) · 11.2 KB

module-contract.md

File metadata and controls

247 lines (198 loc) · 11.2 KB

Module contract

This document is the agreement between the panel shell (OhMyServer; implemented in internal/shell) and modules (independent applications such as docker-go, acme-go, yabber and dns-go). A module that follows it runs on its own and can be mounted by any shell, without importing any shell code.

Contract version: 1.

Principles

  • Modules never depend on the shell. No Go import, no JavaScript import. The contract is expressed in standard-library types, a JSON manifest and one JavaScript function.
  • The shell discovers, the module describes. Everything the shell needs to know is in the module's manifest.
  • Minimal dependencies. A module depends only on what it needs to run on its own. Whoever uses something implements it: a module ships a SQLite store only if it uses SQLite standalone. A shell that wants a module's state somewhere else implements the module's store interface itself.
  • One code path. Standalone and mounted, a module runs the same constructor, handler, store and UI. Only the surrounding host differs.

1. Go interface

The shell defines the interface; modules satisfy it implicitly:

// Defined by the shell. Modules do not import it.
type Module interface {
	Handler() http.Handler // the module's API, with relative routes
	Assets() fs.FS         // browser assets; manifest.json at the root
}

If the module also implements io.Closer, the shell calls Close at shutdown, after the HTTP server has stopped.

By convention, a module's constructor takes only standard-library types and the module's own option types:

func New(ctx context.Context, opts Options) (*Module, error)
// a module that uses SQLite on its own also takes the database:
func New(ctx context.Context, db *sql.DB, opts Options) (*Module, error)

opts carries the module's bootstrap values (a data directory, optional collaborators such as an apps.Exposer, optional store implementations). Cross-module collaboration uses interfaces the consuming module defines. The shell's assembly code implements them with adapters, so modules never import each other.

Mounting

Mounted by a shell Standalone (recommended)
API (Handler) /m/<id>/api/ /api/
Assets (Assets) /m/<id>/ui/ /

The handler sees paths relative to its mount point (the prefix is stripped). It must not assume an absolute path, and it must not hard-code the prefix in links or redirects.

Authentication

When mounted, the shell authenticates every request before it reaches the module's handler, except for the API paths the manifest lists as public: modules must be constructable with their own authentication disabled, and must authenticate their public paths themselves. Standalone, a module authenticates however it likes (password, token, reverse proxy).

Write requests reach the handler only from the panel's own origin: the shell applies http.CrossOriginProtection. Modules should apply it themselves when standalone.

2. Manifest

Assets() contains manifest.json at its root:

{
  "contract": 1,
  "id": "docker",
  "title": "Docker",
  "icon": "icon.svg",
  "entry": "module.js",
  "styles": ["vendor/stylesheets.css", "style.css", "docker.css"],
  "home": "/",
  "nav": [
    { "path": "/", "title": "Dashboard", "icon": "◎", "group": "Overview" },
    { "path": "/containers", "title": "Containers", "icon": "▣", "group": "Resources" }
  ]
}
Field Required Meaning
contract yes Contract version. A shell refuses versions it does not support.
id yes [a-z][a-z0-9-]*; names the module's tables, and its URLs unless the shell mounts it under another id.
title yes Navigation label. A shell may override it.
icon no A path inside the assets (SVG or PNG), or a short glyph.
entry yes Path of the JavaScript module that exports mount.
styles no Stylesheets the shell loads into the module's shadow root, in order.
home no Module path shown at the module root; defaults to the first nav path.
nav no Navigation entries: module path, title, optional icon and group.
public no API paths served without the shell's sign-in, such as webhook endpoints (["/hooks/"]; a trailing / covers everything below). The module authenticates these requests itself, e.g. by checking a signature.

A shell may mount a module under an id of its own choosing, for instance one that names what the module does rather than the project behind it (OhMyServer mounts yabber as gateway). Mounted ids are unique within the shell, and they are what URLs, ctx.id and ctx.link use. Modules therefore never hard-code their own id in URLs.

All paths are relative to the assets root. Module paths (home, nav[].path) are relative to the module and start with /.

3. Browser entry point

entry is an ES module exporting mount:

export function mount(el, ctx) {
  // Render the module into el.
  return {
    update(ctx) { /* the route or another context value changed */ },
    unmount() { /* release listeners, timers, connections */ },
  };
}

ctx contains:

Field Meaning
id The module id.
apiBase Absolute URL of the module's API, ending with /.
assetsBase Absolute URL of the module's assets, ending with /.
path The current module path, e.g. /containers/abc.
query The current query parameters, as an object.
navigate(path, { replace }) Go to a module path.
beforeLeave(guard) Optional: register a synchronous navigation guard; return false to cancel leaving. Returns an unsubscribe function, called on page unmount. Hosts check guards for links, programmatic navigation, browser Back/Forward, and sign-out. Modules separately use beforeunload for reload/close.
href(path, query) The URL of a module path, for <a href>.
link(moduleId, path, query) URL of a path in another module, or null if it is not mounted.
lang Interface language, e.g. en (the default) or zh-CN.

The shell owns the address bar. Modules route on ctx.path, change routes only through navigate or href, and never read or write location for routing. The shell calls update whenever any ctx value changes, and unmount before removing the module.

Isolation and theming

The shell mounts each module into its own shadow root. It loads the manifest's styles into that root before calling mount, so a module's CSS cannot affect the shell or other modules, and vice versa. Dialogs and toasts render inside el.

Theme values reach modules through CSS custom properties, which inherit across the shadow boundary. The shell loads @lsongdev/stylesheets on its document, so its semantic tokens (--color-canvas, --color-surface, --color-text, --color-primary, --color-danger, …, --font-family, --radius-md, …) and component tokens (--button-*, --input-*, …) are defined there, light or dark.

Modules share one stylesheet, the runtime's module.css: the library's component rules (.button, .input, .table, .dialog, …) and the modules' common conventions (.section, .grid, .cell-main, .toolbar, …). The shell loads it into every shadow root before the manifest's styles, so a manifest lists only the module's own stylesheet. Those rules match inside the shadow root while the tokens come from the shell, so the module follows the panel's theme. Standalone, the module's index.html links lib/module.css on its own document, which then defines the tokens too.

Shared runtime and imports

Modules don't carry their own copies of the browser plumbing. Two bare module specifiers are resolved by an import map on the hosting document:

Specifier What it is In the shell
@lsongdev/scripts/… Preact, htm, components, hooks and routing the shell's copy, ./_shell/node_modules/@lsongdev/scripts/
@lsongdev/panel/… this contract's runtime (panel/ui in this repository): mount.js (createMount), panel.js (usePanel), forms.js, hooks.js, client.js, format.js, ui.js, terminal.js (terminalSocket, the terminal WebSocket protocol for the library's Terminal) and standalone.js ./_shell/panel/

Every module imports both only through these specifiers, never through a relative node_modules path, so a page and the runtime it uses share one copy of Preact. The shell allows its import map in the Content-Security-Policy by hash; a standalone module does the same for its own.

Standalone

A standalone module serves its own index.html from the same assets. It links lib/module.css and its own stylesheet, maps @lsongdev/scripts/… to its own node_modules, and serves the runtime beside them at lib/. Modules in this repository serve panel.FS there and import it as @lsongdev/panel/…, which their import map also points at ./lib/. A module in another repository keeps its own ui/lib/, imported with relative paths, so it is the same inside the shell and on its own; it only shares @lsongdev/scripts with the shell. Its entry runs:

import { standalone } from '@lsongdev/panel/standalone.js'; // or './lib/standalone.js'
import { mount } from './module.js';

standalone(mount);

which builds the context with a hash router and apiBase: "api/". The manifest supplies its navigation.

4. Storage

All modules share one SQLite database that the shell opens (WAL journaling, busy timeout, foreign keys) and passes to each module that uses it. A separate database is the exception, kept for modules with a special need (for example very high write volume), and decided case by case.

  • Tables are named <id>_<name> (for example acme_certificates). Modules never read or write another module's tables.
  • Migrations belong to each module. A module records its schema version in its own table (conventionally <id>_schema) and migrates in New. Upgrading a module's own earlier, unprefixed database is that module's job.

Who implements a store follows the minimal-dependency principle:

  • A module that uses SQLite on its own defines its store, ships the SQLite implementation and migrations, and takes a *sql.DB in New. Standalone it opens its own file; mounted it receives the shared database.
  • A module that keeps state in files (records, configuration) defines a store interface with a file implementation as its default, and does not depend on a database driver. A shell that wants that state in the shared database implements the interface (with tables named after the module) and passes it in opts.
  • Files that must exist on disk (compose files, checkouts, app data) go under a data directory the shell passes in opts (<data>/<id>/).
  • Bootstrap values needed before anything opens (listen address, data directory, administrator secret) are the shell's flags or environment. State that operators change lives in the database and is edited through the UI, not in configuration files.

Versioning

The contract version only increases for incompatible changes. Additive changes (new optional manifest fields, new ctx fields) keep the version. Modules must ignore unknown ctx fields, and shells must ignore unknown manifest fields.