From 2caf368b95a41e30d017546045ada3f11c1f6ffd Mon Sep 17 00:00:00 2001
From: MrFry <mrfry@airmail.cc>
Date: Mon, 16 Mar 2020 09:31:43 +0100
Subject: [PATCH] Qmining redirect routes, handling all pages, removed
 unecesary api code

---
 modules/api/api.js         | 63 +--------------------------
 modules/qmining/qmining.js | 88 +++++++++++++++++++++++++++++++++++---
 server.js                  |  2 +-
 sharedViews/404.ejs        |  2 +-
 utils/utils.js             |  7 ++-
 5 files changed, 90 insertions(+), 72 deletions(-)

diff --git a/modules/api/api.js b/modules/api/api.js
index 4cd756b..b6165da 100644
--- a/modules/api/api.js
+++ b/modules/api/api.js
@@ -18,8 +18,6 @@
 
  ------------------------------------------------------------------------- */
 
-let url = ''
-
 const express = require('express')
 const bodyParser = require('body-parser')
 const busboy = require('connect-busboy')
@@ -38,12 +36,6 @@ const dataFile = 'public/data.json'
 const msgFile = 'stats/msgs'
 const motdFile = 'public/motd'
 const versionFile = 'public/version'
-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', [
@@ -96,13 +88,7 @@ Load()
 // --------------------------------------------------------------
 
 app.get('/', function (req, res) {
-  res.end('hai')
-})
-
-app.get('/manual', function (req, res) {
-  res.render('man')
-  res.end()
-  logger.LogReq(req)
+  res.redirect('https://www.youtube.com/watch?v=ieqGJgqiXFk')
 })
 
 app.post('/postfeedback', function (req, res) {
@@ -112,50 +98,6 @@ app.post('/postfeedback', function (req, res) {
   res.end()
 })
 
-app.get('/lred', function (req, res) {
-  res.redirect('/legacy') // TODO: redirect to react legacy
-  res.end()
-  logger.LogReq(req)
-})
-
-app.get('/menuClick', function (req, res) {
-  res.redirect('/') // TODO: redirect to react /
-  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('/donate', function (req, res) {
-  res.redirect(donateURL)
-  res.end()
-  logger.LogReq(req)
-})
-
-app.get('/thanks', function (req, res) {
-  res.render('thanks', { // TODO: redirect to react thanks
-    siteurl: url
-  })
-  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()
@@ -313,8 +255,5 @@ app.post('*', function (req, res) {
 })
 
 exports.app = app
-exports.setup = (x) => {
-  url = x.url
-}
 
 logger.Log('API module started', logger.GetColor('yellow'))
diff --git a/modules/qmining/qmining.js b/modules/qmining/qmining.js
index 4e4ca8e..c32c4ce 100644
--- a/modules/qmining/qmining.js
+++ b/modules/qmining/qmining.js
@@ -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'))
diff --git a/server.js b/server.js
index 47fefdf..0a2f902 100755
--- a/server.js
+++ b/server.js
@@ -19,7 +19,7 @@
  ------------------------------------------------------------------------- */
 
 const startHTTPS = true
-const port = 80
+const port = 8080
 const httpsport = 5001
 
 const express = require('express')
diff --git a/sharedViews/404.ejs b/sharedViews/404.ejs
index e97a2cb..1bb6149 100755
--- a/sharedViews/404.ejs
+++ b/sharedViews/404.ejs
@@ -18,7 +18,7 @@
 	<center>
 		<h1>404</h1>
 
-		<iframe width="660" height="465" src="https://www.youtube-nocookie.com/embed/GOzwOeONBhQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
+		<iframe width="660" height="465" src="https://www.youtube-nocookie.com/embed/qLrnkK2YEcE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
 	</center>
 </body>
 
diff --git a/utils/utils.js b/utils/utils.js
index a429d08..82c5843 100755
--- a/utils/utils.js
+++ b/utils/utils.js
@@ -7,7 +7,8 @@ module.exports = {
   WriteBackup: WriteBackup,
   FileExists: FileExists,
   CreatePath: CreatePath,
-  WatchFile: WatchFile
+  WatchFile: WatchFile,
+  ReadDir: ReadDir
 }
 
 var fs = require('fs')
@@ -16,6 +17,10 @@ var logger = require('../utils/logger.js')
 
 const dataFile = './public/data.json'
 
+function ReadDir (path) {
+  return fs.readdirSync(path)
+}
+
 function ReadFile (name) {
   if (!FileExists(name)) { throw new Error('No such file: ' + name) }
   return fs.readFileSync(name, 'utf8')