aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/controllers/api/server/redundancy.ts
blob: 4140c49912595c242c17c3494644137ee9cfd2c4 (plain) (tree)
1
2
3
4
5
6
7




                                                                                            

                                                            
























                                                                                                           



                                                                                    

                            
import * as express from 'express'
import { UserRight } from '../../../../shared/models/users'
import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares'
import { updateServerRedundancyValidator } from '../../../middlewares/validators/redundancy'
import { ServerModel } from '../../../models/server/server'
import { removeRedundancyOf } from '../../../lib/redundancy'
import { logger } from '../../../helpers/logger'

const serverRedundancyRouter = express.Router()

serverRedundancyRouter.put('/redundancy/:host',
  authenticate,
  ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
  asyncMiddleware(updateServerRedundancyValidator),
  asyncMiddleware(updateRedundancy)
)

// ---------------------------------------------------------------------------

export {
  serverRedundancyRouter
}

// ---------------------------------------------------------------------------

async function updateRedundancy (req: express.Request, res: express.Response, next: express.NextFunction) {
  const server = res.locals.server as ServerModel

  server.redundancyAllowed = req.body.redundancyAllowed

  await server.save()

  // Async, could be long
  removeRedundancyOf(server.id)
    .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, err))

  return res.sendStatus(204)
}