aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/remote/pods.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-25 11:55:06 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commiteb08047657e739bcd9e592d76307befa3998482b (patch)
treefc309f51ece792fd4307c4af510710a853e1d6b2 /server/controllers/api/remote/pods.ts
parent5f04dd2f743961e0a06c29531cc3ccc9e4928d56 (diff)
downloadPeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.gz
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.zst
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.zip
Use async/await in controllers
Diffstat (limited to 'server/controllers/api/remote/pods.ts')
-rw-r--r--server/controllers/api/remote/pods.ts48
1 files changed, 23 insertions, 25 deletions
diff --git a/server/controllers/api/remote/pods.ts b/server/controllers/api/remote/pods.ts
index a62b9c684..326eb61ac 100644
--- a/server/controllers/api/remote/pods.ts
+++ b/server/controllers/api/remote/pods.ts
@@ -5,7 +5,8 @@ import {
5 checkSignature, 5 checkSignature,
6 signatureValidator, 6 signatureValidator,
7 setBodyHostPort, 7 setBodyHostPort,
8 remotePodsAddValidator 8 remotePodsAddValidator,
9 asyncMiddleware
9} from '../../../middlewares' 10} from '../../../middlewares'
10import { sendOwnedDataToPod } from '../../../lib' 11import { sendOwnedDataToPod } from '../../../lib'
11import { getMyPublicCert, getFormattedObjects } from '../../../helpers' 12import { getMyPublicCert, getFormattedObjects } from '../../../helpers'
@@ -18,15 +19,17 @@ const remotePodsRouter = express.Router()
18remotePodsRouter.post('/remove', 19remotePodsRouter.post('/remove',
19 signatureValidator, 20 signatureValidator,
20 checkSignature, 21 checkSignature,
21 removePods 22 asyncMiddleware(removePods)
22) 23)
23 24
24remotePodsRouter.post('/list', remotePodsList) 25remotePodsRouter.post('/list',
26 asyncMiddleware(remotePodsList)
27)
25 28
26remotePodsRouter.post('/add', 29remotePodsRouter.post('/add',
27 setBodyHostPort, // We need to modify the host before running the validator! 30 setBodyHostPort, // We need to modify the host before running the validator!
28 remotePodsAddValidator, 31 remotePodsAddValidator,
29 addPods 32 asyncMiddleware(addPods)
30) 33)
31 34
32// --------------------------------------------------------------------------- 35// ---------------------------------------------------------------------------
@@ -37,35 +40,30 @@ export {
37 40
38// --------------------------------------------------------------------------- 41// ---------------------------------------------------------------------------
39 42
40function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { 43async function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
41 const information = req.body 44 const information = req.body
42 45
43 const pod = db.Pod.build(information) 46 const pod = db.Pod.build(information)
44 pod.save() 47 const podCreated = await pod.save()
45 .then(podCreated => { 48
46 return sendOwnedDataToPod(podCreated.id) 49 await sendOwnedDataToPod(podCreated.id)
47 }) 50
48 .then(() => { 51 const cert = await getMyPublicCert()
49 return getMyPublicCert() 52 return res.json({ cert, email: CONFIG.ADMIN.EMAIL })
50 })
51 .then(cert => {
52 return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
53 })
54 .catch(err => next(err))
55} 53}
56 54
57function remotePodsList (req: express.Request, res: express.Response, next: express.NextFunction) { 55async function remotePodsList (req: express.Request, res: express.Response, next: express.NextFunction) {
58 db.Pod.list() 56 const pods = await db.Pod.list()
59 .then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length))) 57
60 .catch(err => next(err)) 58 return res.json(getFormattedObjects<FormattedPod, PodInstance>(pods, pods.length))
61} 59}
62 60
63function removePods (req: express.Request, res: express.Response, next: express.NextFunction) { 61async function removePods (req: express.Request, res: express.Response, next: express.NextFunction) {
64 const signature: PodSignature = req.body.signature 62 const signature: PodSignature = req.body.signature
65 const host = signature.host 63 const host = signature.host
66 64
67 db.Pod.loadByHost(host) 65 const pod = await db.Pod.loadByHost(host)
68 .then(pod => pod.destroy()) 66 await pod.destroy()
69 .then(() => res.type('json').status(204).end()) 67
70 .catch(err => next(err)) 68 return res.type('json').status(204).end()
71} 69}