72 lines
3 KiB
TypeScript
72 lines
3 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 { createHttpSecurity, httpSecurityOptions, type HttpSecurity } from '@stock-bot/di';
|
|
import type { IServiceContainer } from '@stock-bot/handlers';
|
|
import { ResearchAgentsService } from '../services/research-agents.service';
|
|
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 { 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): Hono {
|
|
const agents = new ResearchAgentsService(container);
|
|
const security =
|
|
suppliedSecurity ??
|
|
createHttpSecurity({
|
|
...httpSecurityOptions(),
|
|
browserSessions: true,
|
|
resolveResearchCredential: request => agents.authenticate(request),
|
|
});
|
|
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);
|
|
|
|
// Create routes with container
|
|
const healthRoutes = createHealthRoutes(container);
|
|
const exchangeRoutes = createExchangeRoutes(container);
|
|
const monitoringRoutes = createMonitoringRoutes(container);
|
|
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));
|
|
app.route('/', createResearchRoutes(container));
|
|
app.route('/', createResearchAgentRoutes(container, security.researchEnabled));
|
|
app.route('/', createExperimentRoutes(container));
|
|
|
|
return app;
|
|
}
|