Skip to content

Commit

Permalink
Big update
Browse files Browse the repository at this point in the history
  • Loading branch information
galaxine-senpai committed Jan 25, 2024
1 parent 6b6658b commit de35798
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
node_modules/*
certs/*
private/*
*.pem
*.key
private/*
*.log
*.env
50 changes: 46 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
const express = require("express");

const app = express();
const morgan = require("morgan");
const { red, blue, green, yellow, cyan, magenta, white, black, reset } = require("./utils/color.js");

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


// create the file access.log in /logs/
open(__dirname + '/logs/access.log', 'w', (err, file) => {
if (err) {
console.log(err);
}
});
// create a write stream (in append mode)
const accessLogStream = createWriteStream(__dirname + '/logs/access.log', { flags: 'a' });
// Yellow is info, green is religated to status codes
app.use(morgan(':date[web] | :method :url - :status - :response-time ms | :req[cf-ipcountry]', { stream: accessLogStream }));

// Paths to define for later use idfk
const css = express.static('/src/css');
Expand All @@ -9,8 +26,7 @@ const img = express.static('/src/img');

// Express settings
const port =
/* Generate random port but if and ENV exists set it to that */ process.env
.PORT || 80;
process.env.PORT || 80;

// serve all files in the css, js, and img directories
app.use('/css', express.static(__dirname + '/src/css'));
Expand All @@ -26,10 +42,36 @@ app.get('/mrs-gaines', function(req, res) {
res.sendFile(__dirname + '/views/mrs-gaines.html');
})

app.get('/status', function(req, res) {
// send user to status.microwavebot.com
res.redirect(301, 'https://status.microwavebot.com');
})


// Will move errors to a router later on
app.use(function(req, res, next) {
res.status(404).sendFile(__dirname + '/views/errors/404.html');
});

app.use(function(req, res, next) {
res.status(403).sendFile(__dirname + '/views/errors/403.html');
})

// Linux only, this also presumes you have sudo perms
const os = require('os');
const { exec } = require('child_process');
if (os.platform() === 'linux') {
exec(`fuser -k ${port}/tcp`, (err, stdout, stderr) => {
if (err) {
console.log(err);
}
})
} else {
console.log('This feature is only available on Linux.');
}


// start the server
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
})
console.log(`Server started on port ${port}`);
});
Loading

0 comments on commit de35798

Please sign in to comment.