golang-store-api

module
v0.0.0-...-f80c191 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2024 License: MIT

README

Golang Store API

Production-ready E-commerce backend, REST API in Go using JWT authentication & MySQL.

API documentation

For auth guarded endpoints, you have to hit the /login endpoint and retrieve the JWT token.

Then, you can use that token string and place it as a query param ?token= or in the Authorization header.

Auth
Method Endpoint Description Request Body Response Authentication
POST /login Logs in a user and returns a JWT. Email and password 200 OK / 400 Bad Request No
POST /register Registers a new user. First name, last name, email, password 201 Created / 400 Bad Request No
Users
Method Endpoint Description Request Body Response Authentication
GET /users Retrieves a list of all users. N/A 200 OK / 500 Internal Server Error Yes
GET /users/{id} Retrieves a user by their ID. User ID 200 OK / 400 Bad Request / 500 Internal Server Error Yes
Products
Method Endpoint Description Request Body Response Authentication
GET /products Retrieves a list of all products. N/A 200 OK / 500 Internal Server Error No
GET /products/{id} Retrieves a product by its ID. Product ID 200 OK / 400 Bad Request / 404 Not Found / 500 Internal Server Error No
POST /products Creates a new product (Admin only). Name, description, price, and other product details 201 Created / 400 Bad Request / 500 Internal Server Error Yes
Cart/Orders
Method Endpoint Description Request Body Response Authentication
POST /cart/checkout Checks out the user's cart and creates an order. List of product items in the cart 200 OK / 400 Bad Request / 500 Internal Server Error Yes

Getting started

Running locally

Make sure to have Go 1.22+, MySQL and Make installed.

Steps:

  1. Create a database named ecommerceDb in MySQL

  2. Run make migrate-up to create database tables.

  3. Run make run to start the server.

The project requires environment variables to be set. You can find the list of required variables in the .env.template file.

Database migrations

We are using golang-migrate to ease all database migrations.

Create a migration

To create a new database migration, run:

make migration <migration-name>

Then, you can find the create (up) and teardown (down) scripts in /cmd/migrate/migrations.

Applying all database migration

To apply all existing database migrations, run:

make migrate-up
Turning down database migrations

To remove all database migrations, run:

make migrate-down

Directories

Path Synopsis
cmd
api
service

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL