access_lists.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import express from "express";
  2. import internalAccessList from "../../internal/access-list.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/access-lists
  15. */
  16. router
  17. .route("/")
  18. .options((_, res) => {
  19. res.sendStatus(204);
  20. })
  21. .all(jwtdecode())
  22. /**
  23. * GET /api/nginx/access-lists
  24. *
  25. * Retrieve all access-lists
  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 internalAccessList.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/access-lists
  55. *
  56. * Create a new access-list
  57. */
  58. .post(async (req, res, next) => {
  59. try {
  60. const payload = await apiValidator(getValidationSchema("/nginx/access-lists", "post"), req.body);
  61. const result = await internalAccessList.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 access-list
  70. *
  71. * /api/nginx/access-lists/123
  72. */
  73. router
  74. .route("/:list_id")
  75. .options((_, res) => {
  76. res.sendStatus(204);
  77. })
  78. .all(jwtdecode())
  79. /**
  80. * GET /api/nginx/access-lists/123
  81. *
  82. * Retrieve a specific access-list
  83. */
  84. .get(async (req, res, next) => {
  85. try {
  86. const data = await validator(
  87. {
  88. required: ["list_id"],
  89. additionalProperties: false,
  90. properties: {
  91. list_id: {
  92. $ref: "common#/properties/id",
  93. },
  94. expand: {
  95. $ref: "common#/properties/expand",
  96. },
  97. },
  98. },
  99. {
  100. list_id: req.params.list_id,
  101. expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
  102. },
  103. );
  104. const row = await internalAccessList.get(res.locals.access, {
  105. id: Number.parseInt(data.list_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/access-lists/123
  116. *
  117. * Update and existing access-list
  118. */
  119. .put(async (req, res, next) => {
  120. try {
  121. const payload = await apiValidator(getValidationSchema("/nginx/access-lists/{listID}", "put"), req.body);
  122. payload.id = Number.parseInt(req.params.list_id, 10);
  123. const result = await internalAccessList.update(res.locals.access, payload);
  124. res.status(200).send(result);
  125. } catch (err) {
  126. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  127. next(err);
  128. }
  129. })
  130. /**
  131. * DELETE /api/nginx/access-lists/123
  132. *
  133. * Delete and existing access-list
  134. */
  135. .delete(async (req, res, next) => {
  136. try {
  137. const result = await internalAccessList.delete(res.locals.access, {
  138. id: Number.parseInt(req.params.list_id, 10),
  139. });
  140. res.status(200).send(result);
  141. } catch (err) {
  142. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  143. next(err);
  144. }
  145. });
  146. export default router;