Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sydrawat01/rest-api
Production-ready REST API with Docker, Hashicorp Packer, custom error handlers and ES7+ support
https://github.com/sydrawat01/rest-api
aws-ami bash-script docker error-handling es7 expressjs hashicorp-packer nodejs packer postgresql production rest-api sequelize sequelize-orm winston-logger
Last synced: 23 days ago
JSON representation
Production-ready REST API with Docker, Hashicorp Packer, custom error handlers and ES7+ support
- Host: GitHub
- URL: https://github.com/sydrawat01/rest-api
- Owner: sydrawat01
- License: mit
- Created: 2023-02-13T15:35:41.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-01-12T20:35:24.000Z (10 months ago)
- Last Synced: 2024-10-12T22:23:25.809Z (about 1 month ago)
- Topics: aws-ami, bash-script, docker, error-handling, es7, expressjs, hashicorp-packer, nodejs, packer, postgresql, production, rest-api, sequelize, sequelize-orm, winston-logger
- Language: JavaScript
- Homepage:
- Size: 319 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# REST API
[![Unit Tests](https://github.com/sydrawat01/rest-api/actions/workflows/unit-test.yml/badge.svg)](https://github.com/sydrawat01/rest-api/actions/workflows/unit-test.yml) [![Packer AMI template validation](https://github.com/sydrawat01/rest-api/actions/workflows/packer-validate.yml/badge.svg)](https://github.com/sydrawat01/rest-api/actions/workflows/packer-validate.yml) [![Build](https://github.com/sydrawat01/rest-api/actions/workflows/packer-build.yml/badge.svg)](https://github.com/sydrawat01/rest-api/actions/workflows/packer-build.yml) [![Release](https://github.com/sydrawat01/rest-api/actions/workflows/release.yml/badge.svg)](https://github.com/sydrawat01/rest-api/actions/workflows/release.yml)
This is a basic skeleton code to create a basic REST API using NodeJS, ExpressJS and Babel.
> \[!NOTE]\
> NOTE: Please note that this is still a work in progress.## Features
- ES7+
- NodeJS
- ExpressJS
- Winston for logging
- Basic test scripts
- Custom error handlers
- PostgreSQL and Sequelize
- Bcrypt
- Custom Amazon Machine Image using Hashicorp Packer
- Containerization using Docker## Planned features
- AWS S3, DynamoDB and SNS Topics
- Email verification of user
- TypeScript support## Connect to local db
```shell
psql postgres
CREATE ROLE me WITH LOGIN PASSWORD 'password';
ALTER ROLE me CREATEDB;psql -d postgres -U me
CREATE DATABASE test;
\c test
select * from users;
```## Docker
This is an example repository that will help you dockerize your Node+Express application using Docker.
1. Create a simple REST API with the following endpoints:
- `/` that returns OK status code (200)
- `/health` that returns OK status code(200)2. Create a simple test that checks the health routes.
3. Create a new file named `Dockerfile` at the root of the repository.
4. Add the following configuration to the above created file:
```docker
# which node to use for your container
FROM node:alpine# the directory where the appication will be copied to in your container
WORKDIR /usr/src/app# copy the contents of the base(app) directory to the container WORKDIR
COPY . .# commands to run to install dependencies
RUN npm install# port that must be exposed to the client where the application is running in the container
EXPOSE 3000# command to start the application in the container
CMD ["yarn", "start"]
```5. Now, to create an image of your basic REST API:
```shell
# -t option to tag the image with a version
# if a version is not provided, the default tag is 'latest'
# the '.' option at the end is to build the current working directory
docker build -t : .
```Example:
```shell
docker build -t api:v1 .
```To create a docker image using another version of a `Dockerfile`, use the following command:
```shell
docker build -t : -f .
```Example:
```shell
docker build -t api:v2 -f Dockerfile.dev .
```6. To verify your image is build and ready:
```shell
# lists all images that docker has built or pulled from docker hub
docker images
```7. To create and run a container:
```shell
# -ti is to run the container in an interactive terminal mode
# --name is to name your container
# -p is to expose the port from the container to the local machine where the container is running
docker run -ti --name \
-p : \
:
```Example:
```shell
docker run -ti --name rest-api-container -p 3000:1337 api:v1
```8. Test the API endpoints using `Postman` on `http://localhost:3000`.
9. To stop a running container:
```shell
docker container stop
```Example:
```shell
docker container stop gpt-container
```10. To restart a stopped container:
```shell
docker container start
```Example:
```shell
docker container start gpt-container
```To enter the `bash` terminal within the container:
```shell
docker exec -ti bash
```Example:
```shell
docker exec -ti gpt-container bash
```### Docker bind mount
The `-v` flag represents the volume in our container, where our application will be uploaded in the Docker Ubuntu image.
```shell
docker run --name \
-p : \
-v : \
:
```Example:
```shell
docker run --name rest-api-container-2 \
-p 3000:1337 \
-v $(pwd):/usr/src/app \
api:v1
```> NOTE: If you are on a windows computer, `$(pwd)` will not work. Instead, use `%cd%`.
### Anonymous volume
To leave the `node_modules` directory untouched in the local machine directory, and to not track it in the docker container, we use anonymous volumes.
```shell
docker run --name \
-p : \
-v : \
-v \
:
```Example:
```shell
docker run --name rest-api-container-3
-p 3000:1337 \
-v $(pwd):/usr/src/app \
-v /usr/src/app/node_modules \
api:v1
```Since we do not have the `:` colon to link this volume in our container to the local machine directory, this is an anonymous volume.
### SSH into a container
```shell
docker exec -ti /bin/sh
```Example:
```shell
docker exec -ti rest-api-container-3 /bin/sh
```### Stop and delete containers
1. Look for the container name:
```shell
docker ps
```2. Stop the container using the container name:
```shell
docker stop
```Example:
```shell
╰─ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15fa4c6bbd11 api:v2 "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 0.0.0.0:3000->1337/tcp rest-api
``````shell
docker stop rest-api
```3. To check for stopped containers:
```shell
docker ps -a
```4. Make sure that the container is stopped, then proceed to remove that container.
```shell
docker ps;
docker rm
```Example:
```shell
docker rm rest-api
```### Delete docker images
To delete docker images:
1. Check the name of the image you want to delete:
```shell
docker images
```2. Delete the image by it's name:
```shell
docker image rm :
```Example:
```shell
# check for docker images
docker images
``````shell
# output
╰─ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
api v3 4d74db3e5ada 7 hours ago 366MB
api v2 7bc302778f7c 7 hours ago 366MB
``````shell
# remove the desired docker image
docker image rm api:v2
```### Work in progress
Do not build `.env` into the docker image.
## Author
[Siddharth Rawat](mailto:[email protected])
## License
[MIT License](./LICENSE)