Integrate auth, config, watcher and terminal wiring in buildApp: auth guards every API route by default, notebook watcher events feed the /api/events hub, the terminal runs in the configured workspace, and SHELLBOOK_DATA_DIR is the single data directory variable. The systemd unit uses KillMode=process so tmux sessions survive service restarts.
59 lines
2.6 KiB
TypeScript
59 lines
2.6 KiB
TypeScript
import Fastify, { type FastifyInstance } from "fastify";
|
|
import websocket from "@fastify/websocket";
|
|
import { loadConfig, type ServerConfig } from "./config";
|
|
import { workspacePlugin } from "./workspace";
|
|
import { runsPlugin, EventsHub } from "./runs";
|
|
import { terminalPlugin, type TerminalOptions } from "./terminal";
|
|
import { createNotebookWatcher } from "./watch";
|
|
import { registerAuth, type AuthOptions } from "./auth";
|
|
import { registerStatic } from "./static";
|
|
|
|
export interface AppOptions {
|
|
logger?: boolean;
|
|
config?: Partial<ServerConfig>;
|
|
events?: EventsHub;
|
|
/**
|
|
* Login protection for all /api routes. Enabled by default with `config.dataDir`.
|
|
* `false` exists only for tests that exercise other plugins in isolation.
|
|
*/
|
|
auth?: Partial<AuthOptions> | false;
|
|
trustProxy?: string[];
|
|
/** Built web app (apps/web/dist); served only when it contains index.html. */
|
|
staticDir?: string;
|
|
/** Watch workspace notebooks and publish file.changed/file.deleted on /api/events. */
|
|
watchNotebooks?: boolean;
|
|
terminal?: Omit<TerminalOptions, "workspaceDir">;
|
|
}
|
|
|
|
/**
|
|
* Builds the Fastify app. Feature areas live in their own folders:
|
|
* src/auth/ login, persistent session secret (registered first: its hook guards every /api route)
|
|
* src/workspace/ notebooks, hosts.yaml; src/ssh/, src/runs/ queue, audit, artifacts, /api/events
|
|
* src/terminal/ node-pty + tmux, /api/terminal; src/watch/ file.changed events into the events hub
|
|
* src/static/ built web app with SPA fallback
|
|
*/
|
|
export async function buildApp(options: AppOptions = {}): Promise<FastifyInstance> {
|
|
const config = loadConfig(options.config);
|
|
const hub = options.events ?? new EventsHub();
|
|
const app = Fastify({ logger: options.logger ?? false, trustProxy: options.trustProxy ?? false });
|
|
if (options.auth !== false) await registerAuth(app, { dataDir: config.dataDir, ...options.auth });
|
|
await app.register(websocket);
|
|
await app.register(workspacePlugin, { config });
|
|
await app.register(runsPlugin, { config, hub });
|
|
await app.register(terminalPlugin, { ...options.terminal, workspaceDir: config.workspaceDir });
|
|
|
|
if (options.watchNotebooks) {
|
|
const watcher = createNotebookWatcher(config.workspaceDir, {
|
|
onError: error => app.log.error({ err: error }, "notebook watcher error"),
|
|
});
|
|
watcher.on(event => hub.publish(event));
|
|
app.addHook("onClose", async () => { await watcher.close(); });
|
|
await watcher.ready;
|
|
}
|
|
|
|
app.get("/api/health", async () => ({ ok: true }));
|
|
if (options.staticDir) await registerStatic(app, options.staticDir);
|
|
|
|
return app;
|
|
}
|