]>
Commit | Line | Data |
---|---|---|
7ad9b984 C |
1 | import * as express from 'express' |
2 | import { logger } from '../../helpers/logger' | |
3 | import { areValidationErrors } from './utils' | |
4 | import { isHostValid } from '../../helpers/custom-validators/servers' | |
5 | import { ServerModel } from '../../models/server/server' | |
6 | import { body } from 'express-validator/check' | |
7 | ||
8 | const serverGetValidator = [ | |
9 | body('host').custom(isHostValid).withMessage('Should have a valid host'), | |
10 | ||
11 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
12 | logger.debug('Checking serverGetValidator parameters', { parameters: req.body }) | |
13 | ||
14 | if (areValidationErrors(req, res)) return | |
15 | ||
16 | const server = await ServerModel.loadByHost(req.body.host) | |
17 | if (!server) { | |
18 | return res.status(404) | |
19 | .send({ error: 'Server host not found.' }) | |
20 | .end() | |
21 | } | |
22 | ||
23 | res.locals.server = server | |
24 | ||
25 | return next() | |
26 | } | |
27 | ] | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
31 | export { | |
32 | serverGetValidator | |
33 | } |