Skip to content

This repository contains code for a loyalty program API that'll allow customers to earn points for purchases and redeem them for rewards.

License

Notifications You must be signed in to change notification settings

Njoguu/loyalty-program-api

Repository files navigation

Loyalty Points API

LICENSE Go Github Issues Github pull requests
Build CodeQL Analysis
GitHub Pages

This is a loyalty points API built with GO and the Gin web framework. The API allows customers to earn points for purchases, and redeem those points for rewards.

Requirements

  • GO (version 1.18 or higher)

Supported databases

  • PostgreSQL

Note loyalty-program-api uses GORM as its ORM

Features

The API has put in place the following features:

  • Built on top of Gin
  • Uses the supported database without writing any extra configuration files
  • Reading Environment variables using godotenv
  • Caching responses
  • Logging
  • Follows CORS policy
  • DB Migration Support
  • Comprehensive Error Handling in API Services
  • Basic auth
  • Password hashing with bcrypt
  • Simple firewall (whitelist/blacklist IP)
  • Webhook Integration for Real-time Deployment Status Notifications
  • Request data validation
  • Email verification (sending verification email)
  • Forgot password recovery
  • Render HTML templates
  • Forward error logs and crash reports.
  • User logout (Send an expired cookie to user’s browser or client which invalidates the user’s ‘session’)

Project Structure

The project follows the following directory structure:

.
├── controllers
│   ├── admin
│   │   ├── admin-auth.go
│   │   └── admin-endpoints.go
│   ├── get-products.go
│   ├── googleauth.go
│   ├── pass-reset-controller.go
│   ├── points.go
│   ├── transactions-history.go
│   └── user-auth.go
├── db
│   ├── migrations
│   │   ├── 000001_init_schema.down.sql
│   │   └── 000001_init_schema.up.sql
├── docs
│   ├── admins
│   │   └── admins.apib
│   ├── auth.apib
│   ├── index.apib
│   ├── index.html
│   ├── points.apib
│   ├── products.apib
│   ├── reset-password.apib
│   └── transaction-history.apib
├── middlewares
│   └── caching.go
│   ├── logger.go
│   └── middlewares.go
├── migrate
│   └── migrate.go
├── models
│   ├── admins.go
│   ├── products.go
│   ├── setup.go
│   ├── transactions.go
│   └── user.go
├── templates
│   ├── base.html
│   ├── passwordReset.html
│   ├── styles.html
│   ├── templates.html
│   └── verificationCode.html
├── utils
│   ├── mail
│   │   ├── email.go
│   │   └── encode.go
│   ├── token
│   │   └── token.go
│   ├── googleOAuth.go
│   └── utils.go
├── .gitignore
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── docker-compose.yml
├── env.sample
├── go.mod
├── go.sum
└── main.go

Environment Installation

Development Setup Instructions

Follow the steps below to set up the development environment for the Loyalty Program API:

  1. Clone the Loyalty Program API repository from the version control system of your choice (e.g., GitHub).
git clone https://github.com/Njoguu/loyalty-program-api.git
cd loyalty-program-api
  1. Copy the example environment file and update the configuration variables as needed.
cp .env.example .env
  1. Open the .env file in a text editor and provide the necessary values for the environment variables. Make sure to set the database connection details, JWT secret, and any other required variables.

  2. Start the API and its dependencies using Docker Compose.

docker-compose up

Docker Compose will build and start the Loyalty Program API, along with the required database and any other defined services. The API will be accessible at http://localhost:8000.

  1. Make changes to the API code as needed. The API server will automatically reload whenever code changes are detected, allowing for a seamless development experience.

Commands

  • Application Lifecycle
    • Install dependencies
    $ go get . || go mod || make install
    • Build application
    $ go build -o loyalty-program-api || make build
    • Start application server in development
    $ go run main.go | make start
  • Docker Lifecycle
    • Start postgres container:
    $ make postgres
    • Create simple_bank database:
    $ make createdb
    • Create a new db migration:
    $ make create-migrations
    • Run db migration up all versions:
    $ make migrateup
    • Run db migration down all versions:
    $ make migratedown
    • Build container
    $ docker-compose build | make dcb
    • Run container
    $ docker-compose up -d --build | make dcu
    • Stop container
    $ docker-compose down | make dcd

API Routes

User Authentication

Users have the following routes available for their authentication:

Path Method Required JSON Header Description
/api/auth/register POST username, firstname, lastname, gender, email, password, city, phone_number Create User account and send activation email
/api/auth/verify-email/{secret_code} GET Verify email of correspondent account and grant login access
/api/auth/login POST email, password Validate logging in of user
/api/auth/logout GET Authorizaiton: "Bearer token" Invalidate the user’s ‘session’.

Documentation

The documentation for this project is generated using aglio, an API Blueprint renderer that supports multiple themes and outputs static HTML that can be served by any web host.

Generating Docs

To generate the documentation for this project, follow these steps:

  1. Install Aglio by running npm install -g aglio in your terminal.

  2. Once installed, navigate to the root of the project and run the command aglio -i docs/index.apib -o docs/index.html.

  3. This will generate the API documentation in the docs folder.

  4. Open the docs/index.html file in your web browser to view the documentation.

Updating Docs

If you make changes to the API documentation, you'll need to regenerate the docs by following the steps above.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

This repository contains code for a loyalty program API that'll allow customers to earn points for purchases and redeem them for rewards.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published