Production-ready E-commerce backend, REST API in Go using JWT authentication & MySQL.
For auth guarded endpoints, you have to hit the
/loginendpoint and retrieve the JWT token.Then, you can use that token string and place it as a query param
?token=or in theAuthorizationheader.
| 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 |
| 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 |
| 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 |
| 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 |
Make sure to have Go 1.22+, MySQL and Make installed.
Steps:
-
Create a database named
ecommerceDbin MySQL -
Run
make migrate-upto create database tables. -
Run
make runto start the server.
The project requires environment variables to be set. You can find the list of required variables in the .env.template file.
We are using golang-migrate to ease all database migrations.
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.
To apply all existing database migrations, run:
make migrate-upTo remove all database migrations, run:
make migrate-down