mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
216 lines
5.3 KiB
JavaScript
Executable file
216 lines
5.3 KiB
JavaScript
Executable file
/* ----------------------------------------------------------------------------
|
|
|
|
Question Server
|
|
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
let url = ''
|
|
|
|
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')
|
|
|
|
const logger = require('../utils/logger.js')
|
|
const utils = require('../utils/utils.js')
|
|
const actions = require('../utils/actions.js')
|
|
|
|
const recivedFiles = 'public/recivedfiles'
|
|
const uloadFiles = 'public/f'
|
|
const dataFile = 'public/data.json'
|
|
const msgFile = 'stats/msgs'
|
|
|
|
app.set('view engine', 'ejs')
|
|
app.use(express.static('public'))
|
|
app.use(busboy({
|
|
limits: {
|
|
fileSize: 10000 * 1024 * 1024
|
|
}
|
|
}))
|
|
app.use(bodyParser.json())
|
|
app.use(bodyParser.urlencoded({
|
|
limit: '5mb',
|
|
extended: true
|
|
}))
|
|
app.use(bodyParser.json({
|
|
limit: '5mb'
|
|
}))
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
app.get('/', function (req, res) {
|
|
// req.hostname
|
|
|
|
res.render('qmining/main', {
|
|
siteurl: url,
|
|
qa: actions.ProcessQA()
|
|
})
|
|
res.end()
|
|
})
|
|
|
|
app.get('/manual', function (req, res) {
|
|
res.render('qmining/man')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/legacy', function (req, res) {
|
|
var f = utils.ReadFile(dataFile)
|
|
var d = actions.LoadJSON(f)
|
|
let qcount = 0
|
|
for (let i = 0; i < d.length; i++) { qcount += d.Subjects[i].length }
|
|
let scount = d.length
|
|
|
|
res.render('qmining/alldata', {
|
|
data: d,
|
|
scount: scount,
|
|
qcount: qcount,
|
|
siteurl: url
|
|
})
|
|
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.post('/postfeedback', function (req, res) {
|
|
res.redirect('back')
|
|
logger.Log('New feedback message', logger.GetColor('bluebg'), true)
|
|
utils.AppendToFile('\n\n' + logger.GetDateString() + ': ' + req.body.message_field, msgFile)
|
|
})
|
|
|
|
app.get('/postfeedback', function (req, res) {
|
|
res.redirect('/')
|
|
})
|
|
|
|
app.post('/isAdding', function (req, res) {
|
|
res.end('OK')
|
|
logger.LogReq(req)
|
|
actions.ProcessIncomingRequest(req.body.datatoadd)
|
|
utils.WriteBackup()
|
|
})
|
|
|
|
app.get('/lred', function (req, res) {
|
|
res.redirect('/legacy')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/menuClick', function (req, res) {
|
|
res.redirect('/')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
// all questions readable
|
|
app.get('/allqr', function (req, res) {
|
|
var f = utils.ReadFile(dataFile)
|
|
var d = actions.LoadJSON(f)
|
|
|
|
res.render('qmining/allqr', {
|
|
d: d.toString().split('\n')
|
|
})
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/greasy', function (req, res) {
|
|
res.redirect('https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/install', function (req, res) {
|
|
res.redirect(url + '/moodle-test-userscript/stable.user.js?install')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/install', function (req, res) {
|
|
res.redirect(url + '/moodle-test-userscript/stable.user.js')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/classesgit', function (req, res) {
|
|
res.redirect('https://gitlab.com/MrFry/question-classes')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/scriptgit', function (req, res) {
|
|
res.redirect('https://gitlab.com/MrFry/moodle-test-userscript')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('/servergit', function (req, res) {
|
|
res.redirect('https://gitlab.com/MrFry/mrfrys-node-server')
|
|
res.end()
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
function UploadFile (req, res, path, next) {
|
|
var fstream
|
|
req.pipe(req.busboy)
|
|
req.busboy.on('file', function (fieldname, file, filename) {
|
|
logger.Log('Uploading: ' + filename, logger.GetColor('blue'))
|
|
|
|
utils.CreatePath(path, true)
|
|
let d = new Date()
|
|
let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename
|
|
|
|
fstream = fs.createWriteStream(path + '/' + fn)
|
|
file.pipe(fstream)
|
|
fstream.on('close', function () {
|
|
logger.Log('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('qmining/uploaded')
|
|
})
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('*', function (req, res) {
|
|
res.status(404).render('shared/404')
|
|
})
|
|
|
|
app.post('*', function (req, res) {
|
|
res.status(404).render('shared/404')
|
|
})
|
|
|
|
exports.app = app
|
|
exports.setup = (x) => {
|
|
url = x.url
|
|
}
|
|
|
|
logger.Log('Qmining module started', logger.GetColor('yellow'))
|