]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
34831b48 | 2 | |
e02643f3 | 3 | import { database as db } from '../../../initializers/database' |
8a02bd04 C |
4 | import { |
5 | checkSignature, | |
6 | signatureValidator, | |
7 | setBodyHostPort, | |
8 | remotePodsAddValidator | |
9 | } from '../../../middlewares' | |
72c7248b | 10 | import { sendOwnedDataToPod } from '../../../lib' |
8a02bd04 C |
11 | import { getMyPublicCert, getFormattedObjects } from '../../../helpers' |
12 | import { CONFIG } from '../../../initializers' | |
13 | import { PodInstance } from '../../../models' | |
14 | import { PodSignature, Pod as FormattedPod } from '../../../../shared' | |
34831b48 | 15 | |
65fcc311 | 16 | const remotePodsRouter = express.Router() |
34831b48 | 17 | |
65fcc311 C |
18 | remotePodsRouter.post('/remove', |
19 | signatureValidator, | |
34831b48 C |
20 | checkSignature, |
21 | removePods | |
22 | ) | |
23 | ||
8a02bd04 C |
24 | remotePodsRouter.post('/list', remotePodsList) |
25 | ||
26 | remotePodsRouter.post('/add', | |
27 | setBodyHostPort, // We need to modify the host before running the validator! | |
28 | remotePodsAddValidator, | |
29 | addPods | |
30 | ) | |
31 | ||
34831b48 C |
32 | // --------------------------------------------------------------------------- |
33 | ||
65fcc311 C |
34 | export { |
35 | remotePodsRouter | |
36 | } | |
34831b48 C |
37 | |
38 | // --------------------------------------------------------------------------- | |
39 | ||
8a02bd04 C |
40 | function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { |
41 | const information = req.body | |
42 | ||
43 | const pod = db.Pod.build(information) | |
44 | pod.save() | |
45 | .then(podCreated => { | |
72c7248b | 46 | return sendOwnedDataToPod(podCreated.id) |
8a02bd04 C |
47 | }) |
48 | .then(() => { | |
49 | return getMyPublicCert() | |
50 | }) | |
51 | .then(cert => { | |
52 | return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL }) | |
53 | }) | |
54 | .catch(err => next(err)) | |
55 | } | |
56 | ||
57 | function remotePodsList (req: express.Request, res: express.Response, next: express.NextFunction) { | |
58 | db.Pod.list() | |
59 | .then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length))) | |
60 | .catch(err => next(err)) | |
61 | } | |
62 | ||
69818c93 | 63 | function removePods (req: express.Request, res: express.Response, next: express.NextFunction) { |
4771e000 C |
64 | const signature: PodSignature = req.body.signature |
65 | const host = signature.host | |
34831b48 | 66 | |
6fcd19ba | 67 | db.Pod.loadByHost(host) |
4771e000 | 68 | .then(pod => pod.destroy()) |
6fcd19ba C |
69 | .then(() => res.type('json').status(204).end()) |
70 | .catch(err => next(err)) | |
34831b48 | 71 | } |