forked from GSA-TTS/10x-vop-pra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
194 lines (166 loc) · 6.23 KB
/
server.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const generateWord = require('./server/generate-word');
const RateLimit = require('express-rate-limit');
const mysql = require('mysql2/promise');
const { v4: uuidv4 } = require('uuid');
const app = express();
const port = process.env.PORT || 3000;
// Enable 'trust proxy' for proxies like Cloud.gov
app.set('trust proxy', 1);
// Middleware
app.use(bodyParser.json());
app.use(cors());
app.use(express.static(path.join(__dirname, '_site')));
app.use('/uploads', express.static(path.join(__dirname, '_site/uploads')));
// Rate limiter middleware
const limiter = RateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Max 100 requests per windowMs
});
// Configure MySQL pool
function getDbConfig() {
// Check if we're running locally
if (process.env.NODE_ENV === 'development') {
console.log('Using local database configuration');
// Ensure all credentials come from environment variables
if (!process.env.DB_USER || !process.env.DB_HOST || !process.env.DB_NAME || !process.env.DB_PASSWORD) {
throw new Error('Local database configuration environment variables are missing');
}
return {
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT || 3306,
};
}
// Cloud.gov production settings
if (!process.env.VCAP_SERVICES) {
throw new Error('VCAP_SERVICES environment variable is missing in production');
}
try {
const vcapServices = process.env.VCAP_SERVICES;
const parsedServices = JSON.parse(vcapServices);
if (!parsedServices['aws-rds'] || !parsedServices['aws-rds'][0]) {
throw new Error('Database service configuration not found in VCAP_SERVICES');
}
const credentials = parsedServices['aws-rds'][0].credentials;
if (!credentials) {
throw new Error('Database credentials not found in VCAP_SERVICES');
}
// Only log non-sensitive configuration data
console.log('Using Cloud.gov database configuration');
return {
host: credentials.host,
port: credentials.port,
database: credentials.db_name,
user: credentials.username,
password: credentials.password
};
} catch (error) {
console.error('Error configuring database:', error);
throw error;
}
}
const pool = mysql.createPool(getDbConfig());
// Routes
app.post('/generate-word', limiter, async (req, res) => {
console.log('POST /generate-word received');
console.log('Request body:', req.body);
try {
await generateWord(req, res);
} catch (error) {
console.error('Error in /generate-word:', error);
res.status(500).json({
success: false,
error: error.message || 'Failed to generate the document.',
details: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
app.post('/save-progress', limiter, async (req, res) => {
try {
const { token, formData } = req.body;
// Ensure formData is stringified properly
const formDataString = typeof formData === 'string' ? formData : JSON.stringify(formData);
if (!token) {
// New session
const newToken = uuidv4();
await pool.execute(
'INSERT INTO form_progress (token, form_data) VALUES (?, ?)',
[newToken, formDataString]
);
res.json({ token: newToken });
} else {
// Update existing session
await pool.execute(
'UPDATE form_progress SET form_data = ?, last_updated = CURRENT_TIMESTAMP WHERE token = ?',
[formDataString, token]
);
res.json({ token });
}
} catch (error) {
console.error('Error saving progress:', error);
console.error('Form data received:', req.body); // Add this for debugging
res.status(500).json({ error: 'Failed to save progress' });
}
});
// Add the S3 test endpoint here
app.get('/test-s3', async (req, res) => {
try {
const { client: s3Client, bucketName } = configureS3();
const testFile = Buffer.from('Hello World');
const testKey = `test-${Date.now()}.txt`;
const uploadResult = await uploadToS3(bucketName, testKey, testFile);
res.json({
success: true,
message: 'Test file uploaded successfully',
fileUrl: uploadResult
});
} catch (error) {
console.error('S3 test failed:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});
app.get('/load-progress/:token', limiter, async (req, res) => {
try {
const { token } = req.params;
const [rows] = await pool.execute(
'SELECT form_data FROM form_progress WHERE token = ?',
[token]
);
if (rows.length === 0) {
res.status(404).json({ error: 'Session not found' });
} else {
// Safely parse the JSON
try {
const formData = typeof rows[0].form_data === 'string'
? JSON.parse(rows[0].form_data)
: rows[0].form_data;
res.json(formData);
} catch (parseError) {
console.error('Error parsing form data:', parseError);
console.error('Raw form data:', rows[0].form_data);
res.status(500).json({ error: 'Invalid form data format' });
}
}
} catch (error) {
console.error('Error loading progress:', error);
res.status(500).json({ error: 'Failed to load progress' });
}
});
// Fallback route
app.use((req, res) => {
res.status(404).send({ error: 'Route not found.' });
});
// Start server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});