-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
88 lines (78 loc) · 1.68 KB
/
routes.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
const { check, validationResult } = require('express-validator');
// The queue
const queue = require('./queue');
// Get config.json
const { config } = require('./config');
// Validation and escaping
const validate = (method) => {
const validations = [
check('branchTag')
.not()
.isEmpty()
.trim()
.escape(),
check('composeType')
.not()
.isEmpty()
.trim()
.custom((value) => {
if (!['default'].includes(value)) {
return Promise.reject(new Error('Provided composeType is not supported.'));
}
return true;
})
.escape(),
];
switch (method) {
case 'warmers': {
return [
...validations,
check('imageTag')
.not()
.isEmpty()
.trim()
.escape(),
];
}
default:
return validations;
}
};
// Validation errors handler
const validationErrorHandler = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
return next();
};
// Request handler
const postHandler = (req, res, type) => {
// Use provided values.
let priority = 0;
if (type === 'run') {
priority = config.queue.priority.runner;
} else if (type === 'warmup') {
priority = config.queue.priority.warmer;
}
queue
.push(
priority,
{
...req.body,
type,
},
config.queue.defaultExpire,
)
.then(() => {
res.json({ success: true });
})
.catch((error) => {
console.log('Failed to add task.', error);
});
};
module.exports = {
validate,
validationErrorHandler,
postHandler,
};