From a6a4ddc10ba1a458b94b13c59a4081e9ce1e5b33 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Fri, 26 May 2017 18:35:51 +0200 Subject: Add emergency redirect --- lib/getUserIndex.js | 17 +++++------------ routes/index.js | 37 +++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lib/getUserIndex.js b/lib/getUserIndex.js index d71cb3b..6288dc3 100644 --- a/lib/getUserIndex.js +++ b/lib/getUserIndex.js @@ -4,12 +4,8 @@ const Promise = require('bluebird') const cheerio = require('cheerio') const request = Promise.promisify(require('request')) -exports = {} -module.exports = exports - -function getStandardUsers () { - return new Promise(function (resolve, reject) { - request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`) +function getUserIndex () { + return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`) .then(function (page) { page = page.body @@ -17,14 +13,14 @@ function getStandardUsers () { const $script = $('script').eq(1) const scriptText = $script.text() - const regexs = [/var classes = \[(.+)\];/, /var teachers = \[(.+)\];/, /var rooms = \[(.+)\];/, /var students = \[(.+)\];/] + const regexs = [/var classes = \[(.+)];/, /var teachers = \[(.+)];/, /var rooms = \[(.+)];/, /var students = \[(.+)];/] const items = regexs.map(function (regex) { return scriptText.match(regex)[1].split(',').map(function (item) { return item.replace(/"/g, '') }) }) - resolve([] + return ([] .concat(items[0].map(function (item, index) { return { type: 'c', @@ -54,9 +50,6 @@ function getStandardUsers () { } }))) }) - }) } -getStandardUsers().then(users => { - exports.users = users -}) +module.exports = getUserIndex diff --git a/routes/index.js b/routes/index.js index a65ac69..0fc0d30 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,24 +2,33 @@ const express = require('express') const router = express.Router() -const users = require('../lib/getUserIndex') +const getUserIndex = require('../lib/getUserIndex') -/* GET home page. */ -router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) { - const isBeta = process.env.BETA === '1' +getUserIndex().then(users => { + /* GET home page. */ + router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) { + const isBeta = process.env.BETA === '1' - let flags = [] - if (isBeta) { - flags.push('BETA') - flags.push('NO_FEATURE_DETECT') - } else if (req.query.nfd != null) { - flags.push('NO_FEATURE_DETECT') - } + let flags = [] + if (isBeta) { + flags.push('BETA') + flags.push('NO_FEATURE_DETECT') + } else if (req.query.nfd != null) { + flags.push('NO_FEATURE_DETECT') + } - const flagsStr = `var FLAGS = ${JSON.stringify(flags)};` - const usersStr = `var USERS = ${JSON.stringify(users.users)};` + const flagsStr = `var FLAGS = ${JSON.stringify(flags)};` + const usersStr = `var USERS = ${JSON.stringify(users)};` - res.render('index', { flagsStr, usersStr, isBeta }) + res.render('index', { flagsStr, usersStr, isBeta }) + }) +}, error => { + console.error('Unable to get user info, emergency redirect!') + console.error('Error:', error) + + router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) { + res.redirect('http://www.meetingpointmco.nl/Roosters-AL/doc/') + }) }) module.exports = router -- cgit v1.1