2017-06-05 21:53:49 +02:00
|
|
|
import * as express from 'express'
|
2017-03-19 18:56:10 +01:00
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
import { database as db } from '../../../initializers/database'
|
2017-10-19 09:43:01 +02:00
|
|
|
import {
|
|
|
|
checkSignature,
|
|
|
|
signatureValidator,
|
|
|
|
setBodyHostPort,
|
|
|
|
remotePodsAddValidator
|
|
|
|
} from '../../../middlewares'
|
2017-10-24 19:41:09 +02:00
|
|
|
import { sendOwnedDataToPod } from '../../../lib'
|
2017-10-19 09:43:01 +02:00
|
|
|
import { getMyPublicCert, getFormattedObjects } from '../../../helpers'
|
|
|
|
import { CONFIG } from '../../../initializers'
|
|
|
|
import { PodInstance } from '../../../models'
|
|
|
|
import { PodSignature, Pod as FormattedPod } from '../../../../shared'
|
2017-03-19 18:56:10 +01:00
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
const remotePodsRouter = express.Router()
|
2017-03-19 18:56:10 +01:00
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
remotePodsRouter.post('/remove',
|
|
|
|
signatureValidator,
|
2017-03-19 18:56:10 +01:00
|
|
|
checkSignature,
|
|
|
|
removePods
|
|
|
|
)
|
|
|
|
|
2017-10-19 09:43:01 +02:00
|
|
|
remotePodsRouter.post('/list', remotePodsList)
|
|
|
|
|
|
|
|
remotePodsRouter.post('/add',
|
|
|
|
setBodyHostPort, // We need to modify the host before running the validator!
|
|
|
|
remotePodsAddValidator,
|
|
|
|
addPods
|
|
|
|
)
|
|
|
|
|
2017-03-19 18:56:10 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
remotePodsRouter
|
|
|
|
}
|
2017-03-19 18:56:10 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-19 09:43:01 +02:00
|
|
|
function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const information = req.body
|
|
|
|
|
|
|
|
const pod = db.Pod.build(information)
|
|
|
|
pod.save()
|
|
|
|
.then(podCreated => {
|
2017-10-24 19:41:09 +02:00
|
|
|
return sendOwnedDataToPod(podCreated.id)
|
2017-10-19 09:43:01 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return getMyPublicCert()
|
|
|
|
})
|
|
|
|
.then(cert => {
|
|
|
|
return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
|
|
|
|
})
|
|
|
|
.catch(err => next(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
function remotePodsList (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
db.Pod.list()
|
|
|
|
.then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length)))
|
|
|
|
.catch(err => next(err))
|
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function removePods (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-10 19:43:21 +02:00
|
|
|
const signature: PodSignature = req.body.signature
|
|
|
|
const host = signature.host
|
2017-03-19 18:56:10 +01:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
db.Pod.loadByHost(host)
|
2017-07-10 19:43:21 +02:00
|
|
|
.then(pod => pod.destroy())
|
2017-07-05 13:26:25 +02:00
|
|
|
.then(() => res.type('json').status(204).end())
|
|
|
|
.catch(err => next(err))
|
2017-03-19 18:56:10 +01:00
|
|
|
}
|