Skip to content

Commit

Permalink
code refatcor for config
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshuteotia committed Feb 10, 2024
1 parent eeb0664 commit 8f41a91
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class App {
express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 })
);
app.use(rateLimitRequestHandler());
app.use('/', (req, res)=> res.status(200).send("Welcome"))
app.use('/', new LoggerMiddleware().endpointsLogs, (req, res)=> res.status(200).send("Welcome"))
app.use(
'/v1/',
new LoggerMiddleware().endpointsLogs,
Expand Down
1 change: 1 addition & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum EnvironmentStr {
const {ENVIRONMENT} = process.env;
export const production = ENVIRONMENT === EnvironmentStr.PRODUCTION;
export const development = ENVIRONMENT === EnvironmentStr.DEVELOPMENT;
export const environment = production ? EnvironmentStr.PRODUCTION : EnvironmentStr.DEVELOPMENT;

const config = () =>{
if(production){
Expand Down
2 changes: 1 addition & 1 deletion src/config/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ const config = {
debug: true,
} satisfies Record<string,number|boolean>

export default merge(production,config);
export default merge(production,config) as typeof production;
3 changes: 2 additions & 1 deletion src/config/production.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const config = {
port : 3000,
debug: true,
} satisfies Record<string,number|boolean>
website: "https://somewebsite.com"
} satisfies Record<string,number|boolean|string>

export default config;
5 changes: 4 additions & 1 deletion src/middlewares/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import logger from '../utils/logger.util';
import { Request, Response, NextFunction } from 'express';
import config from '../config/config';

export class LoggerMiddleware {
endpointsLogs(
request: Request,
response: Response,
next: NextFunction
): void {
logger.info(`${request.method} ${request.path}`);
if(config?.debug){
logger.info(`${request.method} ${request.path}`);
}
next();
}
}
4 changes: 2 additions & 2 deletions src/middlewares/rate-limit.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import rateLimit, { RateLimitRequestHandler } from 'express-rate-limit';
import { environment } from '../config/config';

const env = process.env.NODE_ENV || 'dev';
const rateLimitRequest = Number(process.env.RATE_LIMIT_TIME) || 15;
const rateLimitTime = Number(process.env.RATE_LIMIT_REQUEST) || 100;

export default (): RateLimitRequestHandler => {
if (env === 'production') {
if (environment === 'production') {
return rateLimit({
windowMs: rateLimitTime * 60 * 1000, // 15 minutes
max: rateLimitRequest // limit each IP to 30 requests per windowMs
Expand Down

0 comments on commit 8f41a91

Please sign in to comment.