aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/getSchedule.js55
-rw-r--r--routes/hello.js9
-rw-r--r--routes/index.js29
-rw-r--r--routes/manifest.js19
-rw-r--r--routes/meetingpointProxy.js19
-rw-r--r--routes/opensearch.js12
6 files changed, 106 insertions, 37 deletions
diff --git a/routes/getSchedule.js b/routes/getSchedule.js
new file mode 100644
index 0000000..f6c3cb6
--- /dev/null
+++ b/routes/getSchedule.js
@@ -0,0 +1,55 @@
+const express = require('express')
+const router = express.Router()
+const request = require('request')
+const iconv = require('iconv-lite')
+
+const getUserIndex = require('../lib/getUserIndex')
+const getURLOfUser = require('../lib/getURLOfUser')
+
+// copied from http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/untisscripts.js,
+// were using the same code as they do to be sure that we always get the same
+// week number.
+function getWeekNumber (target) {
+ const dayNr = (target.getDay() + 6) % 7
+ target.setDate(target.getDate() - dayNr + 3)
+ const firstThursday = target.valueOf()
+ target.setMonth(0, 1)
+ if (target.getDay() !== 4) {
+ target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7)
+ }
+
+ return 1 + Math.ceil((firstThursday - target) / 604800000)
+}
+
+router.get('/:type/:value', function (req, res, next) {
+ getUserIndex().then(users => {
+ const { type, value } = req.params
+ let { week } = req.query
+ const user =
+ users.filter(user => user.type === type && user.value === value)[0]
+
+ if (!user) {
+ next(new Error(`${type}${value} is not in the user index.`))
+ }
+
+ if (!week) {
+ week = getWeekNumber(new Date())
+ }
+
+ const { index } = user
+
+ const url = getURLOfUser(type, index, week)
+
+ request(url, { encoding: null }, function (err, data) {
+ if (err) {
+ next(err)
+ return
+ }
+
+ const utf8Body = iconv.decode(data.body, 'ISO-8859-1')
+ res.status(data.statusCode).end(utf8Body)
+ })
+ })
+})
+
+module.exports = router
diff --git a/routes/hello.js b/routes/hello.js
deleted file mode 100644
index 6356b98..0000000
--- a/routes/hello.js
+++ /dev/null
@@ -1,9 +0,0 @@
-var express = require('express')
-var router = express.Router()
-
-/* GET home page. */
-router.get('/', function (req, res, next) {
- res.render('hello')
-})
-
-module.exports = router
diff --git a/routes/index.js b/routes/index.js
index 1ab2ba3..3718015 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -2,18 +2,29 @@
const express = require('express')
const router = express.Router()
-const users = require('../lib/getUserIndex')
+const getUserIndex = require('../lib/getUserIndex')
/* GET home page. */
-router.get('/', function (req, res, next) {
- let flags = []
- if (req.query.nfd != null) {
- flags.push('NO_FEATURE_DETECT')
- }
+router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) {
+ getUserIndex().then(users => {
+ const isBeta = process.env.BETA === '1'
- const flagsStr = `var FLAGS = ${JSON.stringify(flags)};`
- const usersStr = `var USERS = ${JSON.stringify(users.users)};`
- res.render('index', { flagsStr, usersStr })
+ 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)};`
+
+ res.render('index', { flagsStr, usersStr, isBeta })
+ }).catch(function () {
+ console.error('Unable to get user info, emergency redirect!')
+ res.redirect('http://www.meetingpointmco.nl/Roosters-AL/doc/')
+ })
})
module.exports = router
diff --git a/routes/manifest.js b/routes/manifest.js
new file mode 100644
index 0000000..b2ce55f
--- /dev/null
+++ b/routes/manifest.js
@@ -0,0 +1,19 @@
+'use strict'
+
+const express = require('express')
+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'
+
+ if (isBeta) {
+ res.sendFile('manifest.beta.webmanifest', { root: path.join(__dirname, '../public') })
+ } else {
+ res.sendFile('manifest.webmanifest', { root: path.join(__dirname, '../public') })
+ }
+})
+
+module.exports = router
diff --git a/routes/meetingpointProxy.js b/routes/meetingpointProxy.js
deleted file mode 100644
index 68f3fdf..0000000
--- a/routes/meetingpointProxy.js
+++ /dev/null
@@ -1,19 +0,0 @@
-const express = require('express')
-const router = express.Router()
-const request = require('request')
-const iconv = require('iconv-lite')
-
-router.get('/:url', function (req, res, next) {
- const url = `http://www.meetingpointmco.nl/${req.params.url}`
- request(url, { encoding: null }, function (err, data) {
- if (err) {
- next(err)
- return
- }
-
- const utf8Body = iconv.decode(data.body, 'ISO-8859-1')
- res.status(data.statusCode).end(utf8Body)
- })
-})
-
-module.exports = router
diff --git a/routes/opensearch.js b/routes/opensearch.js
new file mode 100644
index 0000000..c3e2e57
--- /dev/null
+++ b/routes/opensearch.js
@@ -0,0 +1,12 @@
+'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