-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
144 lines (121 loc) · 3.28 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const { spawn } = require('child_process')
const express = require('express')
const redis = require('redis')
const router = express.Router()
router.use(express.json())
const {
REDIS_URL_STAGING: REDIS_URL = 'redis://@localhost:6379'
} = process.env;
const client = redis.createClient({ url: REDIS_URL });
client.on('connect', () => console.log('Redis connected!'))
client.on('error', console.error)
const getAsync = promisify(client.get).bind(client)
const { catchErrors } = require('./utils')
/**
* Fetches data by key from redis cache
*
* @param {string} key - key under which data is stored
* @returns {Object}
*/
async function fromCache(key) {
const cached = await getAsync(key)
return JSON.parse(cached)
}
async function getNavigation(req, res, next) {
const value = await fromCache('nav')
if (!value) {
return next()
}
return res.json(value)
}
async function getEPFL(req, res, next) {
const {
level = '',
program = '',
specialization = ''
} = req.params
const redisKey = ['epfl', level, program, specialization].filter(s => s).join('_')
const value = await fromCache(redisKey)
if (!value) {
return next()
}
return res.json(value)
}
async function getCourse(req, res, next) {
const { slug } = req.params
const redisKey = `course_${slug}`
const value = await fromCache(redisKey)
if (!value) {
return next()
}
return res.json(value)
}
async function submitQuery(req, res, next) {
const { query, topk = 10 } = req.query
if (!query) {
return res.status(400).json({ error: 'Parameter <query> is missing' })
}
const cmd = `python ./py/search/query.py ${topk} ${query}`
console.log(query)
const { stdout, stderr } = await exec(cmd)
console.log('stdout:', stdout)
console.error('stderr:', stderr)
if (stderr) {
return res.status(500).json({ error: stderr })
}
return res.json(JSON.parse(stdout))
}
async function findSimlinks(req, res, next) {
const { threshold, slugs } = req.body
// todo improve request body validation?
if (!Array.isArray(slugs) || !threshold) {
return res.status(400).json({ error: 'Invalid parameters' })
}
const child = spawn('python', ['./py/search/simlinks.py'])
child.stderr.pipe(process.stderr)
child.stdin.write(`${threshold}#${slugs}`)
child.stdin.end() // important to call end()!
let data = ''
for await (const chunk of child.stdout) {
data += chunk
}
return res.json(JSON.parse(data))
}
router.get('/', (req, res) => {
res.json({
'/epfl': {
'/': 'Root of EPFL tree',
'/:level': 'Academic level',
'/:level/:program': 'Study program',
'/master/:program/:specialization': 'Master specialization'
},
'/course': {
'/:slug': 'Course information',
'/search?query=<query>&topk=10': 'Keyword search'
},
'/nav': 'Treeview navigation structure',
})
})
router.get(
'/nav',
catchErrors(getNavigation)
)
router.get(
'/epfl/:level([a-z-]+)?/:program([a-z-]+)?/:specialization([a-z-]+)?',
catchErrors(getEPFL)
)
router.get(
'/course/:slug([a-z0-9-]+)',
catchErrors(getCourse)
)
router.get(
'/course/search',
catchErrors(submitQuery)
)
router.post(
'/simlinks',
catchErrors(findSimlinks)
)
module.exports = router