Skip to content

Commit

Permalink
Small Changes
Browse files Browse the repository at this point in the history
Updated index.js to latest
Updated gitignore to lates
Included a new ndoe in readme
  • Loading branch information
galaxine-senpai committed Jun 11, 2024
1 parent b8a8835 commit ed9a412
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ private/*
*.log
*.env
robots.txt
sitemap.xml
sitemap.*
*.bak
*encrypt*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Now you should be able to access the website on `http://localhost`!
If you wish to setup an .env file for the website, you can do so by creating a file called `.env` in the root directory of the repository, and adding the following:

```env
PORT = 80 # The port to run the website on
PORT = 80 # The port to run the website on // Has ability to utilize https granted both key and privatekey are there
STATUS = DEVELOPMENT # The status of the website, can be either DEVELOPMENT or PRODUCTION will be used for things later on
```

Expand Down
37 changes: 29 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const express = require("express");
const dotenv = require("dotenv");
const moment = require("moment");
const https = require("https");
const fs = require('fs')

// Load the .env file
dotenv.config();
Expand All @@ -19,6 +21,15 @@ const {
reset,
} = require("./utils/color.js");

function forceHTTPS(req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https' && req.headers['x-forwarded-proto'] !== undefined) {
return res.redirect(301, 'https://' + req.headers.host + req.url);
}
return next();
}

app.use(forceHTTPS);

// log all morgan logs to a file
const { open, createWriteStream } = require("fs");

Expand Down Expand Up @@ -138,11 +149,10 @@ app.get("/status", function (req, res) {
// send user to status.microwavebot.com
res.redirect(301, "https://status.microwavebot.com");
});
/* // DISABLED DUE TO POSSIBLE VULNERABILITIES

app.get("/robots.txt", function (req, res) {
res.sendFile("./robots.txt"); // Just for web crawlers // weird bug: Error: ENOENT: no such file or directory, stat '/home/xxxxx/microwavebot-webrobots.txt'
res.sendFile(__dirname + "/robots.txt"); // Correctly does the Robotx.txt
});
*/
// Will move errors to a router later on
app.use(function (req, res, next) {
res.status(404).sendFile(__dirname + "/views/errors/404.html");
Expand All @@ -161,8 +171,19 @@ if (os.platform() === "linux") {
console.log("This feature is only available on Linux.");
}

// start the server
app.listen(port, () => {
console.log(`${green}Server started on port ${yellow}${port}${reset}`);
// TODO: add check if API is online and reachable
});
if (fs.existsSync('./certs/privkey.pem') && fs.existsSync('./certs/fullchain.pem')) {
console.log(`${green}SSL Certificates found, starting HTTPS server${reset}`);
const options = {
key: fs.readFileSync('./certs/privkey.pem', 'utf8'),
cert: fs.readFileSync('./certs/fullchain.pem', 'utf8')
};

https.createServer(options, app).listen(port, () => {
console.log(`${green}Listening on port ${port}${reset}`);
});
}

// Now on 80
app.listen(80, () => {
console.log(`${green}Listening on port 80${reset}`);
});

0 comments on commit ed9a412

Please sign in to comment.