-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (81 loc) · 2.36 KB
/
server.js
File metadata and controls
94 lines (81 loc) · 2.36 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
import express from 'express'
import cors from 'cors'
import dotenv from 'dotenv'
// Load environment variables
dotenv.config()
const app = express()
const PORT = process.env.PORT || 3000
// Middleware
app.use(cors({
origin: true,
credentials: true
}))
app.use(express.json())
// Import and convert the API handlers
import createConversationHandler from './api/create-conversation.js'
import checkConversationStatusHandler from './api/check-conversation-status.js'
// Helper function to convert Express req/res to Vercel handler format
const createVercelReq = (req) => ({
method: req.method,
headers: req.headers,
body: req.body,
query: req.query,
url: req.url
})
const createVercelRes = (res) => {
let statusCode = 200
const vercelRes = {
status: (code) => {
statusCode = code
return vercelRes
},
json: (data) => {
res.status(statusCode).json(data)
},
end: () => {
res.status(statusCode).end()
},
setHeader: (name, value) => {
res.setHeader(name, value)
},
getHeader: (name) => {
return res.getHeader(name)
}
}
return vercelRes
}
// API Routes - Handle OPTIONS for CORS preflight
app.options('/api/create-conversation', (req, res) => {
res.status(200).end()
})
app.options('/api/check-conversation-status', (req, res) => {
res.status(200).end()
})
app.post('/api/create-conversation', async (req, res) => {
const vercelReq = createVercelReq(req)
const vercelRes = createVercelRes(res)
try {
await createConversationHandler(vercelReq, vercelRes)
} catch (error) {
console.error('[server] Error in create-conversation handler:', error)
if (!res.headersSent) {
res.status(500).json({ error: 'Internal server error', message: error.message })
}
}
})
app.get('/api/check-conversation-status', async (req, res) => {
const vercelReq = createVercelReq(req)
const vercelRes = createVercelRes(res)
try {
await checkConversationStatusHandler(vercelReq, vercelRes)
} catch (error) {
console.error('[server] Error in check-conversation-status handler:', error)
if (!res.headersSent) {
res.status(500).json({ error: 'Internal server error', message: error.message })
}
}
})
app.listen(PORT, () => {
console.log(`API server running on http://localhost:${PORT}`)
console.log(`API endpoint: http://localhost:${PORT}/api/create-conversation`)
})