|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2026, Optimizely |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Simple HTTP server to serve the datafile for testing polling project config manager |
| 21 | + * Runs at http://localhost:PORT |
| 22 | + */ |
| 23 | + |
| 24 | +import http from 'http'; |
| 25 | +import fs from 'fs'; |
| 26 | +import path from 'path'; |
| 27 | +import { fileURLToPath } from 'url'; |
| 28 | +import { dirname } from 'path'; |
| 29 | + |
| 30 | +const __filename = fileURLToPath(import.meta.url); |
| 31 | +const __dirname = dirname(__filename); |
| 32 | + |
| 33 | +const PORT = 8910; |
| 34 | +const datafilePath = path.join(__dirname, 'datafile.json'); |
| 35 | + |
| 36 | +const server = http.createServer((req, res) => { |
| 37 | + console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`); |
| 38 | + |
| 39 | + // Read and serve the datafile |
| 40 | + fs.readFile(datafilePath, 'utf8', (err, data) => { |
| 41 | + if (err) { |
| 42 | + console.error('Error reading datafile:', err); |
| 43 | + res.writeHead(500, { 'Content-Type': 'application/json' }); |
| 44 | + res.end(JSON.stringify({ error: 'Failed to read datafile' })); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Set CORS headers for cross-origin requests |
| 49 | + res.writeHead(200, { |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + 'Access-Control-Allow-Origin': '*', |
| 52 | + 'Access-Control-Allow-Methods': 'GET, OPTIONS', |
| 53 | + 'Access-Control-Allow-Headers': 'Content-Type' |
| 54 | + }); |
| 55 | + |
| 56 | + res.end(data); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +server.listen(PORT, () => { |
| 61 | + console.log(`\n=== Datafile Server Started ===`); |
| 62 | + console.log(`Server running at http://localhost:${PORT}`); |
| 63 | + console.log(`Serving datafile from: ${datafilePath}`); |
| 64 | + console.log(`\nPress Ctrl+C to stop the server\n`); |
| 65 | +}); |
| 66 | + |
| 67 | +// Handle graceful shutdown |
| 68 | +const shutdown = () => { |
| 69 | + console.log('\n\nShutting down datafile server...'); |
| 70 | + server.close(() => { |
| 71 | + console.log('Server stopped'); |
| 72 | + process.exit(0); |
| 73 | + }); |
| 74 | +}; |
| 75 | + |
| 76 | +process.on('SIGINT', shutdown); |
| 77 | +process.on('SIGTERM', shutdown); |
0 commit comments