redirection_hosts.js 4.9 KB

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