]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/remote/pods.ts
Fix spelling (#126)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / remote / pods.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
34831b48 2
e02643f3 3import { database as db } from '../../../initializers/database'
8a02bd04
C
4import {
5 checkSignature,
6 signatureValidator,
7 setBodyHostPort,
eb080476
C
8 remotePodsAddValidator,
9 asyncMiddleware
8a02bd04 10} from '../../../middlewares'
72c7248b 11import { sendOwnedDataToPod } from '../../../lib'
8a02bd04
C
12import { getMyPublicCert, getFormattedObjects } from '../../../helpers'
13import { CONFIG } from '../../../initializers'
14import { PodInstance } from '../../../models'
15import { PodSignature, Pod as FormattedPod } from '../../../../shared'
34831b48 16
65fcc311 17const remotePodsRouter = express.Router()
34831b48 18
65fcc311
C
19remotePodsRouter.post('/remove',
20 signatureValidator,
34831b48 21 checkSignature,
eb080476 22 asyncMiddleware(removePods)
34831b48
C
23)
24
eb080476
C
25remotePodsRouter.post('/list',
26 asyncMiddleware(remotePodsList)
27)
8a02bd04
C
28
29remotePodsRouter.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
37export {
38 remotePodsRouter
39}
34831b48
C
40
41// ---------------------------------------------------------------------------
42
eb080476 43async 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
55async 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 61async 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}