Skip to content

Commit

Permalink
FEAT: MAIN FUNCTIONALITY
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-GF authored Jan 4, 2024
2 parents f88acfa + 858b92c commit c7aa7bd
Show file tree
Hide file tree
Showing 42 changed files with 13,826 additions and 798 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Deploy to GCE

on:
push:
branches:
- "main"

env:
ENV_CONFIGURATION: ${{ secrets.ENV_CONFIGURATION }}
TERRAFORM_VARS: ${{ secrets.TERRAFORM_VARS }}
GCP_CREDENTIALS: ${{ secrets.ENCODED_GCP_CREDENTIALS }}

jobs:
deploy-flatter-to-gce:
name: Users Microservice Deployment to GCE
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.4.6
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
terraform_wrapper: false

- name: Populate terraform variables
working-directory: ./terraform
run: |-
mkdir .ssh
ssh-keygen -f .ssh/usersMicroservice -t rsa -N ""
echo "$GCP_CREDENTIALS" | base64 --decode > GoogleCloudKey.json
echo "$TERRAFORM_VARS" | base64 --decode > terraform.tfvars
echo "$ENV_CONFIGURATION" | base64 --decode > .env.prod
- name: Terraform Format
id: fmt
run: terraform fmt
continue-on-error: true
working-directory: ./terraform

- name: Terraform Init
id: init
run: terraform init
working-directory: ./terraform

- name: Terraform Validate
id: validate
run: terraform validate -no-color
working-directory: ./terraform

- name: Terraform Destroy
id: destroy
run: terraform destroy -target=google_compute_instance.users_service_instance -auto-approve -input=false -no-color
working-directory: ./terraform
continue-on-error: true

- name: Terraform Plan
id: plan
run: |
terraform plan -input=false -no-color
working-directory: ./terraform

- name: Terraform Apply
run: terraform apply -auto-approve -input=false -no-color
working-directory: ./terraform
81 changes: 81 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Tests

on: [pull_request]

jobs:
build:
name: Test
runs-on: ubuntu-latest

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

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '20.x'

- name: Decrypt Google credentials.json
env:
GOOGLE_API_PW: ${{ secrets.GCP_CREDENTIALS_PK }}
run: |
gpg --quiet --batch --yes --decrypt --passphrase="$GOOGLE_API_PW" \
--output $GITHUB_WORKSPACE/GoogleCloudKey.json GoogleCloudKey.json.gpg
- name: Make envfile
uses: SpicyPizza/[email protected]
with:
envkey_DB_URI: ${{ vars.CI_MONGODB_URI }}
envkey_DB_NAME: ${{ vars.CI_MONGODB_NAME }}
envkey_DB_USER: ${{ vars.CI_MONGODB_USER }}
envkey_DB_PASS: ${{ secrets.CI_MONGODB_PASSWORD }}
envkey_PORT: ${{ vars.CI_PORT }}
envkey_NODE_ENV: ${{ vars.CI_NODE_ENV }}
envkey_AUTH_DB: ${{ vars.CI_AUTH_DB }}
envkey_JWT_SECRET: ${{ secrets.CI_JWT_SECRET }}
envkey_JWT_EXPIRATION_TIME: ${{ vars.CI_JWT_EXPIRATION_TIME }}
envkey_SALT_ROUNDS: ${{ secrets.CI_SALT_ROUNDS }}
envkey_RESET_DB_ON_EACH_RELOAD: ${{ vars.CI_RESET_DB_ON_EACH_RELOAD }}
envkey_ADMIN_USERNAME: ${{ secrets.CI_ADMIN_USERNAME }}
envkey_ADMIN_PASSWORD: ${{ secrets.CI_ADMIN_PASSWORD }}
envkey_ADMIN_EMAIL: ${{ secrets.CI_ADMIN_EMAIL }}
envkey_TESTING_API_BASE_URL: ${{ vars.CI_TESTING_API_BASE_URL }}
envkey_BASE_URL: ${{ vars.CI_BASE_URL }}
envkey_DB_TEST_NAME: ${{ vars.CI_MONGODB_TEST_NAME }}
envkey_GCS_PROJECT_NAME: ${{ vars.CI_GCS_PROJECT_NAME }}
envkey_GCS_BUCKET_NAME: ${{ vars.CI_GCS_BUCKET_NAME }}
envkey_GCS_BUCKET_URL: ${{ vars.CI_GCS_BUCKET_URL }}
envkey_GCF_GENERATE_TOKEN_ENDPOINT: ${{ vars.GCF_GENERATE_TOKEN_ENDPOINT }}
envkey_GCF_VERIFY_TOKEN_ENDPOINT: ${{ vars.GCF_VERIFY_TOKEN_ENDPOINT }}
envkey_GCF_GET_PAYLOAD_FROM_TOKEN_ENDPOINT: ${{ vars.GCF_GET_PAYLOAD_FROM_TOKEN_ENDPOINT }}
envkey_SENDINBLUE_API_KEY: ${{ secrets.CI_SENDINBLUE_API_KEY }}
directory: ./server
file_name: .env
fail_on_empty: false
sort_keys: false

- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: ${{ vars.CI_MONGODB_VERSION }}
mongodb-username: ${{ vars.CI_MONGODB_USER }}
mongodb-password: ${{ secrets.CI_MONGODB_PASSWORD }}
mongodb-db: ${{ vars.CI_MONGODB_NAME }}
mongodb-port: ${{ vars.CI_MONGODB_PORT }}

- name: Install dependencies
working-directory: ./server
run: npm ci

- name: Build project
working-directory: ./server
run: npm run build

- name: Start server
working-directory: ./server
run: npm run start --run-tests&

- name: Run tests
working-directory: ./server
run: npm test
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,15 @@ dist

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

# Google Cloud Key
GoogleCloudKey.json

# Prod variables
terraform/.env.prod
.terraform/
.terraform*
.ssh/
*.tfvars
.terraform/
Binary file added GoogleCloudKey.json.gpg
Binary file not shown.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# sample-microservice
This is a base project to create a microservice using the following set of technologies: MongoDB, ExpressJS, ReactJS and NodeJS
# FISG G4 Users Microservice

![Deployment](https://github.com/fis-g4/users-microservice/actions/workflows/cd.yml/badge.svg)
![Tests](https://github.com/fis-g4/users-microservice/actions/workflows/test.yml/badge.svg)

This microservice is in charge of authenticating users and managing their data.
30 changes: 15 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ services:
# expose:
# - 6379
# networks:
# - sample-microservice
# - users-microservice

mongo-db:
mongo_db:
image: mongo:6.0
container_name: mongo-db
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=sample-db
- MONGO_INITDB_ROOT_USERNAME=${DB_USER}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASS}
- MONGO_INITDB_DATABASE=${DB_NAME}
ports:
- "27017:27017"
volumes:
- db:/data/db
networks:
- sample-microservice
- users-microservice

server:
restart: always
container_name: sample-microservice-backend
image: sample-microservice-backend:latest
container_name: users-microservice-backend
image: users-microservice-backend:latest
build:
context: .
dockerfile: server/docker/Dockerfile
Expand Down Expand Up @@ -59,12 +59,12 @@ services:
# links:
# - redis
networks:
- sample-microservice
- users-microservice

nginx:
restart: always
container_name: sample-microservice-nginx
image: sample-microservice-nginx:latest
container_name: users-microservice-nginx
image: users-microservice-nginx:latest
build:
context: ./nginx
dockerfile: ./Dockerfile
Expand All @@ -77,7 +77,7 @@ services:
- server
- mongo_db
networks:
- sample-microservice
- users-microservice

# volumes:
# static:
Expand All @@ -87,14 +87,14 @@ services:

volumes:
db:
name: sample-microservice-db
name: users-microservice-db
# redis-data:
# name: flatter-redis-data

networks:
sample-microservice:
users-microservice:
driver: bridge
ipam:
driver: default
config:
- subnet: 10.5.0.0/16
- subnet: 10.4.0.0/16
47 changes: 41 additions & 6 deletions server/.env.dev
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
DB_URI=mongodb://localhost:27017
DB_DATABASE=example-db
DB_USER=root
DB_PASS=password
PORT=8000
NODE_ENV=development
# MONGODB CONFIGURATION
DB_URI=mongodb://localhost:27017 # or your mongodb uri
DB_NAME=example-db # or your mongodb database name
DB_USER=root # or your mongodb user
DB_PASS=password # or your mongodb password
AUTH_DB=admin # or your mongodb auth db

# ADMIN CONFIGURATION
ADMIN_USERNAME=admin # or your admin username
ADMIN_PASSWORD=password # or your admin password
ADMIN_EMAIL=[email protected] # or your admin email

# JWT CONFIGURATION
JWT_SECRET=secret # or your jwt secret
JWT_EXPIRATION_TIME=24h # or your jwt expiration time
SALT_ROUNDS=10 # or your salt rounds for bcrypt

# SERVER CONFIGURATION
RESET_DB_ON_EACH_RELOAD=true # or false if you don't want to reset db on each reload
PORT=8000 # or your port
NODE_ENV=development # or production if you want to run in production mode

# TESTING CONFIGURATION
TESTING_API_BASE_URL=http://localhost:8000/v1 # or your testing api base url
DB_TEST_NAME=example-db-test # or your mongodb test database name

# SWAGGER CONFIGURATION
BASE_URL=http://localhost:8000/v1/users # or your custom base url

# SENDINBLUE CONFIGURATION
SENDINBLUE_API_KEY=xkeysib-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXX # or your sendinblue api key

# GOOGLE STORAGE CONFIGURATION
GCS_PROJECT_NAME="FIS G4"
GCS_BUCKET_NAME="test-user-images-bucket"
GCS_BUCKET_URL="https://storage.googleapis.com"

# CLOUD FUNCTION CONFIGURATION
GCF_GENERATE_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-generate"
GCF_VERIFY_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-verify"
GCF_GET_PAYLOAD_FROM_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-payload"
48 changes: 41 additions & 7 deletions server/.env.prod
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
# DB_URI=mongodb://mongo-db:27017
DB_DATABASE=sample-db
DB_USER=root
DB_PASS=password
AUTH_DB=admin
PORT=8000
NODE_ENV=production
# MONGODB CONFIGURATION
DB_URI=mongodb://localhost:27017 # or your mongodb uri
DB_NAME=example-db # or your mongodb database name
DB_USER=root # or your mongodb user
DB_PASS=password # or your mongodb password
AUTH_DB=admin # or your mongodb auth db

# ADMIN CONFIGURATION
ADMIN_USERNAME=admin # or your admin username
ADMIN_PASSWORD=password # or your admin password
ADMIN_EMAIL=[email protected] # or your admin email

# JWT CONFIGURATION
JWT_SECRET=secret # or your jwt secret
JWT_EXPIRATION_TIME=24h # or your jwt expiration time
SALT_ROUNDS=10 # or your salt rounds for bcrypt

# SERVER CONFIGURATION
RESET_DB_ON_EACH_RELOAD=true # or false if you don't want to reset db on each reload
PORT=8000 # or your port
NODE_ENV=development # or production if you want to run in production mode

# TESTING CONFIGURATION
TESTING_API_BASE_URL=http://localhost # or your testing api base url
DB_TEST_NAME=example-db-test # or your mongodb test database name

# SWAGGER CONFIGURATION
BASE_URL=http://localhost/users # or your custom base url

# SENDINBLUE CONFIGURATION
SENDINBLUE_API_KEY=xkeysib-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXX # or your sendinblue api key

# GOOGLE STORAGE CONFIGURATION
GCS_PROJECT_NAME="FIS G4"
GCS_BUCKET_NAME="test-user-images-bucket"
GCS_BUCKET_URL="https://storage.googleapis.com"

# CLOUD FUNCTION CONFIGURATION
GCF_GENERATE_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-generate"
GCF_VERIFY_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-verify"
GCF_GET_PAYLOAD_FROM_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-payload"
38 changes: 38 additions & 0 deletions server/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# MONGODB CONFIGURATION
DB_URI=mongodb://localhost:27017 # or your mongodb uri
DB_NAME=test-db # or your mongodb database name
DB_USER=root # or your mongodb user
DB_PASS=${{ secrets.CI_MONGODB_PASSWORD }} # or your mongodb password
AUTH_DB=admin # or your mongodb auth db

# ADMIN CONFIGURATION
ADMIN_USERNAME=${{ secrets.CI_ADMIN_USERNAME }} # or your admin username
ADMIN_PASSWORD=${{ secrets.CI_ADMIN_PASSWORD }} # or your admin password
ADMIN_EMAIL=${{ secrets.CI_ADMIN_EMAIL }} # or your admin email

# JWT CONFIGURATION
JWT_SECRET=${{ secrets.CI_JWT_SECRET }} # or your jwt secret
JWT_EXPIRATION_TIME=24h # or your jwt expiration time
SALT_ROUNDS=${{ secrets.CI_SALT_ROUNDS }} # or your salt rounds for bcrypt

# SERVER CONFIGURATION
RESET_DB_ON_EACH_RELOAD=true # or false if you don't want to reset db on each reload
PORT=8000 # or your port
NODE_ENV=development # or production if you want to run in production mode

# TESTING CONFIGURATION
TESTING_API_BASE_URL=http://localhost:8000/v1 # or your testing api base url
DB_TEST_NAME=example-db-test # or your mongodb test database name

# SWAGGER CONFIGURATION
BASE_URL=http://localhost:8000/v1/users # or your custom base url

# GOOGLE STORAGE CONFIGURATION
GCS_PROJECT_NAME="FIS G4"
GCS_BUCKET_NAME="test-user-images-bucket"
GCS_BUCKET_URL="https://storage.cloud.google.com"

# CLOUD FUNCTION CONFIGURATION
GCF_GENERATE_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-generate"
GCF_VERIFY_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-verify"
GCF_GET_PAYLOAD_FROM_TOKEN_ENDPOINT="https://europe-southwest1-liquid-layout-406710.cloudfunctions.net/fis-g4-jwt-payload"
Loading

0 comments on commit c7aa7bd

Please sign in to comment.