Added auth and request storage

This commit is contained in:
2026-08-31 12:28:18 +00:00
parent 6febaf327a
commit 1ca9648c09
21 changed files with 634 additions and 117 deletions
+25 -3
View File
@@ -1,12 +1,16 @@
import { Config } from './services/config.ts';
import { Database, MigrationService } from './services/storage/index.ts';
import { AuthSecp256k1 } from './auth/auth.ts';
import { Broadcaster } from './services/broadcaster.ts';
import { ApplicationRouter } from './services/router.ts';
import { HttpTransportRouter } from './services/transport/http-transport.ts';
import { WsTransportRouter } from './services/transport/ws-transport.ts';
import { ServerHost } from './services/server-host.ts';
import { Logger } from './utils/logger.ts';
import { DataRoute } from './routes/resources.ts';
import { AccountRoute } from './routes/account.ts';
import { Accounts } from './auth/accounts.ts';
/** Application composition root. */
export class App {
@@ -23,18 +27,23 @@ export class App {
const migrations = new MigrationService(database, debug);
await migrations.migrateToLatest();
// Create an Auth instance that can be passed in for signature validation
const auth = await AuthSecp256k1.create({ database }, { timestampWindowMs: config.auth.timestampWindowMs });
// Domain services are shared across all transports and route modules.
const broadcaster = new Broadcaster(debug);
const accounts = new Accounts();
const routes = [
// DataRoute owns resource read/write/subscribe logic and maps resource
// ids to broadcaster topics. timestampWindowMs controls write replay protection.
new DataRoute(database, broadcaster, config.auth.timestampWindowMs),
new DataRoute(database, broadcaster, auth, accounts),
// new AccountRoute(database, auth),
];
// Route loading is an explicit startup phase, not first-request work.
// ApplicationRouter.create validates every path and rejects duplicates
// before any client can connect.
const router = await ApplicationRouter.create(routes);
const router = await ApplicationRouter.create({ auth }, routes);
// Both transports share one ApplicationRouter. Routes use the shared
// Broadcaster directly, while HTTP and WebSocket remain protocol adapters.
@@ -43,7 +52,13 @@ export class App {
const ws = new WsTransportRouter(router, debug, config.server.maxRequestBodyBytes);
const host = new ServerHost(config, debug, [ http, ws ]);
return new App(host, database);
// Create the app instance
const app = new App(host, database);
// Start the unique request cleanup interval
app.startUniqueRequestCleanup(config.auth.uniqueRequestCleanupIntervalMs, config.auth.timestampWindowMs);
return app;
}
private stopPromise: Promise<void> | undefined;
@@ -69,6 +84,13 @@ export class App {
})();
await this.stopPromise;
}
startUniqueRequestCleanup(cleanupIntervalMs: number, timestampWindowMs: number): void {
// Every 10 seconds, we will cleanup the requests table
setInterval(async () => {
await this.database.db.deleteFrom('authed_requests').where('timestamp', '<', Date.now() - timestampWindowMs).execute();
}, cleanupIntervalMs);
}
}
const app = await App.create();