Skip to content

Commit

Permalink
deps(config): setup environment
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakSKumar committed Sep 15, 2024
0 parents commit c619654
Show file tree
Hide file tree
Showing 24 changed files with 6,934 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DB_PORT=
DB_USER=
DB_NAME=
DB_HOST=
DB_PASSWORD=
SERVER_PORT=
NODE_ENV=
SECRET_KEY=
REDIS_HOST=
REDIS_PORT=
REDIS_USERNAME=
REDIS_PASSWORD=
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
build
logs
*.logs
106 changes: 106 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/
.idea/
build/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
39 changes: 39 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
.next
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel

# changelog
CHANGELOG.md
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
arrowParens: 'always',
singleQuote: true,
jsxSingleQuote: true,
tabWidth: 2,
semi: true,
};
18 changes: 18 additions & 0 deletions app/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import express, { Express } from 'express';
import { MyServer } from '@root/setup/setupServer';
import { config } from '@root/config';
class Application {
public start(): void {
this.loadConfig();
const app: Express = express();
const server: MyServer = new MyServer(app);
server.start();
}

private loadConfig(): void {
config.validateConfig();
}
}

const application: Application = new Application();
application.start();
54 changes: 54 additions & 0 deletions app/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import dotenv from 'dotenv';
import bunyan from 'bunyan';
import * as process from 'process';

dotenv.config({});
class Config {
public DB_HOST: string | undefined;
public DB_USER: string | undefined;
public DB_PASSWORD: string | undefined;
public DB_NAME: string | undefined;
public DB_PORT: string | undefined;
public SERVER_PORT: string | undefined;
public NODE_ENV: string | undefined;
public SECRET_KEY: string | undefined;
public REDIS_HOST: string | undefined;
public REDIS_PORT: number | undefined;
public REDIS_USERNAME: string | undefined;
public REDIS_PASSWORD: string | undefined;

constructor() {
this.DB_HOST = process.env.DB_HOST;
this.DB_USER = process.env.DB_USER;
this.DB_PASSWORD = process.env.DB_PASSWORD;
this.DB_NAME = process.env.DB_NAME;
this.DB_PORT = process.env.DB_PORT;

this.SERVER_PORT = process.env.PORT || '5000';

this.NODE_ENV = process.env.NODE_ENV;

this.SECRET_KEY = process.env.SECRET_KEY;

this.REDIS_HOST = process.env.REDIS_HOST;
this.REDIS_PORT = process.env.REDIS_PORT
? parseInt(process.env.REDIS_PORT, 10)
: undefined;
this.REDIS_USERNAME = process.env.REDIS_USERNAME;
this.REDIS_PASSWORD = process.env.REDIS_PASSWORD;
}

public createLogger(name: string): bunyan {
return bunyan.createLogger({ name, level: 'debug' });
}

public validateConfig(): void {
for (const [key, value] of Object.entries(this)) {
if (value === undefined) {
throw new Error(`Configuration ${key} is undefined.`);
}
}
}
}

export const config: Config = new Config();
18 changes: 18 additions & 0 deletions app/src/controllers/dbcrud.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Request, Response } from 'express';
import HTTP_STATUS from 'http-status-codes';
import { userQueue } from 'app/src/workers/user.worker';

export class DBCRUDController {
public async read(req: Request, res: Response): Promise<void> {
try {
await userQueue.add('addUserToDB', { data: 'demo' });
res
.status(HTTP_STATUS.ACCEPTED)
.json({ message: 'Job is processing in the background' });
} catch (err: any) {
res
.status(HTTP_STATUS.INTERNAL_SERVER_ERROR)
.json({ message: 'Failed to trigger job', error: err.message });
}
}
}
113 changes: 113 additions & 0 deletions app/src/helpers/error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import HTTP_STATUS from 'http-status-codes';

export interface IErrorResponse {
message: string;
statusCode: number;
status: string;
serializeErrors(): IError;
}

export interface IError {
message: string;
statusCode: number;
status: string;
}

export abstract class CustomError extends Error {
abstract statusCode: number;
abstract status: string;

constructor(message: string) {
super(message);
}

serializeErrors(): IError {
return {
message: this.message,
status: this.status,
statusCode: this.statusCode,
};
}
}


export class BadRequestError extends CustomError {
statusCode = HTTP_STATUS.BAD_REQUEST;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class NotFoundError extends CustomError {
statusCode = HTTP_STATUS.NOT_FOUND;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class FileTooLargeError extends CustomError {
statusCode = HTTP_STATUS.REQUEST_TOO_LONG;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class ServerError extends CustomError {
statusCode = HTTP_STATUS.SERVICE_UNAVAILABLE;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class BadGatewayError extends CustomError {
statusCode = HTTP_STATUS.BAD_GATEWAY;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class ConflictError extends CustomError {
statusCode = HTTP_STATUS.CONFLICT;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class ForbiddenError extends CustomError {
statusCode = HTTP_STATUS.FORBIDDEN;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class MethodNotAllowedError extends CustomError {
statusCode = HTTP_STATUS.METHOD_NOT_ALLOWED;
status = 'error';

constructor(message: string) {
super(message);
}
}

export class TooManyRequestsError extends CustomError {
statusCode = HTTP_STATUS.TOO_MANY_REQUESTS;
status = 'error';

constructor(message: string) {
super(message);
}
}
10 changes: 10 additions & 0 deletions app/src/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class Helpers {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static parseJson(prop: string): any {
try {
return JSON.parse(prop);
} catch (err) {
return prop;
}
}
}
Loading

0 comments on commit c619654

Please sign in to comment.