Qmining redirect routes, handling all pages, removed unecesary api code

This commit is contained in:
MrFry 2020-03-16 09:31:43 +01:00
parent cbdece54f8
commit 2caf368b95
5 changed files with 90 additions and 72 deletions

View file

@ -18,14 +18,23 @@
------------------------------------------------------------------------- */
let url = ''
const express = require('express')
const bodyParser = require('body-parser')
const busboy = require('connect-busboy')
const app = express()
// const utils = require('../../utils/utils.js')
const utils = require('../../utils/utils.js')
const logger = require('../../utils/logger.js')
let donateURL = ''
try {
donateURL = utils.ReadFile('./data/donateURL')
} catch (e) {
logger.Log('Couldnt read donate URL file!', logger.GetColor('red'))
}
app.set('view engine', 'ejs')
app.set('views', [
'./modules/qmining/views',
@ -47,15 +56,79 @@ app.use(bodyParser.json({
}))
// --------------------------------------------------------------
// REDIRECTS
// --------------------------------------------------------------
// to be backwards compatible
app.get('/ask', function (req, res) {
logger.DebugLog(`Qmining module ask redirect`, 'ask', 1)
res.redirect(`http://api.frylabs.net/ask?q=${req.query.q}&subj=${req.query.subj}&data=${req.query.data}`)
})
app.get('/infos', function (req, res) {
logger.DebugLog(`Qmining module infos redirect`, 'infos', 1)
res.redirect('http://api.frylabs.net/infos?version=true&motd=true&subjinfo=true')
const simpleRedirects = [
{
from: '/install',
to: url + '/moodle-test-userscript/stable.user.js?install'
},
{
from: '/scriptgit',
to: 'https://gitlab.com/MrFry/moodle-test-userscript'
},
{
from: '/classesgit',
to: 'https://gitlab.com/MrFry/question-classes'
},
{
from: '/menuClick',
to: '/'
},
{
from: '/lred',
to: '/allQuestions.html'
},
{
from: '/donate',
to: donateURL
},
{ // to be backwards compatible
from: '/legacy',
to: '/allQuestions.html'
},
{
from: '/infos',
to: 'http://api.frylabs.net/infos?version=true&motd=true&subjinfo=true'
}
]
simpleRedirects.forEach((redirect) => {
app.get(redirect.from, function (req, res) {
logger.LogReq(req)
logger.DebugLog(`Qmining module ${redirect.from} redirect`, 'infos', 1)
res.redirect(`${redirect.to}`)
})
})
// --------------------------------------------------------------
function AddHtmlRoutes (files) {
const routes = files.reduce((acc, f) => {
if (f.includes('html')) {
acc.push(f.split('.')[0])
return acc
}
return acc
}, [])
routes.forEach((route) => {
logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1)
app.get(`/${route}`, function (req, res) {
logger.LogReq(req)
res.redirect(`${route}.html`)
})
})
}
AddHtmlRoutes(utils.ReadDir('modules/qmining/public'))
// --------------------------------------------------------------
app.get('/', function (req, res) {
@ -64,9 +137,7 @@ app.get('/', function (req, res) {
})
app.get('*', function (req, res) {
res.redirect('/')
res.end()
// res.status(404).render('404')
res.status(404).render('404')
})
app.post('*', function (req, res) {
@ -74,5 +145,8 @@ app.post('*', function (req, res) {
})
exports.app = app
exports.setup = (x) => {
url = x.url
}
logger.Log('Qmining module started', logger.GetColor('yellow'))