Skip to content

Commit

Permalink
Merge pull request #3 from brunotot/ioc-refactor
Browse files Browse the repository at this point in the history
IoC major rework
  • Loading branch information
brunotot authored May 3, 2024
2 parents b7d5259 + 1d97531 commit 0132eea
Show file tree
Hide file tree
Showing 133 changed files with 2,637 additions and 2,274 deletions.
4 changes: 1 addition & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.eslintrc
docs/
./packages/backend/src/internal.ts
packages/backend/src/**/index.ts
**/internal.ts
packages/backend/src/**/index.ts
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"git.ignoreLimitWarning": true,
"typescript.tsdk": "node_modules/typescript/lib",
"editor.codeActionsOnSave": {
"source.organizeImports": "always",
"source.organizeImports": "never",
"source.addMissingImports": "always"
},
"terminal.integrated.profiles.linux": {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"typedoc": "^0.25.12",
"typedoc-material-theme": "^1.0.2",
"typedoc-plugin-custom-tags": "^1.0.2",
"typedoc-plugin-remove-references": "^0.0.6"
"typedoc-plugin-remove-references": "^0.0.6",
"typedoc-theme-hierarchy": "^4.0.0"
},
"keywords": [
"monorepo",
Expand Down
10 changes: 10 additions & 0 deletions packages/backend/assets/css/swagger.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
table.responses-table
tbody
tr.response:not([data-code="500"]):is([data-code^="4"], [data-code^="5"])
:is(section.response-controls, div.model-example) {
display: none;
}

.swagger-ui .highlight-code > .microlight {
min-height: auto !important;
}
Binary file added packages/backend/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 19 additions & 13 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,46 @@
"compile:ts": "tsc && npm run tsc-alias",
"start": "export PACKAGE_JSON_VERSION=$(grep -o '\"version\": *\"[^\"]*\"' package.json | awk -F'\"' '{print $4}') && node --no-warnings --loader ts-node/esm --experimental-specifier-resolution=node ./dist/backend/src/main.js",
"tsc-alias": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
"dependency-test": "npx madge --circular --extensions ts ./src/index.ts"
"dependency-test": "npm run build && npx madge --image graph.png --extensions js ./dist/backend/src/index.js"
},
"keywords": [],
"author": "Bruno Tot",
"license": "MIT",
"dependencies": {
"@anatine/zod-openapi": "^2.2.5",
"@org/shared": "file:../shared/src",
"@ts-rest/core": "^3.45.0",
"@ts-rest/express": "^3.45.0",
"@ts-rest/open-api": "^3.45.0",
"@tsvdec/core": "^2.0.11",
"@tsvdec/decorators": "^1.0.7",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"bottlejs": "^2.0.1",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"cross-dirname": "^0.1.0",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express-rate-limit": "^7.2.0",
"helmet": "^7.1.0",
"hpp": "^0.2.3",
"http-status": "^1.7.4",
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.5.0",
"mongoose": "^8.3.2",
"morgan": "^1.10.0",
"openapi3-ts": "^4.3.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"zod": "^3.22.4"
"winston": "^3.11.0",
"winston-daily-rotate-file": "^5.0.0",
"zod": "^3.22.5"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
"@types/body-parser": "^1.19.5",
"@types/compression": "^1.7.5",
"@types/cookie-parser": "^1.4.7",
"@types/cors": "^2.8.17",
Expand All @@ -43,18 +59,8 @@
"@types/node": "^20.11.22",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"cross-dirname": "^0.1.0",
"dotenv": "^16.4.5",
"helmet": "^7.1.0",
"hpp": "^0.2.3",
"madge": "^7.0.0",
"morgan": "^1.10.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^5.0.0"
"typescript": "^5.3.3"
}
}
16 changes: 0 additions & 16 deletions packages/backend/src/@types/express.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/backend/src/@types/index.ts

This file was deleted.

93 changes: 0 additions & 93 deletions packages/backend/src/@types/swagger.d.ts

This file was deleted.

83 changes: 56 additions & 27 deletions packages/backend/src/App.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import express from "express";

import {
GLOBAL_MIDDLEWARES,
VAR_ZOD_ENVIRONMENT,
mongoConnect,
registerRoutes,
registerSwagger,
startupLog,
} from "@internal";
import * as swaggerUi from "swagger-ui-express";
import { ContractManager, Environment, Logger, MongoClient } from "@org/backend/config";
import { GLOBAL_MIDDLEWARES } from "@org/backend/infrastructure";
import { CONTRACTS, operationMapper, suppressConsole } from "@org/shared";
import { generateOpenApi } from "@ts-rest/open-api";
import { initServer, createExpressEndpoints } from "@ts-rest/express";

export class App {
public readonly app: express.Application;
Expand All @@ -16,31 +13,31 @@ export class App {
public readonly swaggerPath: string;
public readonly url: string;

private environment = Environment.getInstance();
private logger = Logger.getInstance();
private mongoClient = MongoClient.getInstance();

constructor() {
this.app = express();
this.env = VAR_ZOD_ENVIRONMENT.NODE_ENV;
this.port = VAR_ZOD_ENVIRONMENT.PORT;
this.env = this.environment.vars.NODE_ENV;
this.port = this.environment.vars.PORT;
this.swaggerPath = "api-docs";
function buildUrl() {
const domain =
VAR_ZOD_ENVIRONMENT.NODE_ENV === "production"
? `https://${process.env.RAILWAY_PUBLIC_DOMAIN}`
: "http://localhost";

return `${domain}:${VAR_ZOD_ENVIRONMENT.PORT}`;
}
this.url = buildUrl();
const domain =
this.env === "production"
? `https://${process.env.RAILWAY_PUBLIC_DOMAIN}`
: "http://localhost";
this.url = `${domain}:${this.port}`;

this.#initializeDatabase();
this.#initializeMiddlewares();
this.#initializeGlobalMiddlewares();
this.#initializeRoutes();
this.#initializeSwagger();
}

public listen() {
this.app.listen(this.port, () => {
startupLog({
title: `[Express] MERN Sample App v${VAR_ZOD_ENVIRONMENT.PACKAGE_JSON_VERSION}`,
this.logger.table({
title: `[Express] MERN Sample App v${this.environment.vars.PACKAGE_JSON_VERSION}`,
data: {
"🟢 NodeJS": process.version,
"🏠 Env": this.env,
Expand All @@ -55,18 +52,50 @@ export class App {
}

async #initializeDatabase() {
await mongoConnect();
try {
await this.mongoClient.connect();
} catch (error) {
console.log(error);
}
}

#initializeMiddlewares() {
#initializeGlobalMiddlewares() {
GLOBAL_MIDDLEWARES.forEach(middleware => this.app.use(middleware));
}

#initializeRoutes() {
registerRoutes(this.app);
const s = initServer();
const router = s.router(CONTRACTS, ContractManager.getInstance().getRouters());
suppressConsole(() => createExpressEndpoints(CONTRACTS, router, this.app));
}

#initializeSwagger() {
registerSwagger(this.app, this.swaggerPath);
const apiDoc: Parameters<typeof generateOpenApi>[1] = {
info: {
title: "REST API",
license: {
name: "MIT",
url: "https://spdx.org/licenses/MIT.html",
},
termsOfService: "http://swagger.io/terms/",
contact: {
email: "",
name: "",
url: "",
},
version: Environment.getInstance().vars.PACKAGE_JSON_VERSION,
description: "This is a dynamically generated Swagger API documentation",
},
};

const openApiDocument = generateOpenApi(CONTRACTS, apiDoc, { operationMapper });

this.app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(openApiDocument, {
customCssUrl: "/css/swagger.css",
}),
);
}
}
1 change: 1 addition & 0 deletions packages/backend/src/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Every local module inside `config` must only import from `config` using relative paths.
19 changes: 19 additions & 0 deletions packages/backend/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export * from "./singleton/Bottle";
export * from "./singleton/Environment";
export * from "./singleton/Logger";
export * from "./singleton/MongoClient";
export * from "./singleton/InjectableManager";
export * from "./singleton/ContractManager";
export * from "./singleton/JwtManager"

import "./init";

process.on("uncaughtException", err => {
console.error("There was an uncaught error", err);
process.exit(1);
});

process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
process.exit(1);
});
7 changes: 7 additions & 0 deletions packages/backend/src/config/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { extendZodWithOpenApi } from "@anatine/zod-openapi";
import z from "zod";

import { Environment } from "@org/backend/config/singleton/Environment";

extendZodWithOpenApi(z);
Environment.getInstance();
Loading

0 comments on commit 0132eea

Please sign in to comment.