Skip to content

Commit

Permalink
Update environment variables and add compression middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinhern committed Dec 26, 2023
1 parent 59133b5 commit caa3227
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 6 deletions.
7 changes: 5 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# App's running environment
NODE_ENV=
NODE_ENV=development

# App's running port
PORT=
PORT=8080

# Cors Origin URL
CORS_ORIGIN=""

# App's public path
PUBLIC_PATH="/"
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
3 changes: 0 additions & 3 deletions lint-staged.config.js

This file was deleted.

76 changes: 76 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"lint:fix": "eslint --fix --ext .ts,.js src/",
"format": "prettier --write 'src/**/*.{ts,js,json}'",
"check-format": "prettier --check 'src/**/*.{ts,js,json}'",
"prepare": "husky install",
"docker-prod": "docker-compose up --build",
"docker-stop": "docker-compose down"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"compression": "^1.7.4",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"dotenv": "^16.3.1",
Expand All @@ -27,6 +29,7 @@
},
"devDependencies": {
"@tsconfig/node-lts-strictest-esm": "^18.12.1",
"@types/compression": "^1.7.5",
"@types/cors": "^2.8.17",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.21",
Expand All @@ -36,11 +39,18 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-simple-import-sort": "^10.0.0",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"nodemon": "^3.0.2",
"prettier": "^3.1.1",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"lint-staged": {
"**/*.{js,jsx,ts,tsx}": [
"npx prettier --write",
"npx eslint --fix"
]
}
}
7 changes: 6 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import compression from "compression";
import cors from "cors";
import express, { Express } from "express";
import helmet from "helmet";
import { pino } from "pino";
import { pinoHttp } from "pino-http";

import { middlewares } from "./middleware";
import compressFilter from "./utils/compress-filter";
import { getCorsOrigin } from "./utils/env";

const logger = pino({ name: "server start" });
const app: Express = express();
const cors_origin = getCorsOrigin();

// Setup Middlewares
app.use(cors({ credentials: true }));
app.use(cors({ origin: [cors_origin], credentials: true }));
app.use(pinoHttp({ logger }));
app.use(helmet());
app.use(compression({ filter: compressFilter }));
app.use(middlewares());

export { app, logger };
20 changes: 20 additions & 0 deletions src/utils/compress-filter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import compression from "compression";
import type { Request, Response } from "express";

/**
* Filter Function for the compression middleware
* @param req HTTPs Request
* @param res HTTPs Response
* @returns Returns false if request header contains x-no-compression
*/
function compressFilter(req: Request, res: Response): boolean {
if (req.headers["x-no-compression"]) {
// don't compress responses with this request header
return false;
}

// fallback to standard filter function
return compression.filter(req, res);
}

export default compressFilter;
8 changes: 8 additions & 0 deletions src/utils/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import * as dotenv from "dotenv";
import path from "path";

dotenv.config({
path: path.resolve(__dirname, "../../.env"),
});

export const getPort = () => getEnvVar<number>("PORT", "number");
export const getNodeEnv = () => getEnvVar<string>("NODE_ENV", "string");
export const getCorsOrigin = () => getEnvVar<string>("CORS_ORIGIN", "string");

function getEnvVar<T extends string | number>(key: string, type: "string" | "number"): T {
const value = process.env[key];
Expand Down

0 comments on commit caa3227

Please sign in to comment.