stock-bot/apps/stock/web-api/src/routes/create-routes.ts

82 lines
3.4 KiB
TypeScript

/**
* Route factory for web API service
* Creates routes with access to the service container
*/
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import type { HttpSecurity } from '@stock-bot/di';
import type { IServiceContainer } from '@stock-bot/handlers';
import { createWebApiSecurity } from '../http-security';
import { createResearchServices } from '../services/research-services';
import { createBacktestRoutes } from './backtest.routes';
import { createBacktestV2Routes } from './backtests-v2.routes';
import { createDatasetRoutes } from './datasets.routes';
import { createExchangeRoutes } from './exchange.routes';
import { createExperimentRoutes } from './experiments.routes';
import { createHealthRoutes } from './health.routes';
import { createMonitoringRoutes } from './monitoring.routes';
import { createPaperDeploymentRoutes } from './paper-deployments.routes';
import { createPipelineRoutes } from './pipeline.routes';
import { createResearchAgentRoutes } from './research-agents.routes';
import { createResearchRoutes } from './research.routes';
import { createStrategyRoutes } from './strategies.routes';
// Source exports carry complete datasets. Keep the web transport from imposing
// the old 1 MiB restriction; normalizers still validate every submitted record.
export const WEB_API_MAX_REQUEST_BODY_SIZE = Number.MAX_SAFE_INTEGER;
export function createRoutes(
container: IServiceContainer,
suppliedSecurity?: HttpSecurity,
integrations?: { paperOrchestratorOrigin?: string }
): Hono {
const security = suppliedSecurity ?? createWebApiSecurity(container);
const app = new Hono();
// Add CORS middleware
app.use(
'*',
cors({
origin: security.allowedOrigins,
allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization', 'Idempotency-Key'],
credentials: true,
})
);
security.mount(app);
const researchServices = createResearchServices(container);
// Create routes with container
const healthRoutes = createHealthRoutes(container);
const exchangeRoutes = createExchangeRoutes(container);
const monitoringRoutes = createMonitoringRoutes(container, {
origin: integrations ? integrations.paperOrchestratorOrigin : process.env.ORCHESTRATOR_URL,
});
const pipelineRoutes = createPipelineRoutes(container);
const backtestRoutes = createBacktestRoutes(container);
const backtestV2Routes = createBacktestV2Routes(container);
// Mount routes
app.route('/health', healthRoutes);
app.route('/api/exchanges', exchangeRoutes);
app.route('/api/system/monitoring', monitoringRoutes);
app.route('/api/pipeline', pipelineRoutes);
app.route('/', backtestRoutes); // Mounted at root since routes already have /api prefix
app.route('/', backtestV2Routes); // V2 routes also mounted at root
app.route('/', createDatasetRoutes(container));
app.route('/', createStrategyRoutes(container, researchServices.catalog));
app.route('/', createResearchRoutes(container, researchServices.research));
app.route('/', createResearchAgentRoutes(container, security.researchEnabled));
app.route('/', createExperimentRoutes(container, researchServices));
app.route(
'/',
createPaperDeploymentRoutes(container, {
service: researchServices.paperDeployments,
origin: integrations ? integrations.paperOrchestratorOrigin : process.env.ORCHESTRATOR_URL,
origins: security.allowedOrigins,
})
);
return app;
}