ssl_passthrough_hosts.js 4.4 KB

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