forked from creatorcluster/renderdragon.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (83 loc) · 2.73 KB
/
server.js
File metadata and controls
92 lines (83 loc) · 2.73 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
import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import { Readable } from 'stream';
import infoHandler from './api/info.js';
import downloadHandler from './api/download.js';
import downloadThumbnailHandler from './api/downloadThumbnail.js';
import generateTitlesHandler from './api/generateTitles.js';
import deleteAccountHandler from './api/deleteAccount.js';
import { createRouteHandler } from 'uploadthing/express';
import { uploadRouter } from './src/integrations/uploadthing/router.js';
const app = express();
const port = 3000;
const allowedOrigins = [
'http://localhost:5173',
'http://localhost:3000',
'https://renderdragon.org',
'https://assets-api-worker.powernplant101-c6b.workers.dev'
];
app.use(cors({
origin: function (origin, callback) {
// allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
}));
app.use(express.json());
const createAdapter = (handler) => (req, res) => {
const vercelReq = {
method: req.method,
headers: req.headers,
body: req.body,
url: `http://${req.headers.host}${req.originalUrl}`,
};
handler(vercelReq).then(response => {
if (!response) {
if (!res.headersSent) {
res.status(500).send("Handler returned no response.");
}
return;
}
res.status(response.status);
response.headers.forEach((value, key) => {
res.setHeader(key, value);
});
if (response.body) {
Readable.fromWeb(response.body).pipe(res);
} else {
res.end();
}
}).catch(error => {
console.error("Handler error:", error);
if (!res.headersSent) {
res.status(500).json({ message: 'Internal Server Error' });
}
});
};
app.all('/api/info', createAdapter(infoHandler));
app.all('/api/download', createAdapter(downloadHandler));
app.all('/api/downloadThumbnail', createAdapter(downloadThumbnailHandler));
app.all('/api/generateTitles', createAdapter(generateTitlesHandler));
app.all('/api/deleteAccount', createAdapter(deleteAccountHandler));
// UploadThing route
app.use(
'/api/uploadthing',
createRouteHandler({
router: uploadRouter,
// Explicitly pass token from env per UploadThing v7 docs
config: {
token: process.env.UPLOADTHING_TOKEN,
},
})
);
app.listen(port, () => {
console.log(`[server]: Server is running at http://localhost:${port}`);
});