Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update parse env and docker image #56

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
update parse env and docker image
  • Loading branch information
gladwindos committed Apr 7, 2024
commit c761e93120c81b19b0cb17fda371a67836f1c69b
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["docs"]
"ignore": ["docs", "e2e"]
}
5 changes: 5 additions & 0 deletions .changeset/itchy-geckos-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gateweaver/server": patch
---

Update parse env and docker image
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ FROM base
COPY --from=build /prod/server /app
WORKDIR /app
EXPOSE 8080
CMD [ "pnpm", "start" ]
CMD [ "node", "./dist/main.js" ]
17 changes: 12 additions & 5 deletions packages/server/src/config/parse-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import fs from "fs";
import { parseConfig } from "./parse-config";
import { validateConfig } from "./validate-config";
import { InvalidConfigError, validateConfig } from "./validate-config";

jest.mock("fs");
jest.mock("./validate-config");

describe("parseConfig", () => {
const mockEnv = { TEST_VAR: "value" };

beforeEach(() => {
jest.resetAllMocks();
process.env = { ...mockEnv }; // Reset environment variables for each test
process.env.TEST_VAR = "test value";
});

it("should correctly identify and parse a file with .yaml extension", () => {
Expand Down Expand Up @@ -41,7 +39,7 @@ describe("parseConfig", () => {
it("should replace environment variables in the file content before validating", () => {
const configPath = "config";
const fullPath = `${configPath}.yml`;
const mockConfig = { endpoints: "value" };
const mockConfig = { endpoints: "test value" };

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue("endpoints: ${TEST_VAR}");
Expand All @@ -51,4 +49,13 @@ describe("parseConfig", () => {
expect(fs.readFileSync).toHaveBeenCalledWith(fullPath, "utf8");
expect(validateConfig).toHaveBeenCalledWith(mockConfig);
});

it("should throw an InvalidConfigError if an environment variable is not found", () => {
const configPath = "config";

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue("endpoints: ${MISSING_VAR}");

expect(() => parseConfig(configPath)).toThrow(InvalidConfigError);
});
});
12 changes: 10 additions & 2 deletions packages/server/src/config/parse-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import fs from "fs";
import YAML from "yaml";
import { validateConfig } from "./validate-config";
import { InvalidConfigError, validateConfig } from "./validate-config";
import { Config } from "./config.types";

export const parseEnv = (value: string): string => {
if (!process.env[value]) {
throw new InvalidConfigError(`Environment variable "${value}" not found.`);
}

return process.env[value]!;
};

export const parseConfig = (filePath: string): Config => {
const extensions = [".yml", ".yaml"];

Expand All @@ -24,7 +32,7 @@ export const parseConfig = (filePath: string): Config => {

const file = fs
.readFileSync(finalPath, "utf8")
.replace(/\$\{(.+?)\}/g, (match, name) => process.env[name] || match);
.replace(/\$\{(.+?)\}/g, (_, value) => parseEnv(value));

const config = YAML.parse(file);

Expand Down