feat(server): add persistent authentication and production build (#6)
Protect HTTP and WebSocket API routes with scrypt password login and signed persistent sessions. Restrict forwarded identity and HTTPS detection to configured proxies, and serve optional web assets with SPA fallback. Add @fastify/static for Fastify-compatible static file handling and esbuild to bundle TypeScript core source while retaining native dependencies externally.
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import Fastify, { type FastifyInstance } from "fastify";
|
||||
import websocket from "@fastify/websocket";
|
||||
import { resolve } from "node:path";
|
||||
import { registerAuth, type AuthOptions } from "./auth";
|
||||
import { registerStatic } from "./static";
|
||||
|
||||
export interface AppOptions {
|
||||
logger?: boolean;
|
||||
auth?: AuthOptions;
|
||||
trustProxy?: string[];
|
||||
staticDir?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13,10 +19,12 @@ export interface AppOptions {
|
||||
* M5 src/auth/ (login, persistent session secret)
|
||||
*/
|
||||
export async function buildApp(options: AppOptions = {}): Promise<FastifyInstance> {
|
||||
const app = Fastify({ logger: options.logger ?? false });
|
||||
const app = Fastify({ logger: options.logger ?? false, trustProxy: options.trustProxy ?? false });
|
||||
await registerAuth(app, options.auth ?? { dataDir: resolve(process.env.SHELLBOOK_DATA_DIR ?? "data") });
|
||||
await app.register(websocket);
|
||||
|
||||
app.get("/api/health", async () => ({ ok: true }));
|
||||
await registerStatic(app, options.staticDir ?? resolve("apps/web/dist"));
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user