-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
178 lines (156 loc) · 4.51 KB
/
server.js
File metadata and controls
178 lines (156 loc) · 4.51 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import express from 'express';
import helmet from 'helmet';
import cors from 'cors';
import compression from 'compression';
import rateLimit from 'express-rate-limit';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { register } from 'prom-client';
import winston from 'winston';
import expressWinston from 'express-winston';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Initialize Express app
const app = express();
const PORT = process.env.PORT || 3000;
// Logger configuration
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.json(),
defaultMeta: { service: 'memecoingen' },
transports: [
new winston.transports.Console({
format: winston.format.simple(),
}),
],
});
// Security middleware
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'", "https:"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'", "https:"],
imgSrc: ["'self'", "data:", "https:"],
connectSrc: ["'self'", "https:", "wss:"],
fontSrc: ["'self'", "https:", "data:"],
objectSrc: ["'none'"],
mediaSrc: ["'self'"],
frameSrc: ["'none'"],
},
},
hsts: {
maxAge: 31536000,
includeSubDomains: true,
preload: true,
},
}));
// CORS configuration
const corsOptions = {
origin: process.env.ALLOWED_ORIGINS?.split(',') || ['https://memecoingen.com'],
credentials: true,
maxAge: 86400,
};
app.use(cors(corsOptions));
// Compression
app.use(compression());
// Body parsing
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Request logging
app.use(expressWinston.logger({
winstonInstance: logger,
meta: true,
msg: 'HTTP {{req.method}} {{req.url}}',
expressFormat: true,
colorize: false,
ignoreRoute: (req) => req.url === '/health' || req.url === '/metrics',
}));
// Rate limiting
const generalLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP, please try again later.',
standardHeaders: true,
legacyHeaders: false,
});
const deploymentLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 3, // limit each IP to 3 deployment requests per minute
message: 'Deployment rate limit exceeded. Please wait before deploying another coin.',
standardHeaders: true,
legacyHeaders: false,
skipSuccessfulRequests: false,
});
app.use('/api/', generalLimiter);
app.use('/api/deploy', deploymentLimiter);
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
environment: process.env.NODE_ENV,
});
});
// Metrics endpoint
app.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (err) {
res.status(500).end(err);
}
});
// API proxy to Convex backend
app.use('/api', createProxyMiddleware({
target: process.env.CONVEX_URL || 'https://your-convex-deployment.convex.cloud',
changeOrigin: true,
pathRewrite: {
'^/api': '',
},
onError: (err, req, res) => {
logger.error('Proxy error:', err);
res.status(502).json({ error: 'Bad Gateway' });
},
}));
// Serve static files in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'dist')));
// Catch all handler
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
}
// Error handling middleware
app.use(expressWinston.errorLogger({
winstonInstance: logger,
}));
app.use((err, req, res, next) => {
logger.error('Unhandled error:', err);
res.status(err.status || 500).json({
error: process.env.NODE_ENV === 'production'
? 'Internal Server Error'
: err.message,
});
});
// Graceful shutdown
const server = app.listen(PORT, () => {
logger.info(`Server running on port ${PORT}`);
});
process.on('SIGTERM', () => {
logger.info('SIGTERM signal received: closing HTTP server');
server.close(() => {
logger.info('HTTP server closed');
process.exit(0);
});
});
process.on('SIGINT', () => {
logger.info('SIGINT signal received: closing HTTP server');
server.close(() => {
logger.info('HTTP server closed');
process.exit(0);
});
});
export default app;