-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
229 lines (211 loc) · 9.08 KB
/
Copy pathserver.js
File metadata and controls
229 lines (211 loc) · 9.08 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import compression from 'compression';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import connectDB from './config/db.js';
import mongo_controller from './controllers/controller_DB.js';
import performanceMonitor from './middleware/performanceMonitor.js';
import aggregations from './routes/api/aggregations.js';
import api from './routes/api/api.js';
import combine_files from './routes/api/combine_files.js';
import generateFile from './routes/api/generateDataAPIsFile.js';
import generateFileClean from './routes/api/generateDataClean.js';
import optimized from './routes/api/optimized.js';
import apiRegistration from './routes/api/apiRegistration.js';
import publicApi from './routes/api/publicApi.js';
import swaggerDocs from './routes/api/swagger.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Dotenv config
dotenv.config();
// Connect database
connectDB();
const app = express();
// Use compression middleware to compress responses
app.use(
compression({
// Only compress responses that are larger than this threshold
threshold: 1024, // 1KB
// Compression level (0-9, where 9 is best compression but slowest)
level: 6,
// Only compress certain content types
filter: (req, res) => {
// Don't compress if the request includes a cache-control: no-transform directive
if (req.headers['cache-control'] && req.headers['cache-control'].includes('no-transform')) {
return false;
}
// Fallback to standard filter function
return compression.filter(req, res);
},
}),
);
// Middleware
app.use(performanceMonitor); // Add performance monitoring first
app.use(cors());
app.use(express.json({ limit: '400mb' })); // ADDED: limit option
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Define headers used for API requisitions
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Token, Authorization');
next();
});
// Normalize GLASS country names to match AMRnet's getCountryDisplayName format
function normalizeGLASSCountry(name) {
if (!name) return '';
const map = {
'United Kingdom of Great Britain and Northern Ireland': 'United Kingdom',
'United States of America': 'United States of America',
'Iran (Islamic Republic of)': 'Iran',
'Republic of Korea': 'South Korea',
'Republic of Moldova': 'Moldova',
'Russian Federation': 'Russia',
'Viet Nam': 'Vietnam',
'Lao People\'s Democratic Republic': 'Laos',
'Syrian Arab Republic': 'Syria',
'United Republic of Tanzania': 'Tanzania',
'Türkiye': 'Turkey',
'Czechia': 'Czechia',
'Czech Republic': 'Czechia',
'Bolivia (Plurinational State of)': 'Bolivia',
'Venezuela (Bolivarian Republic of)': 'Venezuela',
'Democratic People\'s Republic of Korea': 'North Korea',
'Democratic Republic of the Congo': 'Dem. Rep. Congo',
'State of Palestine': 'Palestine',
'Congo': 'Congo',
'Eswatini': 'Eswatini',
'Côte d\'Ivoire': "Côte d'Ivoire",
'The Netherlands': 'Netherlands',
'The Gambia': 'Gambia',
'Dominican Republic': 'Dominican Rep.',
'Central African Republic': 'Central African Rep.',
'Brunei Darussalam': 'Brunei',
'Republic of North Macedonia': 'North Macedonia',
'Bosnia and Herzegovina': 'Bosnia and Herzegovina',
'United Arab Emirates': 'United Arab Emirates',
'Saudi Arabia': 'Saudi Arabia',
};
return map[name] || name.trim();
}
// GLASS compiled phenotypic data proxy (from qleclerc/GLASS2022 GitHub repo)
let glassCSVCache = null;
let glassCSVCacheTime = 0;
const GLASS_CSV_CACHE_MS = 24 * 60 * 60 * 1000; // 24 hours
app.get('/api/glass-phenotypic', async (req, res) => {
try {
const now = Date.now();
if (glassCSVCache && (now - glassCSVCacheTime) < GLASS_CSV_CACHE_MS) {
return res.json(glassCSVCache);
}
const csvUrl = 'https://raw.githubusercontent.com/qleclerc/GLASS2022/master/compiled_WHO_GLASS_2022.csv';
const response = await fetch(csvUrl);
if (!response.ok) throw new Error(`GitHub returned ${response.status}`);
const text = await response.text();
const lines = text.split('\n').filter(l => l.trim());
const headers = lines[0].split(',').map(h => h.replace(/"/g, '').trim());
const data = [];
for (let i = 1; i < lines.length; i++) {
const values = lines[i].split(',').map(v => v.replace(/"/g, '').trim());
if (values.length < headers.length) continue;
const row = {};
headers.forEach((h, j) => { row[h] = values[j]; });
// Include relevant specimen types for different organisms
if (['BLOOD', 'STOOL', 'URINE', 'GENITAL'].includes(row.Specimen)) {
data.push({
country: normalizeGLASSCountry(row.CountryTerritoryArea),
iso3: row.Iso3,
region: row.WHORegionName,
year: parseInt(row.Year),
specimen: row.Specimen,
pathogen: row.PathogenName,
antibiotic: row.AbTargets,
tested: parseInt(row.InterpretableAST) || 0,
resistant: parseInt(row.Resistant) || 0,
percentResistant: parseFloat(row.PercentResistant) || 0,
});
}
}
glassCSVCache = data;
glassCSVCacheTime = now;
console.log(`[GLASS Phenotypic] Parsed ${data.length} records from GLASS CSV`);
res.json(data);
} catch (error) {
console.error('[GLASS Phenotypic]', error.message);
res.status(502).json({ error: 'Failed to fetch GLASS phenotypic data' });
}
});
// GLASS data from MongoDB (populated by scripts/check-glass-data.js)
app.get('/api/glass-mongodb', async (req, res) => {
try {
const client = await connectDB();
const col = client.db('amrnet_admin').collection('glass_data');
const count = await col.estimatedDocumentCount();
if (count === 0) {
return res.status(404).json({ error: 'No GLASS data in MongoDB. Run: node scripts/check-glass-data.js' });
}
const ghoRecords = await col.find({ source: 'GHO_API' }).toArray();
const csvRecords = await col.find({ source: 'GLASS_CSV' }).toArray();
// Build the same structure as fetchGLASSData()
const consumption = ghoRecords.filter(r => r.indicator === 'GLASSAMC_TC');
const resistance = {
ecoli_3gc: ghoRecords.filter(r => r.indicator === 'AMR_INFECT_ECOLI'),
mrsa: ghoRecords.filter(r => r.indicator === 'AMR_INFECT_MRSA'),
ng_ciprofloxacin: ghoRecords.filter(r => r.indicator === 'GASPRSCIP'),
ng_azithromycin: ghoRecords.filter(r => r.indicator === 'GASPRSAZM'),
ng_ceftriaxone: ghoRecords.filter(r => r.indicator === 'GASPRSCRO'),
ng_cefixime: ghoRecords.filter(r => r.indicator === 'GASPRSCFM'),
};
res.json({ consumption, resistance, phenotypic: csvRecords, source: 'mongodb' });
} catch (error) {
console.error('[GLASS MongoDB]', error.message);
res.status(500).json({ error: 'Failed to read GLASS data from MongoDB' });
}
});
// GHO OData API proxy (avoids CORS issues with WHO API)
app.get('/api/gho/:indicator', async (req, res) => {
try {
const indicator = req.params.indicator;
// Whitelist allowed indicators to prevent abuse
const allowed = ['GLASSAMC_TC', 'GLASSAMC_AWARE', 'AMR_INFECT_ECOLI', 'AMR_INFECT_MRSA', 'GASPRSCIP', 'GASPRSAZM', 'GASPRSCRO', 'GASPRSCFM', 'GASPRSESC'];
if (!allowed.includes(indicator)) {
return res.status(400).json({ error: 'Invalid indicator' });
}
const response = await fetch(`https://ghoapi.azureedge.net/api/${indicator}`);
if (!response.ok) throw new Error(`GHO API returned ${response.status}`);
const data = await response.json();
res.json(data);
} catch (error) {
console.error('[GHO Proxy]', error.message);
res.status(502).json({ error: 'Failed to fetch from WHO GHO API' });
}
});
// Public API with Swagger docs and self-service registration
app.use('/api-register', apiRegistration);
app.use('/api-docs', swaggerDocs);
app.use('/api/v1', publicApi);
// Define routes API here
app.use('/api', aggregations);
app.use('/api', api);
app.use('/api/optimized', optimized);
app.use('/api/file', generateFile);
app.use('/api/data', generateFileClean);
app.use('/api/combine', combine_files);
app.use('/api/mongo', mongo_controller);
app.use(express.static(path.join(__dirname, './client', 'build')));
// If no API routes are hit, send the React app (but not for /api-docs or /api/v1)
app.use('/', function (req, res, next) {
if (req.path.startsWith('/api-docs') || req.path.startsWith('/api/v1') || req.path.startsWith('/api-register')) {
return next();
}
res.sendFile(path.join(__dirname, './client/build/index.html'));
});
// Set the port from environment variable or default to 3000
const PORT = process.env.PORT || 3000;
// Start the API server and log a message when it's ready
const server = app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT} (${process.env.NODE_ENV || 'development'} mode)`);
});