Skip to content

πŸ“– Forum API with DDD and Clean Architecture

Notifications You must be signed in to change notification settings

bonizario/elysian

Repository files navigation

Elysian

This application is the backend of a forum-like platform that allows users to share knowledge and ideas seamlessly.

It results from an in-depth study and implementation of SOLID principles, Clean Architecture, Domain-Driven Design (DDD), and Test-Driven Development (TDD).

πŸ› οΈ Technologies

  • πŸ’» Language and Framework
    • TypeScript
    • NestJS
  • πŸ”’ Authentication
    • JSON Web Token (RS256 Algorithm)
  • ☁️ Storage
    • Cloudflare R2 (AWS S3 API)
  • πŸ’Ύ Databases
    • Postgres
    • Redis (Cache)
  • 🐳 Containerization
    • Docker
  • βœ… Testing
    • Vitest
    • SuperTest (E2E Tests)
    • Faker.js
  • 🧹 Linting
    • ESLint
    • Prettier

πŸ“¦ Installation

Node.js (v20.10.0) is required to install dependencies and run the project.

git clone git@github.com:bonizario/elysian.git && cd elysian

Install the pnpm package manager.

npm install -g pnpm

Install the project dependencies.

pnpm install

πŸ”’ Environment variables

You can generate a private and public key pair using the openssl command in Linux/macOS:

  1. Generate the private key:
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
  1. Generate the public key from the private key:
openssl rsa -pubout -in private_key.pem -out public_key.pem

This will create a 2048-bit RSA private key (private_key.pem) and a corresponding public key (public_key.pem). Make sure to keep your private key secure.

  1. Convert the private key to base64:
base64 -w 0 private_key.pem > private_key_base64.pem
  1. Convert the public key to base64:
base64 public_key.pem > public_key_base64.pem
  1. Copy the .env.example content into a new .env file:
cp .env.example .env
  1. Paste both base64 encoded keys into the .env file and delete the generated files afterward:
JWT_PRIVATE_KEY=
JWT_PUBLIC_KEY=

πŸ’‘ Key Concepts

What is JSON Web Token (JWT)?

JSON Web Token (JWT) is a compact and self-contained method for securely transmitting information between parties as a JSON object.

This information can be verified and trusted because it is digitally signed (via one of the many cryptography algorithms available). The main difference between these algorithms is the type of key they use:

  • Symmetric algorithms (HMAC) use a secret key for signing and verifying tokens. The key must be kept secure and shared between parties that need to verify the token.

  • Asymmetric algorithms (RSA, ECDSA, EdDSA) use a private key for signing tokens and a corresponding public key for verifying them. The private key must be kept secure within the main service, while the public key can be freely distributed to any other party that needs to verify tokens but should not be able to generate them.

Clean Architecture and Domain-Driven Design

Folder structure

tree -d -I 'node_modules|dist|build' --sort name -A -C
β”œβ”€β”€ prisma
β”‚   └── migrations
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ core
β”‚   β”‚   β”œβ”€β”€ entities
β”‚   β”‚   β”œβ”€β”€ errors
β”‚   β”‚   β”œβ”€β”€ events
β”‚   β”‚   β”œβ”€β”€ repositories
β”‚   β”‚   └── types
β”‚   β”œβ”€β”€ domain
β”‚   β”‚   β”œβ”€β”€ forum
β”‚   β”‚   β”‚   β”œβ”€β”€ application
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ cryptography
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ repositories
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ storage
β”‚   β”‚   β”‚   β”‚   └── use-cases
β”‚   β”‚   β”‚   β”‚       └── errors
β”‚   β”‚   β”‚   └── enterprise
β”‚   β”‚   β”‚       β”œβ”€β”€ entities
β”‚   β”‚   β”‚       β”‚   └── value-objects
β”‚   β”‚   β”‚       └── events
β”‚   β”‚   └── notification
β”‚   β”‚       β”œβ”€β”€ application
β”‚   β”‚       β”‚   β”œβ”€β”€ repositories
β”‚   β”‚       β”‚   β”œβ”€β”€ subscribers
β”‚   β”‚       β”‚   └── use-cases
β”‚   β”‚       └── enterprise
β”‚   β”‚           └── entities
β”‚   └── infra
β”‚       β”œβ”€β”€ auth
β”‚       β”œβ”€β”€ cache
β”‚       β”‚   └── redis
β”‚       β”œβ”€β”€ cryptography
β”‚       β”œβ”€β”€ database
β”‚       β”‚   └── prisma
β”‚       β”‚       β”œβ”€β”€ mappers
β”‚       β”‚       └── repositories
β”‚       β”œβ”€β”€ env
β”‚       β”œβ”€β”€ events
β”‚       β”œβ”€β”€ http
β”‚       β”‚   β”œβ”€β”€ controllers
β”‚       β”‚   β”œβ”€β”€ pipes
β”‚       β”‚   └── presenters
β”‚       └── storage
└── test
    β”œβ”€β”€ cryptography
    β”œβ”€β”€ e2e
    β”œβ”€β”€ factories
    β”œβ”€β”€ repositories
    β”œβ”€β”€ storage
    └── utils