ConfigApi.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright (C) 2019 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import express from "express";
  15. import path from "path";
  16. import fs from "fs";
  17. import requireWithoutCache from "require-without-cache";
  18. import type { Services } from "@src/@types/Config";
  19. const getBaseUrl = () => {
  20. const BASE_URL = process.env.CORIOLIS_URL || "";
  21. return BASE_URL.trim().replace(/\/$/, "");
  22. };
  23. const modServicesUrls = (
  24. configServices: Services,
  25. servicesMod?: Services
  26. ): Services => {
  27. const services = { ...configServices };
  28. Object.keys(services).forEach(key => {
  29. const typedKey = key as keyof Services;
  30. services[typedKey] = (
  31. servicesMod && servicesMod[typedKey]
  32. ? servicesMod[typedKey]
  33. : services[typedKey]
  34. ).replace("{BASE_URL}", getBaseUrl());
  35. });
  36. return services;
  37. };
  38. const NOT_FIRST_LAUNCH_PATH = path.join(__dirname, "../../.not-first-launch");
  39. export default (router: express.Router) => {
  40. router
  41. .get("/config", (_, res) => {
  42. const configPath = path.join(__dirname, "../../config.ts");
  43. const isFirstLaunch = !fs.existsSync(NOT_FIRST_LAUNCH_PATH);
  44. const config: any = requireWithoutCache(configPath, require).config;
  45. const modJsonPath: string | null | undefined = process.env.MOD_JSON;
  46. if (!modJsonPath) {
  47. config.servicesUrls = modServicesUrls(config.servicesUrls);
  48. res.send({ config, isFirstLaunch });
  49. return;
  50. }
  51. try {
  52. const jsonContent: any = fs.readFileSync(modJsonPath);
  53. const configMod = JSON.parse(jsonContent).config;
  54. Object.keys(configMod).forEach(key => {
  55. if (key !== "servicesUrls") {
  56. config[key] = configMod[key];
  57. }
  58. });
  59. config.servicesUrls = modServicesUrls(
  60. config.servicesUrls,
  61. configMod.servicesUrls
  62. );
  63. res.send({ config, isFirstLaunch });
  64. } catch (err) {
  65. console.error(err);
  66. res.status(400).json({ error: { message: "Invalid MOD_JSON file" } });
  67. }
  68. })
  69. .post("/config/first-launch", (req, res) => {
  70. const { isFirstLaunch } = req.body;
  71. if (isFirstLaunch !== false) {
  72. res.status(422).json({
  73. error: { message: "'isFirstLaunch' property not set to 'false'" },
  74. });
  75. return;
  76. }
  77. fs.writeFileSync(NOT_FIRST_LAUNCH_PATH, "");
  78. res.json({ isFirstLaunch });
  79. });
  80. };