forked from github/codespaces-express
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (70 loc) · 2.49 KB
/
index.js
File metadata and controls
82 lines (70 loc) · 2.49 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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const { authenticateToken, optionalAuth } = require('./middleware/auth');
const app = express();
const port = process.env.PORT || 3000;
// --- MIDDLEWARE ---
app.use(express.json());
// CORS (adjust origin in production)
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
// Global rate limiting
const globalLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 60,
message: { error: 'Too many requests, slow down!' }
});
app.use(globalLimiter);
// --- ROUTES ---
// Root endpoint (public)
app.get('/', (req, res) => {
res.json({
message: 'API is running',
version: 'v1',
endpoints: {
account: '/api/v1/account',
billing: '/api/v1/billing',
promotions: '/api/v1/promotions',
domains: '/api/v1/domains',
bots: '/api/v1/bots',
webHosting: '/api/v1/web-hosting',
tools: '/api/v1/tools',
vpTest: '/api/v1/vp-test'
}
});
});
// --- IMPORT ROUTES ---
const accountRoutes = require('./routes/account');
const billingRoutes = require('./routes/billing');
const promotionsRoutes = require('./routes/promotions');
const domainsRoutes = require('./routes/domains');
const botsRoutes = require('./routes/bots');
const webHostingRoutes = require('./routes/web-hosting');
const toolsRoutes = require('./routes/tools');
const vpTestRoutes = require('./routes/vpTest');
// --- PUBLIC ROUTES (OPTIONAL AUTH) ---
app.use('/api/v1/vp-test', optionalAuth, vpTestRoutes);
// --- PROTECTED ROUTES (AUTH REQUIRED) ---
app.use('/api/v1/account', authenticateToken, accountRoutes);
app.use('/api/v1/billing', authenticateToken, billingRoutes);
app.use('/api/v1/promotions', authenticateToken, promotionsRoutes);
app.use('/api/v1/domains', authenticateToken, domainsRoutes);
app.use('/api/v1/bots', authenticateToken, botsRoutes);
app.use('/api/v1/web-hosting', authenticateToken, webHostingRoutes);
app.use('/api/v1/tools', authenticateToken, toolsRoutes);
app.use('/api/v1/vp-info', authenticateToken, vpTestRoutes);
// --- GLOBAL ERROR HANDLER ---
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});
// --- START SERVER ---
app.listen(port, () => {
console.log(`API server listening on port ${port}`);
console.log(`Base URL: http://localhost:${port}/api/v1`);
});