-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.ts
More file actions
45 lines (35 loc) · 1.34 KB
/
server.ts
File metadata and controls
45 lines (35 loc) · 1.34 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
// Load environment variables FIRST before any other imports
import dotenv from 'dotenv';
dotenv.config();
import express, { Request, Response } from 'express';
import { chatHandler } from './routes/chat';
import { tokenHandler, jwksHandler } from './routes/oauth';
import { generateRSAKeyPair } from './utils/jwt-keys';
import { authenticateToken } from './middleware/auth';
import suggestionsRouter from './routes/suggestions';
// Initialize OAuth key pair on startup
generateRSAKeyPair();
console.log('OAuth RSA key pair generated');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true })); // For OAuth token endpoint (application/x-www-form-urlencoded)
// Healthcheck endpoint
app.get('/health', (req: Request, res: Response) => {
res.status(200).json({
status: 'ok',
timestamp: new Date().toISOString()
});
});
// Chat endpoints with fish-named security levels (minnow=insecure, shark=secure)
app.post('/:level/chat', chatHandler);
app.post('/authorized/:level/chat', authenticateToken, chatHandler);
// Smart reply suggestions endpoints
app.use(suggestionsRouter);
// OAuth endpoints
app.post('/oauth/token', tokenHandler);
app.get('/.well-known/jwks.json', jwksHandler);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});