About Project

Simple Library Management System

Features

Add Book
Get Book Info
Get All Books Info
Remove Book
Users

Technologies

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

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

Run

go run main.go

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

docker-compose up

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>


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 Dockerizing Fiber with Postgresql


GitHub

View Github