-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
231 lines (197 loc) · 6.95 KB
/
server.js
File metadata and controls
231 lines (197 loc) · 6.95 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
230
231
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3000;
const DATA_FILE = path.join(__dirname, 'db.json');
const isDevelopment = process.env.NODE_ENV !== 'production';
// Development webpack middleware
if (isDevelopment) {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.config.js');
// Modify webpack config for hot reloading
webpackConfig.entry = [
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
webpackConfig.entry
];
webpackConfig.plugins = webpackConfig.plugins || [];
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
const compiler = webpack(webpackConfig);
// Use webpack dev middleware
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath || '/',
stats: {
colors: true,
chunks: false,
modules: false,
hash: false,
timings: true
}
}));
// Use webpack hot middleware
app.use(webpackHotMiddleware(compiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10 * 1000
}));
}
// Middleware
app.use(cors());
app.use(express.json());
// Initialize data file if it doesn't exist
function initializeData() {
try {
if (!fs.existsSync(DATA_FILE)) {
const initialData = {
public_keys: []
};
fs.writeFileSync(DATA_FILE, JSON.stringify(initialData, null, 2));
}
} catch (error) {
console.error('Error initializing data file:', error);
}
}
// Read data from file
function readData() {
try {
if (fs.existsSync(DATA_FILE)) {
const data = fs.readFileSync(DATA_FILE, 'utf8');
return JSON.parse(data);
}
return { public_keys: [] };
} catch (error) {
console.error('Error reading data file:', error);
return { public_keys: [] };
}
}
// Write data to file
function writeData(data) {
try {
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
} catch (error) {
console.error('Error writing data file:', error);
}
}
// Initialize data on startup
initializeData();
// Routes
// GET /public_keys - Get all public keys
app.get('/public_keys', (req, res) => {
try {
const data = readData();
res.json(data.public_keys);
} catch (error) {
console.error('Error getting public keys:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// POST /public_keys - Add a new public key
app.post('/public_keys', (req, res) => {
try {
const data = readData();
const newKey = req.body;
// Add timestamp if not present
if (!newKey.timestamp) {
newKey.timestamp = new Date().toISOString();
}
// Add the new key
data.public_keys.push(newKey);
// Write back to file
writeData(data);
res.status(201).json(newKey);
} catch (error) {
console.error('Error adding public key:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// GET /public_keys/:id - Get a specific public key by ID
app.get('/public_keys/:id', (req, res) => {
try {
const data = readData();
const key = data.public_keys.find(k => k.id === req.params.id);
if (!key) {
return res.status(404).json({ error: 'Public key not found' });
}
res.json(key);
} catch (error) {
console.error('Error getting public key:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// DELETE /public_keys/:id - Delete a specific public key by ID
app.delete('/public_keys/:id', (req, res) => {
try {
const data = readData();
const keyIndex = data.public_keys.findIndex(k => k.id === req.params.id);
if (keyIndex === -1) {
return res.status(404).json({ error: 'Public key not found' });
}
const deletedKey = data.public_keys.splice(keyIndex, 1)[0];
writeData(data);
res.json(deletedKey);
} catch (error) {
console.error('Error deleting public key:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'OK', timestamp: new Date().toISOString() });
});
// Environment configuration endpoint for frontend
app.get('/env-config', (req, res) => {
try {
const envConfig = {
REACT_APP_EHR_SERVER: process.env.REACT_APP_EHR_SERVER || null,
REACT_APP_CDS_SERVICE: process.env.REACT_APP_CDS_SERVICE || null,
REACT_APP_ORDER_SELECT: process.env.REACT_APP_ORDER_SELECT || null,
REACT_APP_ORDER_SIGN: process.env.REACT_APP_ORDER_SIGN || null,
REACT_APP_LAUNCH_URL: process.env.REACT_APP_LAUNCH_URL || null,
REACT_APP_FORM_EXPIRATION_DAYS: process.env.REACT_APP_FORM_EXPIRATION_DAYS || null,
REACT_APP_ALTERNATIVE_THERAPY: process.env.REACT_APP_ALTERNATIVE_THERAPY || null,
REACT_APP_PUBLIC_KEYS: process.env.REACT_APP_PUBLIC_KEYS || null,
REACT_APP_CLIENT: process.env.REACT_APP_CLIENT || null,
NODE_ENV: process.env.NODE_ENV || 'development'
};
// Remove null values to avoid overriding config file values
Object.keys(envConfig).forEach(key => {
if (envConfig[key] === null) {
delete envConfig[key];
}
});
res.json(envConfig);
} catch (error) {
console.error('Error getting environment config:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Serve static files in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'build')));
// Handle React routing - serve index.html for all non-API routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
}
// Start server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`Data file: ${DATA_FILE}`);
if (isDevelopment) {
console.log('Webpack dev middleware enabled for hot reloading');
}
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down gracefully');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('Received SIGINT, shutting down gracefully');
process.exit(0);
});