2025-12-11 17:43:08 +08:00
|
|
|
import express from 'express';
|
|
|
|
|
import cors from 'cors';
|
|
|
|
|
import helmet from 'helmet';
|
|
|
|
|
import morgan from 'morgan';
|
|
|
|
|
import { mastra } from '../mastra';
|
|
|
|
|
import { agentRouter } from './routes/agent-routes';
|
|
|
|
|
import { responseFormatter } from './middleware/response-formatter';
|
|
|
|
|
import { errorHandler, notFoundHandler } from './middleware/error-handler';
|
|
|
|
|
|
|
|
|
|
const app = express();
|
2025-12-12 10:59:51 +08:00
|
|
|
const PORT = process.env.PORT || 7328;
|
2025-12-11 17:43:08 +08:00
|
|
|
|
|
|
|
|
// Security middleware
|
|
|
|
|
app.use(helmet());
|
|
|
|
|
|
|
|
|
|
// CORS configuration
|
|
|
|
|
app.use(cors({
|
|
|
|
|
origin: process.env.CORS_ORIGIN || '*',
|
|
|
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
|
|
|
allowedHeaders: ['Content-Type', 'Authorization'],
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Logging middleware
|
|
|
|
|
app.use(morgan('combined'));
|
|
|
|
|
|
|
|
|
|
// Body parsing middleware
|
|
|
|
|
app.use(express.json({ limit: '10mb' }));
|
|
|
|
|
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
|
|
|
|
|
|
|
|
|
|
// Response formatting middleware
|
|
|
|
|
app.use(responseFormatter);
|
|
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
app.use('/api/agent', agentRouter);
|
|
|
|
|
|
|
|
|
|
// Health check endpoint
|
|
|
|
|
app.get('/health', (req, res) => {
|
|
|
|
|
res.json({
|
|
|
|
|
status: 'ok',
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
service: 'Multi-Function Agent API',
|
|
|
|
|
version: '1.0.0'
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Root endpoint
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
|
res.json({
|
|
|
|
|
message: 'Multi-Function Agent API',
|
|
|
|
|
endpoints: {
|
|
|
|
|
health: '/health',
|
|
|
|
|
agentInfo: '/api/agent',
|
|
|
|
|
generate: '/api/agent/generate',
|
|
|
|
|
stream: '/api/agent/stream',
|
|
|
|
|
testTools: '/api/agent/test-tools'
|
|
|
|
|
},
|
|
|
|
|
documentation: 'See API documentation for usage details'
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 404 handler - catch all unmatched routes
|
|
|
|
|
app.use(notFoundHandler);
|
|
|
|
|
|
|
|
|
|
// Error handling middleware - must be last
|
|
|
|
|
app.use(errorHandler);
|
|
|
|
|
|
|
|
|
|
// Start server
|
|
|
|
|
const server = app.listen(PORT, () => {
|
|
|
|
|
console.log(`🚀 Express server listening on port ${PORT}`);
|
|
|
|
|
console.log(`📡 Health check: http://localhost:${PORT}/health`);
|
|
|
|
|
console.log(`🤖 Agent API: http://localhost:${PORT}/api/agent`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Graceful shutdown
|
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
|
console.log('SIGTERM received, shutting down gracefully');
|
|
|
|
|
server.close(() => {
|
|
|
|
|
console.log('Server closed');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
|
console.log('SIGINT received, shutting down gracefully');
|
|
|
|
|
server.close(() => {
|
|
|
|
|
console.log('Server closed');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default app;
|