Skip to content

Library Books REST API built using GO-Fiber, GORM and Postgresql along with JWT Session Management and Redis Cache

Notifications You must be signed in to change notification settings

sswastik02/GoLibraryAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About Project

Simple Library Management System

Features


Book : 
    Add Book          (Administrator)
    Get Book Info
    Get All Books Info
    Remove Book       (Administrator)

Users : 
    User Signup
    User Signin
    Refresh JWT token pair
    
    Admin Signup      (Administrator)
    Give Admin Role   (Administrator)
    Revoke Admin Role (Administrator) [Blacklisting JWT access token for its lifetime]
    Delete any User   (Administrator) [Blacklisting JWT access token for its lifetime]

Issue : 
    Add Issue    (Administrator)
    Get Issues
    Delete Issue (Administrator)

Technologies

Backend Framework   : Go-Fiber
Database            : Postresql
ORM                 : GORM
Password Hashing    : HS256
Session Management  : JWT Token
Cache               : Redis

Setup

Local

Clone the project and install required go packages (replace .env contents with .env.dev)

You need postgres to be installed on the system

git clone https://github.com/sswastik02/Books-API
go mod tidy # or go mod download

Note : go mod tidy ensures that the go.mod file matches the source code in the module, you can also go get each library individually


Create Admin

go run main.go -createadmin

Run Server

go run main.go -runserver

Docker

Alternatively, docker container can be built from the docker-compose.yml file (replace .env contents with .env.prod)

You will need docker and docker-compose for this

Create Admin

docker exec -it <go_fiber_container_name> go run main.go -createadmin

go fiber container name is the name of the container running the go fiber backend that is running on your system. You can view it by running docker ps

Run Server

docker-compose up --build

Note : You need to make sure nothing is already running on the ports 8000 and 5432. If there is you need to stop them

Steps followed to build the Project Initially

Initialise Module for the project

We will add a go.mod file with link to our repo with the command in the root directory:

go mod init github.com/sswastik02/Books-API

Basic Directory Structure

Created the files and folders manually, forming the following directory tree

    Books-API
    ├── go.mod
    ├── main.go
    ├── .env
    ├── models/
    └── storage/

Writing to main.go

Next we start writing to the main.go file for :

  • Import Neccessary Go Libraries(Such as Fiber and Gorm)
  • Importing .env
  • Connect with Database
  • Initialise Fiber Framework
  • Write routes and functions

Writing Models

We write the book model required to store in the database in the models folder along with the migrate method

Writing storage

This Step includes making configuration to connect with postgresSQL

Writing .env file

You need to create a postgres database on your localhost before writing the env file

In the following, keep in mind the < angular brackets > are to denote variables you have to set yourself. In the actual, omit the <>

The basic structure of the file looks like :


DB_HOST=localhost
DB_PORT=5432
DB_USER=<username>
DB_PASS=<password>
DB_NAME=<dbname>
DB_SSLMODE=disable
JWT_SECRET=<jwtsecret>

The above .env file is outdated as it was during the inital steps of the project. You can have a look at env.dev or env.prod for reference


Resources

GoLang Setup and Tutorial
Postgres Setup for Ubuntu 20.04
Playlist with GO-Fiber and PostgresQL GORM models
Posgresql Basic create user and database
Implementing Password Authentication
Implementing JWT in Fiber
Implemeting JWT with refresh token
Dockerizing Fiber with Postgresql
Setting up Redis on Ubuntu
Overview of the GORM object relational mapper


About

Library Books REST API built using GO-Fiber, GORM and Postgresql along with JWT Session Management and Redis Cache

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published