Skip to content

FaZeRs/portfolio-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Portfolio API

License: MIT Build Badge Tests Badge Quality Gate Status Coverage Code Smells

Repository Info
portfolio-client Frontend
portfolio-api Rest API
portfolio-server Docker Environment

Features

Feature Info
Authentication JWT
Authorization RBAC (Role based)
ORM Integration TypeORM
DB Migrations TypeORM
Logging winston
Request Validation class-validator
Pagination SQL offset & limit
Docker Ready Dockerfile
Auto-generated OpenAPI -
Auto-generated ChangeLog Release Please
Queue Bull
File System Minio
Mailing Nodemailer

Installation

Note: when using docker, all the pnpm commands can also be performed using ./scripts/pnpm (for example ./scripts/pnpm install). This script allows you to run the same commands inside the same environment and versions than the service, without relying on what is installed on the host.

$ pnpm install

Create a .env file from the template .env.template file.

cp .env.template .env

Generate public and private key pair for jwt authentication:

With docker

Run this command:

./scripts/generate-jwt-keys

It will output something like this. You only need to add it to your .env file.

To setup the JWT keys, please add the following values to your .env file:
JWT_PUBLIC_KEY_BASE64="(long base64 content)"
JWT_PRIVATE_KEY_BASE64="(long base64 content)"

Without docker

$ ssh-keygen -t rsa -b 2048 -m PEM -f jwtRS256.key
# Don't add passphrase
$ openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

You may save these key files in ./local directory as it is ignored in git.

Encode keys to base64:

$ base64 -i local/jwtRS256.key

$ base64 -i local/jwtRS256.key.pub

Must enter the base64 of the key files in .env:

JWT_PUBLIC_KEY_BASE64=BASE64_OF_JWT_PUBLIC_KEY
JWT_PRIVATE_KEY_BASE64=BASE64_OF_JWT_PRIVATE_KEY

Running the app

We can run the project with or without docker.

Local

To run the server without Docker we need this pre-requisite:

  • PostgreSQL server running
  • Redis server running
  • Minio server running

Commands:

# development
$ pnpm start

# watch mode
$ pnpm start:dev

# production mode
$ pnpm start:prod

Docker

# build image
$ docker build -t portfolio-api .

# run container from image
$ docker run -p 3000:3000 --volume 'pwd':/usr/src/app --network="bridge" --env-file .env portfolio-api

# run using docker compose
$ docker compose up

Test

# unit tests
$ pnpm test

# e2e tests
$ pnpm test:e2e

# test coverage
$ pnpm test:cov

Migrations

# generate migration (replace CreateUsers with name of the migration)
$ pnpm migration:generate -- -n CreateUsers

# run migration
$ pnpm migration:run

# revert migration
$ pnpm migration:revert

Architecture