diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-02-07 16:04:31 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-02-07 16:04:31 +0100 |
commit | 074b6e3cc6ce3917459175ebc7481b58d3ca728f (patch) | |
tree | d9d8859abaefceb419ca937c56b76ab633023c4d /src/server/routes | |
parent | 5ebd34ed83f878976b89da78d6bdaa1dc9e935d7 (diff) |
Fix the backend
Diffstat (limited to 'src/server/routes')
-rw-r--r-- | src/server/routes/getSchedule.js | 15 | ||||
-rw-r--r-- | src/server/routes/index.js | 41 | ||||
-rw-r--r-- | src/server/routes/manifest.js | 21 | ||||
-rw-r--r-- | src/server/routes/opensearch.js | 12 | ||||
-rw-r--r-- | src/server/routes/slack.js | 60 |
5 files changed, 31 insertions, 118 deletions
diff --git a/src/server/routes/getSchedule.js b/src/server/routes/getSchedule.js index f6bc256..5e9bb29 100644 --- a/src/server/routes/getSchedule.js +++ b/src/server/routes/getSchedule.js @@ -3,9 +3,8 @@ const express = require('express'); const router = express.Router(); const request = require('request'); const iconv = require('iconv-lite'); -const webshot = require('webshot'); -const getUserIndex = require('../lib/getUserIndex'); +const getMeetingpointData = require('../lib/getMeetingpointData'); const getURLOfUser = require('../lib/getURLOfUser'); // copied from http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/untisscripts.js, @@ -25,18 +24,8 @@ function getWeekNumber(target) { return 1 + Math.ceil((firstThursday - target) / 604800000); } -// router.get('/:type/:value.png', (req, res) => { -// const port = process.env.PORT || 3000; -// const { type, value } = req.params; -// const stream = webshot( -// `http://localhost:${port}/get/${type}/${value}`, -// { customCSS: 'body { background-color: white; }' }, -// ); -// stream.pipe(res); -// }); - router.get('/:type/:value', (req, res, next) => { - getUserIndex().then((users) => { + getMeetingpointData().then(({ users }) => { const { type, value } = req.params; let { week } = req.query; const user = diff --git a/src/server/routes/index.js b/src/server/routes/index.js index d2267ba..7e97fea 100644 --- a/src/server/routes/index.js +++ b/src/server/routes/index.js @@ -1,31 +1,30 @@ -'use strict' +const express = require('express'); -const express = require('express') -const router = express.Router() -const getMeetingpointData = require('../lib/getMeetingpointData') +const router = express.Router(); +const getMeetingpointData = require('../lib/getMeetingpointData'); /* GET home page. */ -router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) { - getMeetingpointData().then(data => { - const isBeta = process.env.BETA === '1' +router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], (req, res) => { + getMeetingpointData().then((data) => { + const isBeta = process.env.BETA === '1'; - let flags = [] + const flags = []; if (isBeta) { - flags.push('BETA') - flags.push('NO_FEATURE_DETECT') + flags.push('BETA'); + flags.push('NO_FEATURE_DETECT'); } else if (req.query.nfd != null) { - flags.push('NO_FEATURE_DETECT') + flags.push('NO_FEATURE_DETECT'); } - const flagsStr = `var FLAGS = ${JSON.stringify(flags)};` - const usersStr = `var USERS = ${JSON.stringify(data.users)};` - const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}` + const flagsStr = `var FLAGS = ${JSON.stringify(flags)};`; + const usersStr = `var USERS = ${JSON.stringify(data.users)};`; + const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}`; - res.render('index', { flagsStr, usersStr, validWeekNumbersStr }) - }).catch(function () { - console.error('Unable to get user info, emergency redirect!') - res.render('redirect') - }) -}) + res.render('index', { flagsStr, usersStr, validWeekNumbersStr }); + }).catch(() => { + console.error('Unable to get user info, emergency redirect!'); + res.render('redirect'); + }); +}); -module.exports = router +module.exports = router; diff --git a/src/server/routes/manifest.js b/src/server/routes/manifest.js index b2ce55f..4cb6342 100644 --- a/src/server/routes/manifest.js +++ b/src/server/routes/manifest.js @@ -1,19 +1,16 @@ -'use strict' +const express = require('express'); -const express = require('express') -const router = express.Router() -const path = require('path') +const router = express.Router(); +const path = require('path'); -router.get('/', function (req, res, next) { - console.log('got a request') - - const isBeta = process.env.BETA === '1' +router.get('/', (req, res) => { + const isBeta = process.env.BETA === '1'; if (isBeta) { - res.sendFile('manifest.beta.webmanifest', { root: path.join(__dirname, '../public') }) + res.sendFile('manifest.beta.webmanifest', { root: path.join(__dirname, '../../client/static') }); } else { - res.sendFile('manifest.webmanifest', { root: path.join(__dirname, '../public') }) + res.sendFile('manifest.webmanifest', { root: path.join(__dirname, '../../client/static') }); } -}) +}); -module.exports = router +module.exports = router; diff --git a/src/server/routes/opensearch.js b/src/server/routes/opensearch.js deleted file mode 100644 index c3e2e57..0000000 --- a/src/server/routes/opensearch.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict' - -const express = require('express') -const router = express.Router() -const path = require('path') - -router.get('/', function (req, res, next) { - res.setHeader('content-type', 'application/opensearchdescription+xml') - res.sendFile('opensearch.xml', { root: path.join(__dirname, '../public') }) -}) - -module.exports = router diff --git a/src/server/routes/slack.js b/src/server/routes/slack.js deleted file mode 100644 index e256f14..0000000 --- a/src/server/routes/slack.js +++ /dev/null @@ -1,60 +0,0 @@ -const express = require('express') -const router = express.Router() - -const getUserIndex = require('../lib/getUserIndex') - -function generateResponse(query) { - return getUserIndex().then(users => { - const user = - users.filter(user => user.value === query)[0] - - if (!user) { - return { - "response_type": "ephemeral", - "mrkdwn": true, - "text": `Sorry, I tried my best, but I couldn't find _${query}_` - } - } - - return { - "response_type": "in_channel", - "text": `Here is the schedule of _${query}_`, - "mrkdwn": true, - "attachments": [ - { - "fallback": `https://beta.rooster.hetmml.nl/${user.type}/${user.value}`, - "image_url": `https://beta.rooster.hetmml.nl/get/${user.type}/${user.value}.png` - } - ] - } - }) -} - -router.all('/', function (req, res, next) { - const query = req.body.text || req.query.text - - if (query.indexOf('!') === 0) { - switch (query) { - case "!help": - res.json({ - "response_type": "ephemeral", - "mrkdwn": true, - "text": "Here are some examples on how you can use me\n>/rooster 18561\n\n>/rooster akh\n\n>/rooster 6-5H2\n\n>/rooster 008-mk\n\nPlease note that the following does not work (yet)", - "attachments": [{ - "text": "/rooster 5h2", - "color": "danger" - }] - }) - default: - res.json({ - "response_type": "ephemeral", - "text": "Unrecognized command, try !help" - }) - break; - } - } - - generateResponse(query).then((json) => res.json(json)) -}) - -module.exports = router |