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
11
README.md
11
README.md
|
@ -1,8 +1,11 @@
|
||||||
Question server
|
# Question server
|
||||||
|
|
||||||
Install:
|
Install:
|
||||||
npm install express connect-busboy ejs express-layout querystring express
|
|
||||||
|
|
||||||
some stuff maybe missing
|
Requires node.js.
|
||||||
|
|
||||||
"Client:" https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript
|
`npm install express connect-busboy ejs express-layout querystring express`
|
||||||
|
|
||||||
|
Should auto generate needed folder structure
|
||||||
|
|
||||||
|
<a href="https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript">Client</a>
|
||||||
|
|
111
server.js
111
server.js
|
@ -18,21 +18,20 @@
|
||||||
|
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
var express = require('express');
|
const express = require('express');
|
||||||
var bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
var busboy = require('connect-busboy');
|
const busboy = require('connect-busboy');
|
||||||
var fs = require('fs');
|
const fs = require('fs');
|
||||||
var app = express();
|
const app = express();
|
||||||
var http = require('http');
|
const http = require('http');
|
||||||
var https = require('https');
|
const https = require('https');
|
||||||
|
|
||||||
var logger = require('./logger.js');
|
const logger = require('./logger.js');
|
||||||
var utils = require('./utils.js');
|
const utils = require('./utils.js');
|
||||||
var actions = require('./actions.js');
|
const actions = require('./actions.js');
|
||||||
var stat = require('./stat.js');
|
const stat = require('./stat.js');
|
||||||
|
|
||||||
var bodyParser = require('body-parser');
|
|
||||||
|
|
||||||
|
const siteUrl = "http://localhost:8080"; // http(s)//asd.basd
|
||||||
const recivedFiles = "public/recivedfiles";
|
const recivedFiles = "public/recivedfiles";
|
||||||
const motdFile = "public/motd";
|
const motdFile = "public/motd";
|
||||||
const staticFile = "public/data/static";
|
const staticFile = "public/data/static";
|
||||||
|
@ -45,10 +44,23 @@ const msgFile = "stats/msgs";
|
||||||
const logFile = "stats/logs";
|
const logFile = "stats/logs";
|
||||||
|
|
||||||
// https://certbot.eff.org/
|
// https://certbot.eff.org/
|
||||||
const key = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/privkey.pem", "utf8");
|
const privkeyFile = "/etc/letsencrypt/live/questionmining.tk/privkey.pem";
|
||||||
const cert = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/fullchain.pem", "utf8");
|
const fullchainFile = "/etc/letsencrypt/live/questionmining.tk/fullchain.pem";
|
||||||
const ca = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/chain.pem", "utf8");
|
const chainFile = "/etc/letsencrypt/live/questionmining.tk/chain.pem";
|
||||||
var certs = {key:key, cert:cert, ca:ca};
|
|
||||||
|
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 port = 8080;
|
||||||
const httpsPort = 8443;
|
const httpsPort = 8443;
|
||||||
|
|
||||||
|
@ -56,8 +68,6 @@ var highlights = ["public", "static", "manual", "isgetting", "postfeedback",
|
||||||
"postquestions"
|
"postquestions"
|
||||||
];
|
];
|
||||||
|
|
||||||
var newMessages = "";
|
|
||||||
|
|
||||||
app.set('view engine', 'ejs');
|
app.set('view engine', 'ejs');
|
||||||
//app.all('*', function(req, res, next) {
|
//app.all('*', function(req, res, next) {
|
||||||
// if(req.secure) {
|
// if(req.secure) {
|
||||||
|
@ -91,7 +101,7 @@ app.use(bodyParser.json({
|
||||||
|
|
||||||
app.get('/', function(req, res) {
|
app.get('/', function(req, res) {
|
||||||
res.render('main', {
|
res.render('main', {
|
||||||
sdata: utils.ReadFile(staticFile)
|
siteurl: siteUrl
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
@ -131,7 +141,6 @@ app.get('/legacy', function(req, res) {
|
||||||
|
|
||||||
app.post('/postfeedback', function(req, res) {
|
app.post('/postfeedback', function(req, res) {
|
||||||
res.redirect('back');
|
res.redirect('back');
|
||||||
newMessages += "\n" + req.body.message_field;
|
|
||||||
logger.Log("[UMSG]: " + req.body.message_field, logger.GetColor("bluebg"), true);
|
logger.Log("[UMSG]: " + req.body.message_field, logger.GetColor("bluebg"), true);
|
||||||
utils.AppendToFile(logger.GetDateString() + ": " + req.body.message_field, msgFile);
|
utils.AppendToFile(logger.GetDateString() + ": " + req.body.message_field, msgFile);
|
||||||
});
|
});
|
||||||
|
@ -158,29 +167,6 @@ app.get('/menuClick', function(req, res) {
|
||||||
res.end();
|
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) {
|
app.get('/scriptgit', function(req, res) {
|
||||||
res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript");
|
res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript");
|
||||||
res.end();
|
res.end();
|
||||||
|
@ -202,12 +188,41 @@ app.post('*', function(req, res) {
|
||||||
// utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile);
|
// 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();
|
stat.Load();
|
||||||
|
|
||||||
const httpServer = http.createServer(app);
|
const httpServer = http.createServer(app);
|
||||||
const httpsServer = https.createServer(certs, app);
|
|
||||||
httpServer.listen(port);
|
httpServer.listen(port);
|
||||||
httpsServer.listen(httpsPort);
|
msg += "Server listening on port " + port + " (http)";
|
||||||
logger.Log("[STRT]: Server listening on port " + port + " (http), and " + httpsPort + " (https)...", logger.GetColor("yellow"));
|
|
||||||
|
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) {
|
function Log(req, toFile, sc) {
|
||||||
try {
|
try {
|
||||||
|
@ -230,7 +245,7 @@ function Log(req, toFile, sc) {
|
||||||
} else {
|
} else {
|
||||||
var defLogs = logger.GetDateString() + ": " + logEntry;
|
var defLogs = logger.GetDateString() + ": " + logEntry;
|
||||||
var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req
|
var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req
|
||||||
.body) + "\n";
|
.body) + "\n";
|
||||||
|
|
||||||
utils.AppendToFile(defLogs, logFile);
|
utils.AppendToFile(defLogs, logFile);
|
||||||
}
|
}
|
||||||
|
|
8
todos
8
todos
|
@ -1,8 +0,0 @@
|
||||||
make init function that
|
|
||||||
creates directory and file structure
|
|
||||||
redirects, for more stats (ex. to greasyfork: /script
|
|
||||||
like - dislike button
|
|
||||||
static public links above textboxes
|
|
||||||
public questions allow question deletion
|
|
||||||
remove public questions
|
|
||||||
remove greasyfork images from manual
|
|
21
utils.js
21
utils.js
|
@ -4,7 +4,9 @@ module.exports = {
|
||||||
writeFileAsync: WriteFileAsync,
|
writeFileAsync: WriteFileAsync,
|
||||||
AppendToFile: AppendToFile,
|
AppendToFile: AppendToFile,
|
||||||
Beep: Beep,
|
Beep: Beep,
|
||||||
WriteBackup: WriteBackup
|
WriteBackup: WriteBackup,
|
||||||
|
FileExists: FileExists,
|
||||||
|
CreatePath: CreatePath
|
||||||
};
|
};
|
||||||
|
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
@ -18,25 +20,32 @@ const manFile = "public/man.html";
|
||||||
const logFile = "stats/logs";
|
const logFile = "stats/logs";
|
||||||
|
|
||||||
function ReadFile(name) {
|
function ReadFile(name) {
|
||||||
if (!fs.existsSync(name))
|
if (!FileExists(name))
|
||||||
throw "No such file: " + name;
|
throw "No such file: " + name;
|
||||||
return fs.readFileSync(name, "utf8");
|
return fs.readFileSync(name, "utf8");
|
||||||
}
|
}
|
||||||
|
|
||||||
function CreatePath(path) {
|
function FileExists(path) {
|
||||||
if (fs.existsSync(path))
|
return fs.existsSync(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
function CreatePath(path, onlyPath) {
|
||||||
|
if (FileExists(path))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var p = path.split("/");
|
var p = path.split("/");
|
||||||
var currDir = p[0];
|
var currDir = p[0];
|
||||||
for (var i = 1; i < p.length; i++) {
|
for (var i = 1; i < p.length; i++) {
|
||||||
console.log(currDir);
|
|
||||||
if (!fs.existsSync(currDir)) {
|
if (!fs.existsSync(currDir)) {
|
||||||
fs.mkdirSync(currDir);
|
fs.mkdirSync(currDir);
|
||||||
}
|
}
|
||||||
currDir += "/" + p[i];
|
currDir += "/" + p[i];
|
||||||
}
|
}
|
||||||
fs.writeFileSync(path, "");
|
if (onlyPath == undefined || onlyPath == false)
|
||||||
|
fs.writeFileSync(path, "");
|
||||||
|
else
|
||||||
|
fs.mkdirSync(path);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function WriteFile(content, path) {
|
function WriteFile(content, path) {
|
||||||
|
|
|
@ -20,15 +20,15 @@ a {color: #9999ff;}
|
||||||
<a href="https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help">Script
|
<a href="https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help">Script
|
||||||
greasyforkon</a>
|
greasyforkon</a>
|
||||||
|
|
|
|
||||||
<a href="http://questionmining.tk/manual">Manual</a>
|
<a href="<%= siteurl %>/manual">Manual</a>
|
||||||
|
|
|
|
||||||
<a href="http://questionmining.tk/servergit">Szerver repó</a>
|
<a href="<%= siteurl %>/servergit">Szerver repó</a>
|
||||||
|
|
|
|
||||||
<a href="http://questionmining.tk/scriptgit">Userscript repó</a>
|
<a href="<%= siteurl %>/scriptgit">Userscript repó</a>
|
||||||
|
|
|
|
||||||
<a href="http://questionmining.tk/data.json">Összes kérdés (JSON)</a>
|
<a href="<%= siteurl %>/data.json">Összes kérdés (JSON)</a>
|
||||||
|
|
|
|
||||||
<a href="http://questionmining.tk/legacy">Összes kérdés (Régi formátum)</a>
|
<a href="<%= siteurl %>/legacy">Összes kérdés (Régi formátum)</a>
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<table style="table-layout:fixed;width:100%">
|
<table style="table-layout:fixed;width:100%">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue