diff options
Diffstat (limited to 'server/controllers/api/pods.ts')
-rw-r--r-- | server/controllers/api/pods.ts | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts index 804aa0659..bf1b744e5 100644 --- a/server/controllers/api/pods.ts +++ b/server/controllers/api/pods.ts | |||
@@ -16,7 +16,8 @@ import { | |||
16 | paginationValidator, | 16 | paginationValidator, |
17 | setPagination, | 17 | setPagination, |
18 | setPodsSort, | 18 | setPodsSort, |
19 | podsSortValidator | 19 | podsSortValidator, |
20 | asyncMiddleware | ||
20 | } from '../../middlewares' | 21 | } from '../../middlewares' |
21 | import { PodInstance } from '../../models' | 22 | import { PodInstance } from '../../models' |
22 | 23 | ||
@@ -27,25 +28,25 @@ podsRouter.get('/', | |||
27 | podsSortValidator, | 28 | podsSortValidator, |
28 | setPodsSort, | 29 | setPodsSort, |
29 | setPagination, | 30 | setPagination, |
30 | listPods | 31 | asyncMiddleware(listPods) |
31 | ) | 32 | ) |
32 | podsRouter.post('/make-friends', | 33 | podsRouter.post('/make-friends', |
33 | authenticate, | 34 | authenticate, |
34 | ensureIsAdmin, | 35 | ensureIsAdmin, |
35 | makeFriendsValidator, | 36 | makeFriendsValidator, |
36 | setBodyHostsPort, | 37 | setBodyHostsPort, |
37 | makeFriendsController | 38 | asyncMiddleware(makeFriendsController) |
38 | ) | 39 | ) |
39 | podsRouter.get('/quit-friends', | 40 | podsRouter.get('/quit-friends', |
40 | authenticate, | 41 | authenticate, |
41 | ensureIsAdmin, | 42 | ensureIsAdmin, |
42 | quitFriendsController | 43 | asyncMiddleware(quitFriendsController) |
43 | ) | 44 | ) |
44 | podsRouter.delete('/:id', | 45 | podsRouter.delete('/:id', |
45 | authenticate, | 46 | authenticate, |
46 | ensureIsAdmin, | 47 | ensureIsAdmin, |
47 | podRemoveValidator, | 48 | podRemoveValidator, |
48 | removeFriendController | 49 | asyncMiddleware(removeFriendController) |
49 | ) | 50 | ) |
50 | 51 | ||
51 | // --------------------------------------------------------------------------- | 52 | // --------------------------------------------------------------------------- |
@@ -56,33 +57,33 @@ export { | |||
56 | 57 | ||
57 | // --------------------------------------------------------------------------- | 58 | // --------------------------------------------------------------------------- |
58 | 59 | ||
59 | function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { | 60 | async function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { |
60 | db.Pod.listForApi(req.query.start, req.query.count, req.query.sort) | 61 | const resultList = await db.Pod.listForApi(req.query.start, req.query.count, req.query.sort) |
61 | .then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total))) | 62 | |
62 | .catch(err => next(err)) | 63 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
63 | } | 64 | } |
64 | 65 | ||
65 | function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { | 66 | async function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
66 | const hosts = req.body.hosts as string[] | 67 | const hosts = req.body.hosts as string[] |
67 | 68 | ||
69 | // Don't wait the process that could be long | ||
68 | makeFriends(hosts) | 70 | makeFriends(hosts) |
69 | .then(() => logger.info('Made friends!')) | 71 | .then(() => logger.info('Made friends!')) |
70 | .catch(err => logger.error('Could not make friends.', err)) | 72 | .catch(err => logger.error('Could not make friends.', err)) |
71 | 73 | ||
72 | // Don't wait the process that could be long | 74 | return res.type('json').status(204).end() |
73 | res.type('json').status(204).end() | ||
74 | } | 75 | } |
75 | 76 | ||
76 | function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { | 77 | async function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
77 | quitFriends() | 78 | await quitFriends() |
78 | .then(() => res.type('json').status(204).end()) | 79 | |
79 | .catch(err => next(err)) | 80 | return res.type('json').status(204).end() |
80 | } | 81 | } |
81 | 82 | ||
82 | function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) { | 83 | async function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) { |
83 | const pod = res.locals.pod as PodInstance | 84 | const pod = res.locals.pod as PodInstance |
84 | 85 | ||
85 | removeFriend(pod) | 86 | await removeFriend(pod) |
86 | .then(() => res.type('json').status(204).end()) | 87 | |
87 | .catch(err => next(err)) | 88 | return res.type('json').status(204).end() |
88 | } | 89 | } |