mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
250 lines
6 KiB
TypeScript
250 lines
6 KiB
TypeScript
/* ----------------------------------------------------------------------------
|
|
|
|
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/>.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
// package requires
|
|
import express from 'express'
|
|
import bodyParser from 'body-parser'
|
|
import busboy from 'connect-busboy'
|
|
const app = express()
|
|
|
|
// other requires
|
|
import utils from '../../utils/utils'
|
|
import logger from '../../utils/logger'
|
|
import auth from '../../middlewares/auth.middleware'
|
|
import { SetupData } from '../../server'
|
|
import { ModuleType, Request } from '../../types/basicTypes'
|
|
|
|
// stuff gotten from server.js
|
|
let publicdirs = []
|
|
let userDB
|
|
let nextdir = ''
|
|
|
|
function GetApp(): ModuleType {
|
|
app.use(
|
|
bodyParser.urlencoded({
|
|
limit: '5mb',
|
|
extended: true,
|
|
})
|
|
)
|
|
app.use(
|
|
bodyParser.json({
|
|
limit: '5mb',
|
|
})
|
|
)
|
|
app.set('view engine', 'ejs')
|
|
app.set('views', ['./src/modules/qmining/views', './src/sharedViews'])
|
|
app.use(
|
|
auth({
|
|
userDB: userDB,
|
|
jsonResponse: false,
|
|
exceptions: [
|
|
'/thanks',
|
|
'/thanks.html',
|
|
'/img/thanks.gif',
|
|
'/favicon.ico',
|
|
'/moodle-test-userscript/stable.user.js',
|
|
'/irc',
|
|
'/donate',
|
|
],
|
|
})
|
|
)
|
|
publicdirs.forEach((pdir) => {
|
|
logger.Log(`Using public dir: ${pdir}`)
|
|
app.use(express.static(pdir))
|
|
})
|
|
app.use(express.static(nextdir))
|
|
app.use(
|
|
busboy({
|
|
limits: {
|
|
fileSize: 10000 * 1024 * 1024,
|
|
},
|
|
})
|
|
)
|
|
|
|
let donateURL = ''
|
|
const donateURLFile = './data/donateURL'
|
|
|
|
function loadDonateURL() {
|
|
try {
|
|
donateURL = utils.ReadFile(donateURLFile).replace(/\n/g, '')
|
|
} catch (err) {
|
|
logger.Log('Couldnt read donate URL file!', logger.GetColor('red'))
|
|
console.error(err)
|
|
}
|
|
}
|
|
|
|
loadDonateURL()
|
|
|
|
utils.WatchFile(donateURLFile, (newData) => {
|
|
logger.Log(`Donate URL changed: ${newData.replace(/\/n/g, '')}`)
|
|
loadDonateURL()
|
|
})
|
|
|
|
// --------------------------------------------------------------
|
|
// REDIRECTS
|
|
// --------------------------------------------------------------
|
|
|
|
// to be backwards compatible
|
|
app.get('/ask', function(req: Request, 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}`
|
|
)
|
|
})
|
|
|
|
const simpleRedirects = [
|
|
{
|
|
from: '/dataeditor',
|
|
to: 'https://dataeditor.frylabs.net',
|
|
},
|
|
{
|
|
from: '/install',
|
|
to: 'https://qmining.frylabs.net/moodle-test-userscript/stable.user.js',
|
|
},
|
|
{
|
|
from: '/servergit',
|
|
to: 'https://gitlab.com/MrFry/mrfrys-node-server',
|
|
},
|
|
{
|
|
from: '/scriptgit',
|
|
to: 'https://gitlab.com/MrFry/moodle-test-userscript',
|
|
},
|
|
{
|
|
from: '/qminingSite',
|
|
to: 'https://gitlab.com/MrFry/qmining-page',
|
|
},
|
|
{
|
|
from: '/classesgit',
|
|
to: 'https://gitlab.com/MrFry/question-classes',
|
|
},
|
|
{
|
|
from: '/addQuestion',
|
|
to: 'https://dataeditor.frylabs.net',
|
|
},
|
|
{
|
|
from: '/donate',
|
|
to: donateURL,
|
|
},
|
|
{
|
|
from: '/menuClick',
|
|
to: '/',
|
|
},
|
|
{
|
|
from: '/legacy',
|
|
to: '/allQuestions.html',
|
|
},
|
|
{
|
|
from: '/subjectBrowser',
|
|
to: '/allQuestions.html',
|
|
},
|
|
{
|
|
from: '/lred',
|
|
to: '/allQuestions',
|
|
},
|
|
{
|
|
from: '/allqr',
|
|
to: 'http://api.frylabs.net/allqr.txt',
|
|
},
|
|
{
|
|
from: '/allqr.txt',
|
|
to: 'http://api.frylabs.net/allqr.txt',
|
|
},
|
|
{
|
|
from: '/infos',
|
|
to: 'http://api.frylabs.net/infos?version=true&motd=true&subjinfo=true',
|
|
nolog: true,
|
|
},
|
|
{
|
|
from: '/irc',
|
|
to: 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining',
|
|
},
|
|
]
|
|
|
|
simpleRedirects.forEach((redirect) => {
|
|
app.get(redirect.from, function(req: Request, res) {
|
|
if (!redirect.nolog) {
|
|
logger.LogReq(req)
|
|
}
|
|
logger.DebugLog(`Qmining module ${redirect.from} redirect`, 'infos', 1)
|
|
|
|
let target = redirect.to
|
|
if (!redirect.to.includes('?')) {
|
|
target += utils.formatUrl({ query: req.query })
|
|
}
|
|
|
|
res.redirect(target)
|
|
})
|
|
})
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
function AddHtmlRoutes(files) {
|
|
const routes = files.reduce((acc, file) => {
|
|
if (file.includes('html')) {
|
|
acc.push(file.split('.')[0])
|
|
return acc
|
|
}
|
|
return acc
|
|
}, [])
|
|
|
|
routes.forEach((route) => {
|
|
logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1)
|
|
app.get(`/${route}`, function(req: Request, res) {
|
|
logger.LogReq(req)
|
|
res.redirect(
|
|
utils.formatUrl({
|
|
pathname: `${route}.html`,
|
|
query: req.query,
|
|
})
|
|
)
|
|
})
|
|
})
|
|
}
|
|
AddHtmlRoutes(utils.ReadDir(nextdir))
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
app.get('/', function(req: Request, res) {
|
|
res.end('hai')
|
|
logger.LogReq(req)
|
|
})
|
|
|
|
app.get('*', function(req: Request, res) {
|
|
res.status(404).render('404')
|
|
})
|
|
|
|
app.post('*', function(req: Request, res) {
|
|
res.status(404).render('404')
|
|
})
|
|
|
|
return {
|
|
app: app,
|
|
}
|
|
}
|
|
|
|
export default {
|
|
name: 'Qmining',
|
|
getApp: GetApp,
|
|
setup: (data: SetupData): void => {
|
|
userDB = data.userDB
|
|
publicdirs = data.publicdirs
|
|
nextdir = data.nextdir
|
|
},
|
|
}
|