-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
53 lines (39 loc) · 1.29 KB
/
index.ts
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
import cors from 'cors';
import dotenv from 'dotenv';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
import express, { Request, Response } from 'express';
import launchRoutes from './routes/launchRoute';
import dynamicLinkRoutes from './routes/linkRoute';
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
app.get('/ping', (req: express.Request, res: express.Response) => {
res.status(200).json({ message: 'pong', status: 'OK' });
});
// Launch routes
app.use('/launch', launchRoutes);
// Dynamic Link routes
app.use('/links', dynamicLinkRoutes);
app.use('/', async (req: Request, res: Response) => {
try {
const link = await prisma.dynamicLink.findFirst();
if (!link) {
return res.status(404).json({ error: 'Link not found' });
}
return res.redirect(307,link.target);
} catch (error) {
console.error('Error redirecting to link target:', error);
return res.status(500).json({ error: 'Internal server error' });
}
});
app.use((err: Error, req: express.Request, res: express.Response, next: () => void) => {
if (err.name === 'UnauthorizedError') {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
});
app.listen(4000, () => {
console.log(`Listening on port ${4000}`);
});