diff --git a/packages/backend-core/src/environment.ts b/packages/backend-core/src/environment.ts index ed882fe96a..138dbbd9e0 100644 --- a/packages/backend-core/src/environment.ts +++ b/packages/backend-core/src/environment.ts @@ -107,6 +107,7 @@ const environment = { ENCRYPTION_KEY: process.env.ENCRYPTION_KEY, API_ENCRYPTION_KEY: getAPIEncryptionKey(), COUCH_DB_URL: process.env.COUCH_DB_URL || "http://localhost:4005", + COUCH_DB_SQL_URL: process.env.COUCH_DB_SQL_URL || "http://localhost:4984", COUCH_DB_USERNAME: process.env.COUCH_DB_USER, COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID, diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 51b5fda3d4..b5810b9ba3 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -17,7 +17,6 @@ import { basicWebhook, } from "./structures" import { - auth, cache, constants, context, diff --git a/packages/worker/src/api/controllers/system/environment.ts b/packages/worker/src/api/controllers/system/environment.ts index ade5f241e2..bf9270607f 100644 --- a/packages/worker/src/api/controllers/system/environment.ts +++ b/packages/worker/src/api/controllers/system/environment.ts @@ -1,6 +1,25 @@ import { Ctx } from "@budibase/types" import env from "../../../environment" import { env as coreEnv } from "@budibase/backend-core" +import nodeFetch from "node-fetch" + +let sqsAvailable: boolean +async function isSqsAvailable() { + if (sqsAvailable !== undefined) { + return sqsAvailable + } + + try { + await nodeFetch(coreEnv.COUCH_DB_SQL_URL, { + timeout: 1000, + }) + sqsAvailable = true + return true + } catch (e) { + sqsAvailable = false + return false + } +} export const fetch = async (ctx: Ctx) => { ctx.body = { @@ -12,4 +31,10 @@ export const fetch = async (ctx: Ctx) => { baseUrl: env.PLATFORM_URL, isDev: env.isDev() && !env.isTest(), } + + if (env.SELF_HOSTED) { + ctx.body.infrastructure = { + sqs: await isSqsAvailable(), + } + } } diff --git a/packages/worker/src/api/routes/system/tests/environment.spec.ts b/packages/worker/src/api/routes/system/tests/environment.spec.ts index 897cc970cc..2efbfa07c9 100644 --- a/packages/worker/src/api/routes/system/tests/environment.spec.ts +++ b/packages/worker/src/api/routes/system/tests/environment.spec.ts @@ -1,5 +1,7 @@ import { TestConfiguration } from "../../../../tests" +jest.unmock("node-fetch") + describe("/api/system/environment", () => { const config = new TestConfiguration() @@ -27,5 +29,22 @@ describe("/api/system/environment", () => { offlineMode: false, }) }) + + it("returns the expected environment for self hosters", async () => { + await config.withEnv({ SELF_HOSTED: true }, async () => { + const env = await config.api.environment.getEnvironment() + expect(env.body).toEqual({ + cloud: false, + disableAccountPortal: 0, + isDev: false, + multiTenancy: true, + baseUrl: "http://localhost:10000", + offlineMode: false, + infrastructure: { + sqs: false, + }, + }) + }) + }) }) }) diff --git a/packages/worker/src/tests/TestConfiguration.ts b/packages/worker/src/tests/TestConfiguration.ts index c43d1b9d13..8e163f0373 100644 --- a/packages/worker/src/tests/TestConfiguration.ts +++ b/packages/worker/src/tests/TestConfiguration.ts @@ -36,6 +36,7 @@ import { } from "@budibase/types" import API from "./api" import jwt, { Secret } from "jsonwebtoken" +import cloneDeep from "lodash/fp/cloneDeep" class TestConfiguration { server: any @@ -240,6 +241,34 @@ class TestConfiguration { return { message: "Admin user only endpoint.", status: 403 } } + async withEnv(newEnvVars: Partial, f: () => Promise) { + let cleanup = this.setEnv(newEnvVars) + try { + await f() + } finally { + cleanup() + } + } + + /* + * Sets the environment variables to the given values and returns a function + * that can be called to reset the environment variables to their original values. + */ + setEnv(newEnvVars: Partial): () => void { + const oldEnv = cloneDeep(env) + + let key: keyof typeof newEnvVars + for (key in newEnvVars) { + env._set(key, newEnvVars[key]) + } + + return () => { + for (const [key, value] of Object.entries(oldEnv)) { + env._set(key, value) + } + } + } + // USERS async createDefaultUser() {