copyInConfig.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { db } from "@server/db";
  2. import { exitNodes, orgs, resources } from "../db/schema";
  3. import config from "@server/lib/config";
  4. import { eq, ne } from "drizzle-orm";
  5. import logger from "@server/logger";
  6. export async function copyInConfig() {
  7. const domain = config.getBaseDomain();
  8. const endpoint = config.getRawConfig().gerbil.base_endpoint;
  9. const listenPort = config.getRawConfig().gerbil.start_port;
  10. // update the domain on all of the orgs where the domain is not equal to the new domain
  11. // TODO: eventually each org could have a unique domain that we do not want to overwrite, so this will be unnecessary
  12. await db.update(orgs).set({ domain }).where(ne(orgs.domain, domain));
  13. // TODO: eventually each exit node could have a different endpoint
  14. await db.update(exitNodes).set({ endpoint }).where(ne(exitNodes.endpoint, endpoint));
  15. // TODO: eventually each exit node could have a different port
  16. await db.update(exitNodes).set({ listenPort }).where(ne(exitNodes.listenPort, listenPort));
  17. // update all resources fullDomain to use the new domain
  18. await db.transaction(async (trx) => {
  19. const allResources = await trx.select().from(resources);
  20. for (const resource of allResources) {
  21. let fullDomain = "";
  22. if (resource.isBaseDomain) {
  23. fullDomain = domain;
  24. } else {
  25. fullDomain = `${resource.subdomain}.${domain}`;
  26. }
  27. await trx
  28. .update(resources)
  29. .set({ fullDomain })
  30. .where(eq(resources.resourceId, resource.resourceId));
  31. }
  32. });
  33. logger.info(`Updated orgs with new domain (${domain})`);
  34. }