Files
sentryagent-idp/src/server.ts
SentryAgent.ai Developer 7328a61c44 feat(phase-2): workstream 5 — OPA Policy Engine
- policies/authz.rego: Rego policy with path normalisation and scope enforcement
- policies/data/scopes.json: all 13 endpoint → scope mappings
- src/middleware/opa.ts: OpaMiddleware with Wasm primary path + scopes.json fallback;
  exports createOpaMiddleware() and reloadOpaPolicy() for SIGHUP hot-reload
- All four route files: opaMiddleware wired after authMiddleware
- AuditController, OAuth2Service: manual scope checks removed (now centralised in OPA)
- src/server.ts: SIGHUP handler calls reloadOpaPolicy()
- docs/devops/environment-variables.md: POLICY_DIR documented
- 38 new tests; 302/302 passing; opa.ts coverage 98.66% statements

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-28 23:02:11 +00:00

57 lines
1.4 KiB
TypeScript

/**
* Server entry point for SentryAgent.ai AgentIdP.
* Loads environment variables, creates the app, and starts listening.
*/
import * as dotenv from 'dotenv';
dotenv.config();
import { createApp } from './app.js';
import { reloadOpaPolicy } from './middleware/opa.js';
const PORT = parseInt(process.env['PORT'] ?? '3000', 10);
/**
* Bootstraps the application and starts the HTTP server.
*/
async function main(): Promise<void> {
try {
const app = await createApp();
const server = app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`SentryAgent.ai AgentIdP listening on port ${PORT}`);
});
// Graceful shutdown
const shutdown = (): void => {
// eslint-disable-next-line no-console
console.log('Shutting down gracefully...');
server.close(() => {
process.exit(0);
});
};
process.on('SIGTERM', () => {
shutdown();
});
process.on('SIGINT', () => {
shutdown();
});
// Hot-reload OPA policy without restarting the server
process.on('SIGHUP', () => {
reloadOpaPolicy().catch((err) => {
// eslint-disable-next-line no-console
console.error('[AgentIdP] Failed to reload OPA policy:', err);
});
});
} catch (err) {
// eslint-disable-next-line no-console
console.error('Failed to start server:', err);
process.exit(1);
}
}
void main();