diff options
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 25 |
1 files changed, 17 insertions, 8 deletions
@@ -2,7 +2,8 @@ const fs = require('fs'); const crypto = require('crypto'); const http = require('http'); -const fetch = require('node-fetch'); +// Import necessary for fetch.... +const _ = require('node:path'); // read from .env file without package var env = {}; @@ -25,6 +26,7 @@ const screenAndUpload = async () => { await runUpload(); } catch (error) { console.error('Error occurred while running screener and upload:', error); + return Error('Failed to run screener and upload'); } } @@ -38,22 +40,29 @@ const main = () => { // no https is necessary, will be running off a reverse proxy const server = http.createServer((req, res) => { // print all keys and values of req - console.log('Request Headers:'); + if (req.url == '/favicon.ico') { + res.writeHead(204); + res.end(); + return; + } if (req.url == `/api/update-stock-screener?token=${env.STOCK_SCREENER_AUTH_TOKEN}`) { screenAndUpload().then(() => { res.writeHead(200, { 'Content-Type': 'text/plain' }); - res.end('Stock Screener API'); + res.end('Success! Here is the url of the sheet -> https://docs.google.com/spreadsheets/d/1csTrnR5LwSBft1wtczDdxk0aPnHIdIOHX3MTP-IkpPk'); + console.log('Stock screener sucessfully updated on ' + new Date().toISOString() + ' from ' + (req.headers['x-forwarded-for'] || req.socket.remoteAddress)); }).catch((error) => { console.error('Error occurred while updating stock screener:', error); res.writeHead(500, { 'Content-Type': 'text/plain' }); res.end('Internal Server Error'); - }) + }); } else { // throw a 403 res.writeHead(403, { 'Content-Type': 'text/plain' }); res.end('Forbidden'); } }); + + // Start "sleeping" procs const PORT = env.PORT || 5000; server.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); @@ -61,8 +70,8 @@ const main = () => { // have a set interval that updates the stock prices weekly setInterval(() => { - console.log('running screen and upload!') - // screenAndUpload(); + console.log('Running screen and upload from interval!') + screenAndUpload(); }, 1000 * 60 * 60 * 24 * 7); } main(); @@ -536,11 +545,11 @@ const runUpload = async () => { // write the request to a file const outputPath = 'batch_update_request.json'; fs.writeFileSync(outputPath, JSON.stringify(batchUpdateRequest, null, 2)); - console.log(`Batch update request written to ${outputPath}`); + // console.log(`Batch update request written to ${outputPath}`); // post the batch update request try { const response = await postBatchUpdateRequest(batchUpdateRequest); - console.log('Batch update response:', response); + // console.log('Batch update response:', response); } catch (error) { console.error('Error:', error); } |