From 34e2dcbc5f00d946d1c4b269712072e9e60fff43 Mon Sep 17 00:00:00 2001 From: Pete Walker <108812366+petertimwalker@users.noreply.github.com> Date: Sat, 24 Aug 2024 18:10:13 -0400 Subject: [PATCH] refactor and allow dev environment --- server.js | 60 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/server.js b/server.js index 6ae4348..84e310f 100644 --- a/server.js +++ b/server.js @@ -4,19 +4,30 @@ const dotenv = require('dotenv'); dotenv.config(); const booksRouter = require('./books'); const cors = require('cors'); +const isProduction = process.env.NODE_ENV === 'production'; +const API_KEY = process.env.API_KEY || 'API_KEY not defined'; +const PORT = isProduction ? 443 : 3001; const allowedOrigins = ['https://peterwalker.xyz']; -app.use(cors({ - origin: (origin, callback) => { - if (!origin) { - return callback(new Error('Origin not specified'), false); - } - if (!allowedOrigins.includes(origin)) { - return callback(new Error('Not allowed by CORS'), false); - } - return callback(null, true); - } -})); +if (isProduction) { + app.use( + cors({ + origin: (origin, callback) => { + if (!origin) { + return callback(new Error('Origin not specified'), false); + } + if (!allowedOrigins.includes(origin)) { + return callback(new Error('Not allowed by CORS'), false); + } + return callback(null, true); + }, + }), + ); + createHTTPSServer(); +} else { + app.use(cors()); + createHTTPServer(); +} app.use((req, res, next) => { const origin = req.headers.origin; @@ -24,12 +35,6 @@ app.use((req, res, next) => { next(); }); -const isProduction = process.env.NODE_ENV === 'production'; -const PORT = isProduction ? 443 : 3001; - -// Store your API key securely in an environment variable -const API_KEY = process.env.API_KEY || 'API_KEY not defined'; - // Route for the root URL '/' app.get('/', (req, res) => { res.send('Hi from api.peterwalker.xyz'); @@ -37,12 +42,21 @@ app.get('/', (req, res) => { // Route for '/api/key' app.get('/api/key', (req, res) => { + console.log('API key requested'); res.json({ apiKey: API_KEY }); }); app.use('/api/books', booksRouter); -if (isProduction) { +function createHTTPServer() { + const http = require('http'); + + http.createServer(app).listen(PORT, () => { + console.log(`HTTP Server is running on port ${PORT}`); + }); +} + +function createHTTPSServer() { const https = require('https'); const fs = require('fs'); @@ -50,16 +64,12 @@ if (isProduction) { key: fs.readFileSync( '/etc/letsencrypt/live/api.peterwalker.xyz/privkey.pem', ), - cert: fs.readFileSync('/etc/letsencrypt/live/api.peterwalker.xyz/fullchain.pem'), + cert: fs.readFileSync( + '/etc/letsencrypt/live/api.peterwalker.xyz/fullchain.pem', + ), }; https.createServer(options, app).listen(PORT, () => { console.log(`HTTPS Server is running on port ${PORT}`); }); -} else { - const http = require('http'); - - http.createServer(app).listen(PORT, () => { - console.log(`HTTP Server is running on port ${PORT}`); - }); }