mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
prettier 4 tabwidth
This commit is contained in:
parent
00ec614f1d
commit
96b413a365
42 changed files with 7034 additions and 6905 deletions
|
@ -36,212 +36,219 @@ let userDB: Database
|
|||
let nextdir = ''
|
||||
|
||||
function GetApp(): ModuleType {
|
||||
app.use(
|
||||
express.urlencoded({
|
||||
limit: '5mb',
|
||||
extended: true,
|
||||
}) as RequestHandler
|
||||
)
|
||||
app.use(
|
||||
express.json({
|
||||
limit: '5mb',
|
||||
}) as RequestHandler
|
||||
)
|
||||
app.set('view engine', 'ejs')
|
||||
app.set('views', ['./src/modules/qmining/views', './src/sharedViews'])
|
||||
app.use(
|
||||
auth({
|
||||
userDB: userDB,
|
||||
jsonResponse: false,
|
||||
exceptions: ['/favicon.ico', '/img/frylabs-logo_large_transparent.png'],
|
||||
})
|
||||
)
|
||||
app.use((req: Request, _res, next) => {
|
||||
const url = req.url.split('?')[0]
|
||||
if (url.includes('.html') || url === '/') {
|
||||
logger.LogReq(req)
|
||||
}
|
||||
next()
|
||||
})
|
||||
publicdirs.forEach((pdir) => {
|
||||
logger.Log(`Using public dir: ${pdir}`)
|
||||
app.use(express.static(pdir))
|
||||
})
|
||||
app.use(express.static(nextdir))
|
||||
const linksFile = 'data/links.json'
|
||||
let links: { [key: string]: string } = {}
|
||||
|
||||
function loadDonateURL() {
|
||||
try {
|
||||
links = utils.ReadJSON(linksFile)
|
||||
} catch (err) {
|
||||
logger.Log('Couldnt read donate URL file!', logger.GetColor('red'))
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
loadDonateURL()
|
||||
|
||||
if (utils.FileExists(linksFile)) {
|
||||
utils.WatchFile(linksFile, (newData: string) => {
|
||||
logger.Log(`Donate URL changed: ${newData.replace(/\/n/g, '')}`)
|
||||
loadDonateURL()
|
||||
})
|
||||
} else {
|
||||
logger.Log('Couldnt read donate URL file!', logger.GetColor('red'))
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// 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}`
|
||||
app.use(
|
||||
express.urlencoded({
|
||||
limit: '5mb',
|
||||
extended: true,
|
||||
}) as RequestHandler
|
||||
)
|
||||
})
|
||||
|
||||
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: links.donate,
|
||||
},
|
||||
{
|
||||
from: '/menuClick',
|
||||
to: '/',
|
||||
},
|
||||
{
|
||||
from: '/legacy',
|
||||
to: '/allQuestions.html',
|
||||
},
|
||||
{
|
||||
from: '/subjectBrowser',
|
||||
to: '/allQuestions.html',
|
||||
},
|
||||
{
|
||||
from: '/lred',
|
||||
to: '/allQuestions',
|
||||
},
|
||||
{
|
||||
from: '/allqr',
|
||||
to: 'https://api.frylabs.net/allqr.txt',
|
||||
},
|
||||
{
|
||||
from: '/allqr.txt',
|
||||
to: 'https://api.frylabs.net/allqr.txt',
|
||||
},
|
||||
{
|
||||
from: '/infos',
|
||||
to: 'https://api.frylabs.net/infos?version=true&motd=true&subjinfo=true',
|
||||
nolog: true,
|
||||
},
|
||||
{
|
||||
from: '/irc',
|
||||
to: '/chat',
|
||||
},
|
||||
{
|
||||
from: '/patreon',
|
||||
to: links.patreon,
|
||||
},
|
||||
]
|
||||
|
||||
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('https://')) {
|
||||
target += utils.formatUrl({ query: req.query })
|
||||
}
|
||||
|
||||
res.redirect(target)
|
||||
app.use(
|
||||
express.json({
|
||||
limit: '5mb',
|
||||
}) as RequestHandler
|
||||
)
|
||||
app.set('view engine', 'ejs')
|
||||
app.set('views', ['./src/modules/qmining/views', './src/sharedViews'])
|
||||
app.use(
|
||||
auth({
|
||||
userDB: userDB,
|
||||
jsonResponse: false,
|
||||
exceptions: [
|
||||
'/favicon.ico',
|
||||
'/img/frylabs-logo_large_transparent.png',
|
||||
],
|
||||
})
|
||||
)
|
||||
app.use((req: Request, _res, next) => {
|
||||
const url = req.url.split('?')[0]
|
||||
if (url.includes('.html') || url === '/') {
|
||||
logger.LogReq(req)
|
||||
}
|
||||
next()
|
||||
})
|
||||
})
|
||||
publicdirs.forEach((pdir) => {
|
||||
logger.Log(`Using public dir: ${pdir}`)
|
||||
app.use(express.static(pdir))
|
||||
})
|
||||
app.use(express.static(nextdir))
|
||||
const linksFile = 'data/links.json'
|
||||
let links: { [key: string]: string } = {}
|
||||
|
||||
// --------------------------------------------------------------
|
||||
function loadDonateURL() {
|
||||
try {
|
||||
links = utils.ReadJSON(linksFile)
|
||||
} catch (err) {
|
||||
logger.Log('Couldnt read donate URL file!', logger.GetColor('red'))
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
function AddHtmlRoutes(files: string[]) {
|
||||
const routes = files.reduce((acc, file) => {
|
||||
if (file.includes('html')) {
|
||||
acc.push(file.split('.')[0])
|
||||
return acc
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
loadDonateURL()
|
||||
|
||||
routes.forEach((route: string) => {
|
||||
logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1)
|
||||
app.get(`/${route}`, function (req: Request, res) {
|
||||
if (utils.FileExists(linksFile)) {
|
||||
utils.WatchFile(linksFile, (newData: string) => {
|
||||
logger.Log(`Donate URL changed: ${newData.replace(/\/n/g, '')}`)
|
||||
loadDonateURL()
|
||||
})
|
||||
} else {
|
||||
logger.Log('Couldnt read donate URL file!', logger.GetColor('red'))
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// REDIRECTS
|
||||
// --------------------------------------------------------------
|
||||
|
||||
// to be backwards compatible
|
||||
app.get('/ask', function (req: Request, res) {
|
||||
logger.DebugLog(`Qmining module ask redirect`, 'ask', 1)
|
||||
res.redirect(
|
||||
utils.formatUrl({
|
||||
pathname: `${route}.html`,
|
||||
query: req.query,
|
||||
})
|
||||
`http://api.frylabs.net/ask?q=${req.query.q}&subj=${req.query.subj}&data=${req.query.data}`
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
AddHtmlRoutes(utils.ReadDir(nextdir))
|
||||
|
||||
// --------------------------------------------------------------
|
||||
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: links.donate,
|
||||
},
|
||||
{
|
||||
from: '/menuClick',
|
||||
to: '/',
|
||||
},
|
||||
{
|
||||
from: '/legacy',
|
||||
to: '/allQuestions.html',
|
||||
},
|
||||
{
|
||||
from: '/subjectBrowser',
|
||||
to: '/allQuestions.html',
|
||||
},
|
||||
{
|
||||
from: '/lred',
|
||||
to: '/allQuestions',
|
||||
},
|
||||
{
|
||||
from: '/allqr',
|
||||
to: 'https://api.frylabs.net/allqr.txt',
|
||||
},
|
||||
{
|
||||
from: '/allqr.txt',
|
||||
to: 'https://api.frylabs.net/allqr.txt',
|
||||
},
|
||||
{
|
||||
from: '/infos',
|
||||
to: 'https://api.frylabs.net/infos?version=true&motd=true&subjinfo=true',
|
||||
nolog: true,
|
||||
},
|
||||
{
|
||||
from: '/irc',
|
||||
to: '/chat',
|
||||
},
|
||||
{
|
||||
from: '/patreon',
|
||||
to: links.patreon,
|
||||
},
|
||||
]
|
||||
|
||||
app.get('/', function (req: Request, res) {
|
||||
res.end('hai')
|
||||
logger.LogReq(req)
|
||||
})
|
||||
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
|
||||
)
|
||||
|
||||
app.get('*', function (_req: Request, res) {
|
||||
res.status(404).render('404')
|
||||
})
|
||||
let target = redirect.to
|
||||
if (!redirect.to.includes('https://')) {
|
||||
target += utils.formatUrl({ query: req.query })
|
||||
}
|
||||
|
||||
app.post('*', function (_req: Request, res) {
|
||||
res.status(404).render('404')
|
||||
})
|
||||
res.redirect(target)
|
||||
})
|
||||
})
|
||||
|
||||
return {
|
||||
app: app,
|
||||
}
|
||||
// --------------------------------------------------------------
|
||||
|
||||
function AddHtmlRoutes(files: string[]) {
|
||||
const routes = files.reduce((acc, file) => {
|
||||
if (file.includes('html')) {
|
||||
acc.push(file.split('.')[0])
|
||||
return acc
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
|
||||
routes.forEach((route: string) => {
|
||||
logger.DebugLog(`Added route /${route}`, 'Qmining routes', 1)
|
||||
app.get(`/${route}`, function (req: Request, res) {
|
||||
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
|
||||
},
|
||||
name: 'Qmining',
|
||||
getApp: GetApp,
|
||||
setup: (data: SetupData): void => {
|
||||
userDB = data.userDB
|
||||
publicdirs = data.publicdirs
|
||||
nextdir = data.nextdir
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue