dead_hosts.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. const express = require('express');
  2. const validator = require('../../lib/validator');
  3. const jwtdecode = require('../../lib/express/jwt-decode');
  4. const apiValidator = require('../../lib/validator/api');
  5. const internalDeadHost = require('../../internal/dead-host');
  6. const schema = require('../../schema');
  7. let router = express.Router({
  8. caseSensitive: true,
  9. strict: true,
  10. mergeParams: true
  11. });
  12. /**
  13. * /api/nginx/dead-hosts
  14. */
  15. router
  16. .route('/')
  17. .options((_, res) => {
  18. res.sendStatus(204);
  19. })
  20. .all(jwtdecode())
  21. /**
  22. * GET /api/nginx/dead-hosts
  23. *
  24. * Retrieve all dead-hosts
  25. */
  26. .get((req, res, next) => {
  27. validator({
  28. additionalProperties: false,
  29. properties: {
  30. expand: {
  31. $ref: 'common#/properties/expand'
  32. },
  33. query: {
  34. $ref: 'common#/properties/query'
  35. }
  36. }
  37. }, {
  38. expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null),
  39. query: (typeof req.query.query === 'string' ? req.query.query : null)
  40. })
  41. .then((data) => {
  42. return internalDeadHost.getAll(res.locals.access, data.expand, data.query);
  43. })
  44. .then((rows) => {
  45. res.status(200)
  46. .send(rows);
  47. })
  48. .catch(next);
  49. })
  50. /**
  51. * POST /api/nginx/dead-hosts
  52. *
  53. * Create a new dead-host
  54. */
  55. .post((req, res, next) => {
  56. apiValidator(schema.getValidationSchema('/nginx/dead-hosts', 'post'), req.body)
  57. .then((payload) => {
  58. return internalDeadHost.create(res.locals.access, payload);
  59. })
  60. .then((result) => {
  61. res.status(201)
  62. .send(result);
  63. })
  64. .catch(next);
  65. });
  66. /**
  67. * Specific dead-host
  68. *
  69. * /api/nginx/dead-hosts/123
  70. */
  71. router
  72. .route('/:host_id')
  73. .options((req, res) => {
  74. res.sendStatus(204);
  75. })
  76. .all(jwtdecode())
  77. /**
  78. * GET /api/nginx/dead-hosts/123
  79. *
  80. * Retrieve a specific dead-host
  81. */
  82. .get((req, res, next) => {
  83. validator({
  84. required: ['host_id'],
  85. additionalProperties: false,
  86. properties: {
  87. host_id: {
  88. $ref: 'common#/properties/id'
  89. },
  90. expand: {
  91. $ref: 'common#/properties/expand'
  92. }
  93. }
  94. }, {
  95. host_id: req.params.host_id,
  96. expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null)
  97. })
  98. .then((data) => {
  99. return internalDeadHost.get(res.locals.access, {
  100. id: parseInt(data.host_id, 10),
  101. expand: data.expand
  102. });
  103. })
  104. .then((row) => {
  105. res.status(200)
  106. .send(row);
  107. })
  108. .catch(next);
  109. })
  110. /**
  111. * PUT /api/nginx/dead-hosts/123
  112. *
  113. * Update and existing dead-host
  114. */
  115. .put((req, res, next) => {
  116. apiValidator(schema.getValidationSchema('/nginx/dead-hosts/{hostID}', 'put'), req.body)
  117. .then((payload) => {
  118. payload.id = parseInt(req.params.host_id, 10);
  119. return internalDeadHost.update(res.locals.access, payload);
  120. })
  121. .then((result) => {
  122. res.status(200)
  123. .send(result);
  124. })
  125. .catch(next);
  126. })
  127. /**
  128. * DELETE /api/nginx/dead-hosts/123
  129. *
  130. * Update and existing dead-host
  131. */
  132. .delete((req, res, next) => {
  133. internalDeadHost.delete(res.locals.access, {id: parseInt(req.params.host_id, 10)})
  134. .then((result) => {
  135. res.status(200)
  136. .send(result);
  137. })
  138. .catch(next);
  139. });
  140. /**
  141. * Enable dead-host
  142. *
  143. * /api/nginx/dead-hosts/123/enable
  144. */
  145. router
  146. .route('/:host_id/enable')
  147. .options((_, res) => {
  148. res.sendStatus(204);
  149. })
  150. .all(jwtdecode())
  151. /**
  152. * POST /api/nginx/dead-hosts/123/enable
  153. */
  154. .post((req, res, next) => {
  155. internalDeadHost.enable(res.locals.access, {id: parseInt(req.params.host_id, 10)})
  156. .then((result) => {
  157. res.status(200)
  158. .send(result);
  159. })
  160. .catch(next);
  161. });
  162. /**
  163. * Disable dead-host
  164. *
  165. * /api/nginx/dead-hosts/123/disable
  166. */
  167. router
  168. .route('/:host_id/disable')
  169. .options((_, res) => {
  170. res.sendStatus(204);
  171. })
  172. .all(jwtdecode())
  173. /**
  174. * POST /api/nginx/dead-hosts/123/disable
  175. */
  176. .post((req, res, next) => {
  177. internalDeadHost.disable(res.locals.access, {id: parseInt(req.params.host_id, 10)})
  178. .then((result) => {
  179. res.status(200)
  180. .send(result);
  181. })
  182. .catch(next);
  183. });
  184. module.exports = router;