Skip to content

Commit

Permalink
replace config vars with env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
gladwindos committed Feb 10, 2024
1 parent cf8f67a commit 1691633
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ endpoints:
- key: "Content-Type"
value: "application/json"
- key: "Authorization"
value: "Bearer 1234567890"
value: "Bearer ${AUTH_TOKEN}"
- name: "todos-post"
path: "/todos"
method: "POST"
Expand All @@ -18,7 +18,7 @@ endpoints:
- key: "Content-Type"
value: "application/json"
- key: "Authorization"
value: "Bearer 1234567890"
value: "Bearer ${AUTH_TOKEN}"
- name: "posts"
path: "/posts"
method: "GET"
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import "dotenv/config";
import express from "express";
import { parseConfig } from "./utils/config/parse-config";

const app = express();

const PORT = process.env.PORT || 6060;

app.get("/", (req, res) => {
res.send("Hello World");
const config = parseConfig("config.yml");

res.send(config);
});

app.listen(PORT, () => {
Expand Down
7 changes: 5 additions & 2 deletions src/utils/config/parse-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { Config } from "../../types/config";
import { validateConfig } from "./validate-config";

export const parseConfig = (filePath: string): Config => {
const file = fs.readFileSync(filePath, "utf8");
const config = YAML.parse(file);
const fileContents = fs
.readFileSync(filePath, "utf8")
.replace(/\$\{(.+?)\}/g, (match, name) => process.env[name] || match);

const config = YAML.parse(fileContents);

const valid = validateConfig(config);

Expand Down

0 comments on commit 1691633

Please sign in to comment.