mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Auto generating folders, more compatibility running on any machine
This commit is contained in:
parent
c10a5a8a6c
commit
7edce016b5
5 changed files with 90 additions and 71 deletions
111
server.js
111
server.js
|
@ -18,21 +18,20 @@
|
|||
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var busboy = require('connect-busboy');
|
||||
var fs = require('fs');
|
||||
var app = express();
|
||||
var http = require('http');
|
||||
var https = require('https');
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const busboy = require('connect-busboy');
|
||||
const fs = require('fs');
|
||||
const app = express();
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
|
||||
var logger = require('./logger.js');
|
||||
var utils = require('./utils.js');
|
||||
var actions = require('./actions.js');
|
||||
var stat = require('./stat.js');
|
||||
|
||||
var bodyParser = require('body-parser');
|
||||
const logger = require('./logger.js');
|
||||
const utils = require('./utils.js');
|
||||
const actions = require('./actions.js');
|
||||
const stat = require('./stat.js');
|
||||
|
||||
const siteUrl = "http://localhost:8080"; // http(s)//asd.basd
|
||||
const recivedFiles = "public/recivedfiles";
|
||||
const motdFile = "public/motd";
|
||||
const staticFile = "public/data/static";
|
||||
|
@ -45,10 +44,23 @@ const msgFile = "stats/msgs";
|
|||
const logFile = "stats/logs";
|
||||
|
||||
// https://certbot.eff.org/
|
||||
const key = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/privkey.pem", "utf8");
|
||||
const cert = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/fullchain.pem", "utf8");
|
||||
const ca = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/chain.pem", "utf8");
|
||||
var certs = {key:key, cert:cert, ca:ca};
|
||||
const privkeyFile = "/etc/letsencrypt/live/questionmining.tk/privkey.pem";
|
||||
const fullchainFile = "/etc/letsencrypt/live/questionmining.tk/fullchain.pem";
|
||||
const chainFile = "/etc/letsencrypt/live/questionmining.tk/chain.pem";
|
||||
|
||||
var certsLoaded = false;
|
||||
if (utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExistsc(chainFile)) {
|
||||
const key = fs.readFileSync(privkeyFile, "utf8");
|
||||
const cert = fs.readFileSync(fullchainFile, "utf8");
|
||||
const ca = fs.readFileSync(chainFile, "utf8");
|
||||
var certs = {
|
||||
key: key,
|
||||
cert: cert,
|
||||
ca: ca
|
||||
};
|
||||
certsLoaded = true;
|
||||
}
|
||||
|
||||
const port = 8080;
|
||||
const httpsPort = 8443;
|
||||
|
||||
|
@ -56,8 +68,6 @@ var highlights = ["public", "static", "manual", "isgetting", "postfeedback",
|
|||
"postquestions"
|
||||
];
|
||||
|
||||
var newMessages = "";
|
||||
|
||||
app.set('view engine', 'ejs');
|
||||
//app.all('*', function(req, res, next) {
|
||||
// if(req.secure) {
|
||||
|
@ -91,7 +101,7 @@ app.use(bodyParser.json({
|
|||
|
||||
app.get('/', function(req, res) {
|
||||
res.render('main', {
|
||||
sdata: utils.ReadFile(staticFile)
|
||||
siteurl: siteUrl
|
||||
});
|
||||
res.end();
|
||||
});
|
||||
|
@ -131,7 +141,6 @@ app.get('/legacy', function(req, res) {
|
|||
|
||||
app.post('/postfeedback', function(req, res) {
|
||||
res.redirect('back');
|
||||
newMessages += "\n" + req.body.message_field;
|
||||
logger.Log("[UMSG]: " + req.body.message_field, logger.GetColor("bluebg"), true);
|
||||
utils.AppendToFile(logger.GetDateString() + ": " + req.body.message_field, msgFile);
|
||||
});
|
||||
|
@ -158,29 +167,6 @@ app.get('/menuClick', function(req, res) {
|
|||
res.end();
|
||||
});
|
||||
|
||||
app.route('/badtestsender').post(function(req, res, next) {
|
||||
var fstream;
|
||||
req.pipe(req.busboy);
|
||||
req.busboy.on('file', function(fieldname, file, filename) {
|
||||
logger.Log("[UPL]: Uploading: " + filename, logger.GetColor("blue"));
|
||||
|
||||
fstream = fs.createWriteStream(recivedFiles + "/" + filename + " | " + Date().toString());
|
||||
file.pipe(fstream);
|
||||
fstream.on('close', function() {
|
||||
logger.Log("[UPL]: Upload Finished of " + filename, logger.GetColor("blue"));
|
||||
res.render("uploaded");
|
||||
});
|
||||
fstream.on('error', function(err) {
|
||||
console.log("ERROR:" + err);
|
||||
res.end("file uploaded");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/sanityCheck', function(req, res) {
|
||||
res.end('Uploaded :) go back now');
|
||||
});
|
||||
|
||||
app.get('/scriptgit', function(req, res) {
|
||||
res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript");
|
||||
res.end();
|
||||
|
@ -202,12 +188,41 @@ app.post('*', function(req, res) {
|
|||
// utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile);
|
||||
});
|
||||
|
||||
app.route('/badtestsender').post(function(req, res, next) {
|
||||
var fstream;
|
||||
req.pipe(req.busboy);
|
||||
req.busboy.on('file', function(fieldname, file, filename) {
|
||||
logger.Log("[UPL]: Uploading: " + filename, logger.GetColor("blue"));
|
||||
|
||||
utils.CreatePath(recivedFiles, true);
|
||||
fstream = fs.createWriteStream(recivedFiles + "/" + filename + " | " + Date().toString());
|
||||
file.pipe(fstream);
|
||||
fstream.on('close', function() {
|
||||
logger.Log("[UPL]: Upload Finished of " + filename, logger.GetColor("blue"));
|
||||
res.render("uploaded");
|
||||
});
|
||||
fstream.on('error', function(err) {
|
||||
console.log("ERROR:" + err);
|
||||
res.end("file uploaded");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
var msg = "[STRT]: ";
|
||||
stat.Load();
|
||||
|
||||
const httpServer = http.createServer(app);
|
||||
const httpsServer = https.createServer(certs, app);
|
||||
httpServer.listen(port);
|
||||
httpsServer.listen(httpsPort);
|
||||
logger.Log("[STRT]: Server listening on port " + port + " (http), and " + httpsPort + " (https)...", logger.GetColor("yellow"));
|
||||
msg += "Server listening on port " + port + " (http)";
|
||||
|
||||
if (certsLoaded) {
|
||||
const httpsServer = https.createServer(certs, app);
|
||||
httpsServer.listen(httpsPort);
|
||||
msg += ", and " + httpsPort + " (https)...";
|
||||
} else {
|
||||
logger.Log("Cert files does not exists, starting http only!" ,logger.GetColor("redbg"));
|
||||
}
|
||||
logger.Log(msg ,logger.GetColor("yellow"));
|
||||
|
||||
function Log(req, toFile, sc) {
|
||||
try {
|
||||
|
@ -230,7 +245,7 @@ function Log(req, toFile, sc) {
|
|||
} else {
|
||||
var defLogs = logger.GetDateString() + ": " + logEntry;
|
||||
var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req
|
||||
.body) + "\n";
|
||||
.body) + "\n";
|
||||
|
||||
utils.AppendToFile(defLogs, logFile);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue