-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
97 lines (81 loc) · 3.09 KB
/
server.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
const express = require('express')
const cors = require('cors')
const got = require('got')
const path = require('path')
const fs = require('fs')
const crypto = require("crypto")
const PORT = process.env.PORT || 5000
// Settings for Toloka API
const TOLOKA_API_ENDPOINT = process.env.TOLOKA_API_ENDPOINT || 'https://sandbox.toloka.yandex.com/api/v1'
const TOLOKA_API_TOKEN = process.env.TOLOKA_API_TOKEN
// Helper function for transfering content
const transferData = (key, res) => {
var stream = fs.createReadStream(path.join(__dirname, 'data', `${key}.jpg`))
res.contentType('image/jpeg')
stream.on('open', () => {
stream.pipe(res)
})
}
const app = express()
app.use(express.json())
// Endpoint for serving data upon request
app.get('/data/:requestedKey', (req, res) => {
const assignmentId = req.query.assignmentId
const requestedKey = req.params.requestedKey
// Testing mode (?assignmentId=undefined in a project preview)
if (assignmentId == 'undefined') {
transferData('undefined', res)
return
}
// Call Toloka back to get information about the assignment with given id
got(`${TOLOKA_API_ENDPOINT}/assignments/${assignmentId}`, {
headers: {
'Authorization': `OAuth ${TOLOKA_API_TOKEN}`
},
responseType: 'json'
})
.then(tlkRes => {
if (tlkRes.statusCode == 200) {
// Get status of the assignment
const assignmentStatus = tlkRes.body && tlkRes.body.status
// Check whether the assignment is active
if (assignmentStatus != 'ACTIVE') {
// If no, access is denied
res.status(403).send('Assignment is inactive');
return
}
const keyMatched =
tlkRes.body &&
tlkRes.body.tasks &&
tlkRes.body.tasks.some(t => t.input_values && t.input_values.input_key == requestedKey)
if (!keyMatched) {
// If no, access is denied
res.status(403).send('Key mismatched')
} else {
// If yes, serve content by the requested key
transferData(requestedKey, res)
}
}
})
.catch(error => {
console.log(error)
res.status(403).send('Error while Toloka API call')
})
})
// Setup CORS to be able send AJAX requests from toloker interface
const corsOptions = {
origin: ['https://iframe-toloka.com', 'https://sandbox.iframe-toloka.com']
}
app.options('/result', cors(corsOptions))
// Endpoint for storing results and returning output_key back
app.put('/result', cors(corsOptions), (req, res) => {
// Generate random output key (possible collisions, just for example)
const outputKey = crypto.randomBytes(3).toString('hex')
// Log the key and the real result (here should be some persistence)
console.log(`Got for key=${outputKey} result: ${JSON.stringify(req.body)}`)
// Send the key for "stored" data back to Toloka
res.send({
key: outputKey
})
})
app.listen(PORT, () => console.log(`Listening on ${ PORT }`))