abdm64 / mongo-typescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ“ Table of Contents

🧐 About

  • This API allow you connect to a mongodb database and preform CRUD operation via http request

🏁 Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a Kubernetes live system.

Prerequisites

You need to install the fellowing software in order to get the application up and running :

  • Node.js and npm.
  • Docker.
  • Kubernetes on cloud.
  • Mongodb.

Development

APP Architecture

  • Alt text

APP Architecture Explained

The application architecture contain 2 part

  • express Nodejs application written in typescript that connect to mongodb to preform a CRUD Operations
  • the api get all the information from the request to preform CRUD operation to the database ( see the api documentation on /docs)

Installing

Running the Node.js application

cd api
npm install
  • Before start the application you should change the keys in the api/src/config folder (ip, user, password etc) in order to connect to the database the default value will connect to a local database.

  • Start the application in watch mode by running npm run serve from the command line in the app folder, you should see the message: "Server started on port: 3000".

npm run serve

Testing

  • to run the the test on watch mode just run npm run test:watch in the api directory
npm run test:watch
  • to run the test with Coverage just run npm run test in the api directory
npm run test

PS : the tests does NOT cover all use cases

πŸš€ Deployment

Deployment Architecture

  • Alt text

Deployment Architecture Explained

  • In order to deploy this application in production mode we need :
    • Docker to build image for this application and push it to the docker hub or private registry .
    • Kubernetes cluster to run the application in production mode from the image that was created .
    • Access to a bash CLI

Docker

Installing

  • Download and install docker on your machine

Build Docker image

  • Build your own docker image and push it to your repo by running "docker build -t my-app-name:v1 . " from the command line in app folder
cd api
docker build -t my-app-name:v1 .

you need to push the image to Docker hub or your own registry .

Run Docker image

  • Run your Docker image for the application by the command line
docker run -e [inject your env variable here] my-app-name:v1

Kubernetes

  • you should have access to Kubernetes cluster GKE or you can download and install a Kubernetes cluster in your machine for development purposes

  • install and configure ingress nginx on your Kubernetes cluster Please see [ingress-nginx] (https://kubernetes.github.io/ingress-nginx/).

  • To run the application on Kubernetes just run the fellowing command from infra directory

cd infra
bash deploy.sh 
  • This script take care of everything, build docker image and deploy it to a kubernetes (you must have kubectl cli )

  • this will create 3 Kubernetes objects:

    • Deployment for the api with one pod ( running container) insuring high availability for that service.
    • Cluster ip service that connected to the pod .
    • ingress service that connect the cluster ip service with ingress-nginx load balancer .
    • StatefulSet the mongodb database.
    • PVC (Persistent Volume Claim) to store the data from the database
  • to drop the application just run the command :

kubectl delete  -f infra/k8s

PS : the config provided works on local k8s cluster with docker desktop in order to works in cloud you must change env variable

⛏️ Built Using

  • Mongodb - Node.js ORM for mongodb
  • Mongoose - Node.js ORM for mongodb
  • NodeJs - Server side environment for javascript .
  • Express - back end web application framework for Node.js .
  • typescript - TypeScript is an open-source language which builds on JavaScript .
  • Docker - Software platform for building and packaging applications.
  • Kubernetes - Container Orchestration.

✍️ Author

Made with ❀️ by Abdellah

About


Languages

Language:TypeScript 98.5%Language:Shell 1.0%Language:Dockerfile 0.5%