aboutsummaryrefslogtreecommitdiff
path: root/src/server/routes
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-09-13 16:28:53 +0200
committerNoah Loomans <noahloomans@gmail.com>2017-09-13 16:28:53 +0200
commit3fb86482404e11942cd83c3500a297a3991db0e4 (patch)
treec5e7261de72c2b7f871580784525d06d036a6219 /src/server/routes
parent5aac32f72eca8c66e879583ce653d07bb3c7370f (diff)
Restructure project
Diffstat (limited to 'src/server/routes')
-rw-r--r--src/server/routes/getSchedule.js55
-rw-r--r--src/server/routes/index.js31
-rw-r--r--src/server/routes/manifest.js19
-rw-r--r--src/server/routes/opensearch.js12
4 files changed, 117 insertions, 0 deletions
diff --git a/src/server/routes/getSchedule.js b/src/server/routes/getSchedule.js
new file mode 100644
index 0000000..f6c3cb6
--- /dev/null
+++ b/src/server/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/src/server/routes/index.js b/src/server/routes/index.js
new file mode 100644
index 0000000..d2267ba
--- /dev/null
+++ b/src/server/routes/index.js
@@ -0,0 +1,31 @@
+'use strict'
+
+const express = require('express')
+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'
+
+ 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(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')
+ })
+})
+
+module.exports = router
diff --git a/src/server/routes/manifest.js b/src/server/routes/manifest.js
new file mode 100644
index 0000000..b2ce55f
--- /dev/null
+++ b/src/server/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/src/server/routes/opensearch.js b/src/server/routes/opensearch.js
new file mode 100644
index 0000000..c3e2e57
--- /dev/null
+++ b/src/server/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