proxy_hosts.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import express from "express";
  2. import internalProxyHost from "../../internal/proxy-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/proxy-hosts
  15. */
  16. router
  17. .route("/")
  18. .options((_, res) => {
  19. res.sendStatus(204);
  20. })
  21. .all(jwtdecode())
  22. /**
  23. * GET /api/nginx/proxy-hosts
  24. *
  25. * Retrieve all proxy-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 internalProxyHost.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/proxy-hosts
  55. *
  56. * Create a new proxy-host
  57. */
  58. .post(async (req, res, next) => {
  59. try {
  60. const payload = await apiValidator(getValidationSchema("/nginx/proxy-hosts", "post"), req.body);
  61. const result = await internalProxyHost.create(res.locals.access, payload);
  62. res.status(201).send(result);
  63. } catch (err) {
  64. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err} ${JSON.stringify(err.debug, null, 2)}`);
  65. next(err);
  66. }
  67. });
  68. /**
  69. * Specific proxy-host
  70. *
  71. * /api/nginx/proxy-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/proxy-hosts/123
  81. *
  82. * Retrieve a specific proxy-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 internalProxyHost.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/proxy-hosts/123
  116. *
  117. * Update and existing proxy-host
  118. */
  119. .put(async (req, res, next) => {
  120. try {
  121. const payload = await apiValidator(getValidationSchema("/nginx/proxy-hosts/{hostID}", "put"), req.body);
  122. payload.id = Number.parseInt(req.params.host_id, 10);
  123. const result = await internalProxyHost.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/proxy-hosts/123
  132. *
  133. * Update and existing proxy-host
  134. */
  135. .delete(async (req, res, next) => {
  136. try {
  137. const result = await internalProxyHost.delete(res.locals.access, {
  138. id: Number.parseInt(req.params.host_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. /**
  147. * Enable proxy-host
  148. *
  149. * /api/nginx/proxy-hosts/123/enable
  150. */
  151. router
  152. .route("/:host_id/enable")
  153. .options((_, res) => {
  154. res.sendStatus(204);
  155. })
  156. .all(jwtdecode())
  157. /**
  158. * POST /api/nginx/proxy-hosts/123/enable
  159. */
  160. .post(async (req, res, next) => {
  161. try {
  162. const result = await internalProxyHost.enable(res.locals.access, {
  163. id: Number.parseInt(req.params.host_id, 10),
  164. });
  165. res.status(200).send(result);
  166. } catch (err) {
  167. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  168. next(err);
  169. }
  170. });
  171. /**
  172. * Disable proxy-host
  173. *
  174. * /api/nginx/proxy-hosts/123/disable
  175. */
  176. router
  177. .route("/:host_id/disable")
  178. .options((_, res) => {
  179. res.sendStatus(204);
  180. })
  181. .all(jwtdecode())
  182. /**
  183. * POST /api/nginx/proxy-hosts/123/disable
  184. */
  185. .post(async (req, res, next) => {
  186. try {
  187. const result = await internalProxyHost.disable(res.locals.access, {
  188. id: Number.parseInt(req.params.host_id, 10),
  189. });
  190. res.status(200).send(result);
  191. } catch (err) {
  192. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  193. next(err);
  194. }
  195. });
  196. export default router;