- This API allow you connect to a mongodb database and preform CRUD operation via http request
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.
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.
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)
- Download and Install node.js and NPM from https://nodejs.org/en/download/ .
-
Download or clone the project code from https://github.com/abdm64/mongo-typescript.git
-
Install all required npm packages by running npm install from the command line in the app folder (where the package.json is located).
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
- Now you have api that connected to the mongo database,you can visit the swagger documentation on http://localhost:3000/docs
- 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
- 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
- Download and install docker on your machine
- 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 your Docker image for the application by the command line
docker run -e [inject your env variable here] my-app-name:v1
-
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
- 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.
Made with β€οΈ by Abdellah