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