mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Lots of bugfixes, improvements: Dinamyc QA, file uploading refactoring, file managing logging improvements ...
This commit is contained in:
parent
3250139c82
commit
b8c1a6ca97
14 changed files with 662 additions and 533 deletions
84
server.js
84
server.js
|
@ -18,6 +18,9 @@
|
|||
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
const startHTTPS = false;
|
||||
const siteUrl = "https://qmining.tk"; // http(s)//asd.basd
|
||||
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const busboy = require('connect-busboy');
|
||||
|
@ -31,8 +34,8 @@ const utils = require('./utils.js');
|
|||
const actions = require('./actions.js');
|
||||
const stat = require('./stat.js');
|
||||
|
||||
const siteUrl = "https://questionmining.tk"; // http(s)//asd.basd
|
||||
const recivedFiles = "public/recivedfiles";
|
||||
const uloadFiles = "public/f";
|
||||
const motdFile = "public/motd";
|
||||
const staticFile = "public/data/static";
|
||||
const dataFile = "public/data.json";
|
||||
|
@ -49,7 +52,7 @@ 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.FileExists(chainFile)) {
|
||||
if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists(chainFile)) {
|
||||
const key = fs.readFileSync(privkeyFile, "utf8");
|
||||
const cert = fs.readFileSync(fullchainFile, "utf8");
|
||||
const ca = fs.readFileSync(chainFile, "utf8");
|
||||
|
@ -101,11 +104,17 @@ app.use(bodyParser.json({
|
|||
|
||||
app.get('/', function(req, res) {
|
||||
res.render('main', {
|
||||
siteurl: siteUrl
|
||||
siteurl: siteUrl,
|
||||
qa: actions.ProcessQA()
|
||||
});
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.get('/sio', function(req, res) {
|
||||
res.render('uload');
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.get('/manual', function(req, res) {
|
||||
res.write(utils.ReadFile(manFile));
|
||||
res.end();
|
||||
|
@ -141,11 +150,13 @@ app.get('/legacy', function(req, res) {
|
|||
|
||||
app.post('/postfeedback', function(req, res) {
|
||||
res.redirect('back');
|
||||
logger.Log("[UMSG]: " + req.body.message_field, logger.GetColor("bluebg"), true);
|
||||
utils.AppendToFile(logger.GetDateString() + ": " + req.body.message_field, msgFile);
|
||||
logger.Log("[UMSG]: New feedback message", logger.GetColor("bluebg"), true);
|
||||
utils.AppendToFile("\n\n" + logger.GetDateString() + ": " + req.body.message_field, msgFile);
|
||||
});
|
||||
|
||||
app.get('/postfeedback', function(req, res) {
|
||||
// TODO: res.redirect("/"); or if needs this anyways, becouse /postfeedback post handler already
|
||||
// redirects
|
||||
res.render('main', {
|
||||
sdata: utils.ReadFile(staticFile)
|
||||
});
|
||||
|
@ -158,10 +169,6 @@ app.post('/isAdding', function(req, res) {
|
|||
utils.WriteBackup();
|
||||
});
|
||||
|
||||
app.get('/sanityCheck', function(req, res) {
|
||||
res.end('Hello guys');
|
||||
});
|
||||
|
||||
app.get('/menuClick', function(req, res) {
|
||||
res.redirect("/");
|
||||
res.end();
|
||||
|
@ -177,6 +184,43 @@ app.get('/servergit', function(req, res) {
|
|||
res.end();
|
||||
});
|
||||
|
||||
function UploadFile(req, res, path, 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(path, true);
|
||||
let d = new Date();
|
||||
let fsplit = filename.split('.');
|
||||
let fn = d.getHours() + "" + d.getMinutes() + "" + d.getSeconds() + "." + fsplit[fsplit.length
|
||||
- 1];
|
||||
|
||||
fstream = fs.createWriteStream(path + "/" + fn);
|
||||
file.pipe(fstream);
|
||||
fstream.on('close', function() {
|
||||
logger.Log("[UPL]: Upload Finished of " + path + "/" + fn, logger.GetColor("blue"));
|
||||
next(fn);
|
||||
});
|
||||
fstream.on('error', function(err) {
|
||||
console.log(err);
|
||||
res.end("something bad happened :s");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
app.route('/fosuploader').post(function(req, res, next) {
|
||||
UploadFile(req, res, uloadFiles, (fn) => {
|
||||
res.redirect("/f/" + fn);
|
||||
});
|
||||
});
|
||||
|
||||
app.route('/badtestsender').post(function(req, res, next) {
|
||||
UploadFile(req, res, recivedFiles, (fn) => {
|
||||
res.render("uploaded");
|
||||
});
|
||||
});
|
||||
|
||||
app.get('*', function(req, res) {
|
||||
res.render('404');
|
||||
res.status(404);
|
||||
|
@ -188,26 +232,6 @@ 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();
|
||||
|
||||
|
@ -215,7 +239,7 @@ const httpServer = http.createServer(app);
|
|||
httpServer.listen(port);
|
||||
msg += "Server listening on port " + port + " (http)";
|
||||
|
||||
if (certsLoaded) {
|
||||
if (startHTTPS && certsLoaded) {
|
||||
const httpsServer = https.createServer(certs, app);
|
||||
httpsServer.listen(httpsPort);
|
||||
msg += ", and " + httpsPort + " (https)...";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue