Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: config & env setup.
  • Loading branch information
hanoak20 committed Jan 2, 2024
commit a9db43bc20aa5ff9e851650d490fc5a02dff46e1
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Config file
config*.json
dist/
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
Expand Down Expand Up @@ -351,4 +349,5 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
.dccache
.dccache
*.env
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npx tsc && node dist/server.js --env=prod",
"dev": "ts-node-dev --respawn ./src/server.ts --env=dev",
"start": "npx tsc && NODE_ENV=prod node dist/server.js",
"dev": "NODE_ENV=dev ts-node-dev --respawn ./src/server.ts",
"prettify": "prettier --write .",
"lint:fix": "eslint src/**/*.ts",
"precommit": "npm run prettify && npm run lint:fix"
Expand Down
1 change: 0 additions & 1 deletion src/config.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/config/dev.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const devConfig = {
CS_API: {
US: "https://stag-api.csnonprod.com/v3",
EU: "https://stag-eu-api.csnonprod.com/v3",
AZURE_NA: "https://stag-azure-na-api.csnonprod.com/v3",
},
};
23 changes: 23 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import dotenv from "dotenv";
import path from "path";
import { prodConfig } from "./prod.config";
import { devConfig } from "./dev.config";

dotenv.config({
path: path.resolve(process.cwd(), `${process.env.NODE_ENV}.env`),
});

export type ConfigType = {
PORT: string;
CS_API: {
US: string;
EU: string;
AZURE_NA: string;
AZURE_EU?: string;
};
};

export const config: ConfigType = {
PORT: process.env.PORT!,
...(process.env.NODE_ENV === "prod" ? prodConfig : devConfig),
};
8 changes: 8 additions & 0 deletions src/config/prod.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const prodConfig = {
CS_API: {
US: "https://api.contentstack.io/v3",
EU: "https://eu-api.contentstack.com/v3",
AZURE_NA: "https://azure-na-api.contentstack.com/v3",
AZURE_EU: "https://azure-eu-api.contentstack.com/v3",
},
};
31 changes: 0 additions & 31 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
let env: string;
let config: ConfigType = {};

export type ConfigType = {
config?: string;
};

export type ConfigFilePathType = {
dev: string;
stage: string;
prod: string;
};
export type HttpErrorCodes = {
HTTP_OK: number;
FORBIDDEN: number;
Expand All @@ -20,13 +8,6 @@ export type HttpErrorCodes = {
MOVED_PERMANENTLY: number;
};
export type ConstantType = {
NODE_CLI_ENV_KEY: string;
NODE_CLI_INVALID_ENV: string;
CONFIG_FILE_PATHS: ConfigFilePathType;
setEnv: (e: string) => void;
getEnv: () => string;
setConfig: (config: ConfigType) => void;
getConfig: (key: string) => unknown;
HTTP_ERROR_CODES: HttpErrorCodes;
HTTP_ERROR_TEXTS: HttpErrorTexts;
HTTP_RESPONSE_HEADERS: HttpResponseHeaders;
Expand All @@ -45,18 +26,6 @@ export type HttpResponseHeaders = {
};

export const constants: ConstantType = {
NODE_CLI_ENV_KEY: "--env",
NODE_CLI_INVALID_ENV:
"Please provide a valid cli argument for --env when starting the node server",
CONFIG_FILE_PATHS: {
dev: "../config-dev.json",
stage: "../config-stage.json",
prod: "../config.json",
},
setEnv: (e: string) => (env = e),
getEnv: () => env,
setConfig: (c: ConfigType) => (config = c),
getConfig: (key: string) => (key ? config[key as keyof ConfigType] : config),
HTTP_ERROR_CODES: {
HTTP_OK: 200,
FORBIDDEN: 403,
Expand Down
11 changes: 3 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { parseCLIArgsFromProcess, loadConfigFile } from "./utils";
import { config } from "./config";
import { constants } from "./constants";
import express, { NextFunction, Request, Response } from "express";
import cors from "cors";
import dotenv from "dotenv";
import helmet from "helmet";
const PORT = process.env.PORT ?? 5000;
import authRoutes from "./routes/auth.routes";
import projectRoutes from "./routes/projects.routes";

try {
loadConfigFile(parseCLIArgsFromProcess(process.argv));
dotenv.config();

const app = express();
app.use(
helmet({
Expand Down Expand Up @@ -57,8 +52,8 @@ try {
});
});

app.listen(PORT, () => {
console.info(`Server listening at port ${PORT}`);
app.listen(config.PORT, () => {
console.info(`Server listening at port ${config.PORT}`);
});
} catch (e) {
console.error("Error while starting the server!");
Expand Down
4 changes: 2 additions & 2 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request } from "express";
import axios from "axios";
import { API_MGMT_URL } from "../config";
import { config } from "../config";
import { generateToken } from "../utils/jwt.utils";
import * as fs from "fs/promises";
import { MigrationPayload, ResponseType } from "../models/types";
Expand All @@ -12,7 +12,7 @@ const createUserService = () => {
try {
const apiResponse = await axios({
method: "POST",
url: API_MGMT_URL,
url: config.CS_API.US,
headers: {
"Content-Type": "application/json",
},
Expand Down
22 changes: 0 additions & 22 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { constants, ConfigType } from "../constants";

export const throwError = (message: string, statusCode: number) => {
throw Object.assign(new Error(message), { statusCode });
};
Expand All @@ -9,23 +7,3 @@ export const isEmpty = (val: unknown) =>
val === null ||
(typeof val === "object" && !Object.keys(val).length) ||
(typeof val === "string" && !val.trim().length);

export const parseCLIArgsFromProcess = (argv: string[] = []) => {
if (argv?.length < 2) return {};
const parsedArgs: { [key: string]: string } = {};
(argv?.slice(2) || []).forEach((oneArg: string = "") => {
const keyAndValue = oneArg?.split("=");
parsedArgs[keyAndValue[0]] = keyAndValue[1];
});
return parsedArgs;
};

export const loadConfigFile = (cliArgs: { [key: string]: string } = {}) => {
console.info(cliArgs);
constants.setEnv(cliArgs[constants.NODE_CLI_ENV_KEY]);
const configFilePath = constants.CONFIG_FILE_PATHS[
constants.getEnv() as keyof typeof constants.CONFIG_FILE_PATHS
] as ConfigType;
if (!configFilePath) throw new Error(constants.NODE_CLI_INVALID_ENV);
constants.setConfig(configFilePath);
};