Giter Club home page Giter Club logo

microservices-backend's Introduction

BeatGo - Microservices Backend API & JWT Refresh Token - Blacklist Authorization process

BeatGo app microservices Backend API has been designed to showcase microservices design patterns & refresh token - Blacklist authorization process. It consists of a two lightweight services, supporting database and back end data enrichment functions.

The BeatGo microservices Backend API is based on simple REST principles. The web API endpoints return JSON metadata about music artists, albums, and tracks, directly from the Spotify Data Catalogue. It also allows user registration and login, it provides access to user related data, like playlists and music that the user saves in the Your Music library. Such access is enabled through selective authorization, by the user.

  • The two microservices are both written in Node.js using the Express framework. These have been containerized so can easily be deployed & run as containers
  • Two NoSQL databases document store holding JSON, provided by MongoDB.
  • Reverse proxy using NGINX responsible for forwarding requests and responses between the clients and the microservices. Our primary purpose is to access our API mapping to port 80 for HTTP.
  • Redis Cache Memory server used in the authorization process for storing refresh tokens (key:userId, value:refreshToken) and used tokens in a blacklist.
  • Message broker using MQRabbit to provide communication between services. (Subscriber/Publisher)

The app has been designed to be containerize with Docker Compose to run all the images in the same container.

This application supports a range of demonstration, and learning scenarios, such as:

  • A working example of microservices design
  • Use of containers, Docker
  • NoSQL and document stores over traditional relational databases
  • RESTful API design
  • Use of an open source application stack such as Node.js

Table of Contents


๐Ÿ—ƒ๏ธ Architecture & Core App Components

architecture

๐Ÿค˜ Getting Started

There are a number of ways to get started with this project depending on your goals. Below are some suggestions and pointers

Build and Run

Prerequisites:

Assuming you have the pre-reqs installed and MongoDB running. A simplified "happy path" of the steps at the bash command line are:

1. Clone the repo, set up local configuration env files and install Node modules (This configuration only allow you to run microservices webs API. Redis must be installed locally in your computer as a local server):

git clone https://github.com/alicembranos/microservices-backend.git
cd microservices
cp spotify/.env.example spotify/.env
cp users/.env.example users/.env
cd ./spotify
npm install install
cd ./users
npm install install

2. Open two terminals and start the data API server

cd spotify
npm run start
cd users
npm run start
  • Docker Container - There is available Docker container and Docker Composer if you would like to run many NodeJS Microservices. Build API Microservice by using following command:
docker-compose up -d --build

See Dockerfile and docker-compose.yml for more details.

๐Ÿ’ป Repository Structure

This is a monorepo so contains multiple discreet but loosely dependant projects. The top levels of the repository directory tree are laid out as follows

/
โ”œโ”€โ”€ spotify                 Base microservice folder
โ”‚   โ”œโ”€โ”€ node_modules        Node folder (npm install)
โ”‚   โ””โ”€โ”€ src                 Source microservice architecture folder
โ”‚       โ”œโ”€โ”€ api                 Router endpoints
โ”‚       โ”œโ”€โ”€ config              Projet configuration file
โ”‚       โ”œโ”€โ”€ database            Database seeds and connection logic
โ”‚       โ”œโ”€โ”€ documentation       Swagger documentation
โ”‚       โ”œโ”€โ”€ interfaces          Typescript interfaces
โ”‚       โ”œโ”€โ”€ models              Database Models
โ”‚       โ”œโ”€โ”€ repository          Database Methods
โ”‚       โ”œโ”€โ”€ services            Microservices controllers
โ”‚       โ”œโ”€โ”€ types               Global types
โ”‚       โ”œโ”€โ”€ utils               Standard functions
โ”œโ”€โ”€ users                   Base microservice folder
โ”‚   โ”œโ”€โ”€ node_modules        Node folder (npm install)
โ”‚   โ””โ”€โ”€ src                 Source microservice architecture folder
โ”‚       โ”œโ”€โ”€ api                 Router endpoints
โ”‚       โ”œโ”€โ”€ config              Projet configuration file
โ”‚       โ”œโ”€โ”€ database            Database seeds and connection logic
โ”‚       โ”œโ”€โ”€ documentation       Swagger documentation
โ”‚       โ”œโ”€โ”€ interfaces          Typescript interfaces
โ”‚       โ”œโ”€โ”€ models              Database Models
โ”‚       โ”œโ”€โ”€ repository          Database Methods
โ”‚       โ”œโ”€โ”€ services            Microservices controllers
โ”‚       โ”œโ”€โ”€ types               Global types
โ”‚       โ”œโ”€โ”€ utils               Standard functions
โ””โ”€โ”€ proxy                   Nginx configuration file

๐Ÿ… Fundamentals

๐Ÿ“ƒ API Reference & Data Model

(1) Users - http://localhost/users (With docker) | http://localhost:4001 (Locally)

Endpoint HTTP Method CRUD Method Result
/user GET READ All users
/signup POST CREATE Sign up user
/signin POST CREATE Login User
/user/{id} GET READ Get user
/user/{id} PATCH UPDATE Update user
/user/{id} DELETE DELETE Delete user

Swagger / OpenAPI

Swagger is generated dynamically by the Node implementation, using express-swagger-generator The data API server will expose this out using Swagger UI. http://localhost:4001/doc || http://localhost/users/doc

(2) Spotify - http://localhost/spotify (With docker) | http://localhost:4002 (Locally)

Endpoint HTTP Method CRUD Method Result
ALBUMS ----------------- ------------- ------------- ----------------------------------
/album GET READ All albums
/album/{id} GET READ Get album
/album/library PUT UPDATE Add/Remove album from library
ARTISTS ---------------- ------------- ------------- ----------------------------------
/artist GET READ All artists
/artist/{id} GET READ Get artist
/artist/library PUT UPDATE Add/Remove artist from library
TRACKS ----------------- ------------- ------------- ----------------------------------
/track GET READ All tracks
/track POST CREATE Upload track
/track/{id} GET READ Get track
/track/library PUT UPDATE Add/Remove track from library
PLAYLISTS -------------- ------------- ------------- ----------------------------------
/playlist GET READ All playlists
/playlist POST CREATE Create playlist
/playlist/{id} GET READ Get playist
/playlist/{id} PATCH UPDATE Update playlist
/playlist/{id} DELETE DELETE Delete playlist
/playlist/tracks/{id} PATCH UPDATE Add track to playlist
/playlist/tracks/{id} DELETE DELETE Delete track to playlist
SEARCH ----------------- ------------- ------------- ----------------------------------
/search?key=value GET READ Search artists/albums/tracks

Swagger / OpenAPI

Swagger is generated dynamically by the Node implementation, using express-swagger-generator The data API server will expose this out using Swagger UI. http://localhost:4002/doc || http://localhost/spotify/doc


Data Model

  • Users Service: There are one main model, Users.
User {
    username: string;
    email: string;
    password: string;
    image: string;
    genres: string[];
    phone: string;
    playlists: Partial<IPlaylist>[];
    albums: Partial<IAlbum>[];
    artists: Partial<IArtist>[];
    likedSongs: Partial<ITrack>[];
    chats: IChat[];
}
  • Spotify Service: There are four main models, Albums, Artists Tracks, Playlists.
Album {
    _id: string;
	image: string;
	title: string;
	releaseDate: Date;
	totalTracks: number;
	tracks: ITrack[];
	artist: IArtist;
}

Artist {
	_id: string;
	image: string;
	followers: number;
	name: string;
	popularity: number;
	genres: string[];
	tracks: ITrack[];
	albums: IAlbum[];
}

Track {
	_id: string;
	title: string;
	duration: number;
	trackNumber: number;
	trackAudio: string;
	album: IAlbum;
}

Playlist {
	_id: string;
	title: string;
	description: string;
	image: string;
	isPublic: boolean;
	tracks: ITrack[];
	userId: string;
}

๐Ÿ“ƒ JWT refresh token & blacklist token Authorization Process

architecture

Token based authentication system using jwt with access, refresh token mechanism and token blacklist once the user has logged out.

Everytime the login or register flow is success, it will produce a token with an expire time of 15 min and a refresh token with an expire time of 30 days. The refresh token will be store in the memory cache using redis with a key - value = userId - refreshToken. Every private endpoint will pass through the authentication middleware to validate the authorization. If success , a req.user object will be create.

If the client make a htttp request with an expired token, it must fetch to /token endpoint to get a new token. This /token endpoint will check if the userId key exists in the memory cache and if it's true, the server will respond with a new token.

On the logout process, the refrech token of the logged out user will remove from the memory cache, and the token will be add to the blacklist in the memory cache to avoid using still validated tokens.

๐Ÿ–‡๏ธ Contributing

If you want to contribute, please fork the repository, create a new branch whit your contribution, and push the branch as a pull requests.

โœจ Contributors

Thanks goes to these superb people

microservices-backend's People

Contributors

alicembranos avatar rogerolivedelgado avatar jcarloscabello1991 avatar

Forkers

juliomc23

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.