Skip to content

Commit

Permalink
make working locally easier
Browse files Browse the repository at this point in the history
  • Loading branch information
petertimwalker committed May 27, 2024
1 parent 2f338ff commit f2b2527
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
const https = require('https');
const fs = require('fs');
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 443;
const dotenv = require('dotenv');
dotenv.config();

// Use CORS middleware to allow requests from all origins
app.use(
cors({
origin: 'https://peterwalker.xyz',
}),
);
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';
Expand All @@ -33,14 +25,32 @@ app.get('/api/key', (req, res) => {
res.json({ apiKey: API_KEY });
});

// HTTPS options
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/api.peterwalker.xyz/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/api.peterwalker.xyz/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/api.peterwalker.xyz/chain.pem'),
};
if (isProduction) {
const https = require('https');
const fs = require('fs');
const cors = require('cors');

// Start the HTTPS server
https.createServer(options, app).listen(PORT, () => {
console.log(`HTTPS Server is running on port ${PORT}`);
});
app.use(
cors({
origin: 'https://peterwalker.xyz',
}),
);

const options = {
key: fs.readFileSync(
'/etc/letsencrypt/live/api.peterwalker.xyz/privkey.pem',
),
cert: fs.readFileSync('/etc/letsencrypt/live/api.peterwalker.xyz/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/api.peterwalker.xyz/chain.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}`);
});
}

0 comments on commit f2b2527

Please sign in to comment.