Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup ⚒️ and config env variables & docker-composed instances #2

Merged
merged 4 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Ignore node_modules directory
node_modules

# Ignore build output directory
dist

# Ignore development files
.env
.env.\*
.env.local
.env.development
.env.test
.env.production

# Ignore logs
logs
\*.log

# Ignore Docker-related files
.dockerignore
22 changes: 22 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# App specific configuration
NODE_ENV=development # Change it as you may need
APP_NAME=nestjs-api # Change it as you may need
APP_VERSION=1.0.0 # Change it as you may need
APP_DESCRIPTION='NestJS API' # Change it as you may need
APP_SECRET=
PORT=4000 # Change it as you may need


# Database configuration
DB_HOST=127.0.0.1 # IPv4
DB_PORT=5432 # Postgres default port
DB_NAME=nestjs_db # Change it as you may need
DB_USER=developer # Change it as you may need
DB_PASSWORD=developer123 # Change it as you like

# API configuration
API_SECRET=super_secret # Change it as you like

# JWT configuration
JWT_SECRET=super_secret_007 # Change it as you like
JWT_EXPIRES_IN='12h' # Change it as you like
35 changes: 35 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Linters

on:
pull_request:
branches:
- main
- dev

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 20 # Change this to your Node.js version

- name: Install dependencies
run: npm ci

- name: Run ESLint
run: npm run lint

- name: Check node_modules
run: |
if [ -d "node_modules" ]; then
echo "Error: node_modules directory exists! Please remove ❌ it from your PR. & push again."
else
echo "node_modules directory does not exist in the PR ✅. Good to go!"
exit 1
fi
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ lerna-debug.log*
!.vscode/extensions.json

# dotenv environment variable files
.env*
*.env*
!.env.example

# temp directory
Expand Down
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"files.eol": "\n",
"rest-client.mimeAndFileExtensionMapping": {
"application/json": "json",
"application/javascript": "js",
"application/pdf": "pdf",
"application/xhtml+xml": "xhtml",
"application/xml": "xml",
"application/zip": "zip"
},
"cSpell.words": ["commitlint", "mahabubx", "nestjs", "precommit"]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Mailer: NodeMailer (tempMail)
- [x] Setup `commit-lint` for standard commit history
- [x] Setup `ESLint` & `Prettier`
- [x] Setup `husky` & `lint-staged` for lint-proof commits
- [ ] Setup and config `env` variables & docker-composed instances
- [x] Setup and config `env` variables & docker-composed instances
- [ ] Setup `Knex.js` with `PostgreSQL`
- [ ] Setup Validation-pipes for incoming request body
- [ ] Implement User Module with CRUD
Expand Down
25 changes: 25 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use the official Node.js 20 image as the base image
FROM node:lts

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install the app dependencies
RUN npm install --frozen

# Copy the rest of the app source code to the working directory
COPY . .

# Build the app
RUN npm run build

# Expose the port that the app will run on
# Note: Change this as you see fit
EXPOSE 3000

# Start the app
# Note: Change this as you see fit
CMD [ "npm", "run", "start:prod", "--host", "0.0.0.0"]
22 changes: 22 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'
services:
api:
image: your-api-image:latest
ports:
- 80:80
environment:
- NODE_ENV=production
- APP_ENV=production
restart: always
# Add any additional configuration specific to your API service

db:
image: postgres:latest
# Add any additional configuration specific to your database service

# Add any other services your application requires (e.g., caching, message queue, etc.)

networks:
default:
external:
name: network-stack-name-nestjsapp
9 changes: 9 additions & 0 deletions docs/hello.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Hello, World - Test using REST-API HTTP client

GET http://localhost:4000 HTTP/1.1
content-type: application/json

## Response
{
"message": "Hello, World!"
}
50 changes: 48 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"docker:prod": "sh scripts/prod.bash",
"docker:reset": "sh scripts/reset.bash",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
Expand All @@ -22,6 +25,7 @@
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"reflect-metadata": "^0.2.0",
Expand Down
17 changes: 17 additions & 0 deletions scripts/prod.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Set the environment variables
export NODE_ENV=production # Node.js level declaration
export APP_ENV=production # Nest.js app level declaration

arguments = "$1"

# Start the Docker container
docker compose -p nestjs_app -f docker/docker-compose.yml up -d $arguments

# Check if the container is running
if [ "$(docker ps -q -f name=nestjs_app)" ]; then
echo "Server 🚀 started successfully!"
else
echo "Failed ❌ to start the server."
fi
16 changes: 16 additions & 0 deletions scripts/reset.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Set the project name
PROJECT_NAME="$1" # Pass the project name as an argument

# Stop and remove containers
docker compose --project-name $PROJECT_NAME down

# Remove volumes
docker compose --project-name $PROJECT_NAME down -v

# Remove images
docker compose --project-name $PROJECT_NAME rm -f

# Remove networks
docker compose --project-name $PROJECT_NAME network prune -f
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
getHello(): Record<string, string> {
return this.appService.getHello();
}
}
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { envVariables } from './config';

@Module({
imports: [],
imports: [ConfigModule.forRoot({ isGlobal: true, load: [envVariables] })],
controllers: [AppController],
providers: [AppService],
})
Expand Down
6 changes: 6 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import variablesConfig from './variables.config';

const envVariables = variablesConfig;

// Exports the config object
export { envVariables };
Loading
Loading