-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
123 lines (106 loc) · 3 KB
/
index.ts
File metadata and controls
123 lines (106 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Main Express Application
* Entry point for the chatbot API server
*/
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import { config } from "./src/config/index.js";
import { requestLogger } from "./src/middleware/requestLogger.js";
import { apiLimiter } from "./src/middleware/rateLimiter.js";
import {
errorHandler,
notFoundHandler,
} from "./src/middleware/errorHandler.js";
import v1Routes from "./src/routes/v1/index.js";
// Load environment variables
dotenv.config();
// Initialize Express app
const app = express();
// ============================================
// Global Middleware
// ============================================
/**
* CORS configuration
* Allows requests from configured frontend origins
*/
app.use(
cors({
origin: config.allowedOrigins,
credentials: true,
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization", "x-api-key"],
exposedHeaders: ["Content-Type"],
})
);
/**
* Request body parsing
*/
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
/**
* Request logging
*/
app.use(requestLogger);
/**
* Rate limiting for all API routes
*/
app.use("/api", apiLimiter);
// ============================================
// API Routes
// ============================================
/**
* Mount v1 API routes
*/
app.use("/api/v1", v1Routes);
/**
* Root endpoint
*/
app.get("/", (_req, res) => {
res.json({
name: "Chatbot Widget API",
version: "1.0.0",
status: "running",
endpoints: {
health: "/api/v1/health",
chat: "/api/v1/chat",
chatStream: "/api/v1/chat/stream",
documentUpload: "/api/v1/documents/upload",
documentQA: "/api/v1/documents/qa",
},
documentation: "See README.md for API documentation",
});
});
// ============================================
// Error Handling
// ============================================
/**
* 404 handler - must be after all routes
*/
app.use(notFoundHandler);
/**
* Global error handler - must be last
*/
app.use(errorHandler);
// ============================================
// Start Server
// ============================================
/**
* Start the Express server
*/
app.listen(config.port, () => {
/* eslint-disable no-console */
console.log(`\n🚀 Server started successfully!`);
console.log(`📡 Port: ${config.port}`);
console.log(`🌍 Environment: ${config.nodeEnv}`);
console.log(`🤖 AI Model: ${config.ai.model}`);
console.log(`🔗 Base URL: http://localhost:${config.port}`);
console.log(`\n📚 Available endpoints:`);
console.log(` GET /api/v1/health - Health check`);
console.log(` POST /api/v1/chat - Chat (non-streaming)`);
console.log(` POST /api/v1/chat/stream - Chat (streaming)`);
console.log(` POST /api/v1/documents/upload - Upload PDF document`);
console.log(` POST /api/v1/documents/qa - Q&A over document`);
/* eslint-enable no-console */
});
export default app;