-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (131 loc) Β· 4.36 KB
/
Copy pathserver.js
File metadata and controls
155 lines (131 loc) Β· 4.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
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
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
import path from 'path';
import { fileURLToPath } from 'url';
// Import routes
import mockupRoutes from './routes/mockupRoutes.js';
// Import utilities
import { setupDirectories, validateEnvironment } from './utils/setup.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 5002;
// Security middleware
app.use(helmet());
// CORS configuration
const corsOptions = {
origin: [
'http://localhost:3000',
'http://localhost:3001',
'http://localhost:5000',
'http://localhost:5001',
'http://127.0.0.1:3000',
'http://127.0.0.1:5000',
'https://yourdomain.com', // Add your production domain
'https://www.yourdomain.com'
],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
};
app.use(cors(corsOptions));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: {
error: 'Too many requests from this IP, please try again later.',
retryAfter: '15 minutes'
},
standardHeaders: true,
legacyHeaders: false
});
app.use('/api/', limiter);
// Body parsing middleware
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// Note: Static file serving removed - images are returned directly in responses
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
environment: process.env.NODE_ENV || 'development'
});
});
// API routes
app.use('/api/mockup', mockupRoutes);
// Root endpoint
app.get('/', (req, res) => {
res.json({
message: 'Mockup Generator API Server',
version: '1.0.0',
endpoints: {
health: '/health',
mockup: '/api/mockup',
documentation: '/api/mockup/docs'
},
status: 'running'
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({
error: 'Endpoint not found',
message: `Cannot ${req.method} ${req.originalUrl}`,
availableEndpoints: [
'GET /health',
'GET /',
'POST /api/mockup/generate',
'GET /api/mockup/products',
'GET /api/mockup/docs'
]
});
});
// Global error handler
app.use((err, req, res, next) => {
console.error('Global error handler:', err);
res.status(err.status || 500).json({
error: 'Internal server error',
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong',
...(process.env.NODE_ENV === 'development' && { stack: err.stack })
});
});
// Initialize server
async function startServer() {
try {
// Validate environment and setup directories
await validateEnvironment();
await setupDirectories();
// Start server
app.listen(PORT, () => {
console.log(`
π Mockup Generator API Server Started
ββββββββββββββββββββββββββββββββββββββββ
π Server: http://localhost:${PORT}
π₯ Health: http://localhost:${PORT}/health
π API Docs: http://localhost:${PORT}/api/mockup/docs
π Mockups: http://localhost:${PORT}/mockups/
ββββββββββββββββββββββββββββββββββββββββ
`);
});
} catch (error) {
console.error('Failed to start server:', error);
process.exit(1);
}
}
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('SIGINT received, shutting down gracefully');
process.exit(0);
});
// Start the server
startServer();
export default app;