A simple RESTful Ecommerce API written in Go programming language. The structure of this project was inspired by Bagas Hizbullah through this article:
https://dev.to/bagashiz/building-restful-api-with-hexagonal-architecture-in-go-1mij
It uses the Go standard library with Go-chi as the lightweight router and PostgreSQL as the database with pgx as the driver and Squirrel as the query builder.
The development process relies heavily on Make. Make is used to manage dependencies, run migrations, build the application, and run tests.
-
Ensure you have Go 1.23 or higher and Make installed on your machine:
go version && make --version
If they are not installed, install them using the Links above for your specific architecture.
-
Create a copy of the
config-sample.yml
file and rename it toconfig.yml
:cp config-sample.yml config.yml
Update configuration values as needed.
-
Update the
Makefile
variableCONFIG_FILE
to point to yourconfig.yml
file.
To set up the database, you have three options:
Option 1: Using Docker Compose
You can use Docker Compose to start a Postgres database container. To do this:
- Install Docker and Docker Compose on your machine.
- Run the following command to start the database container:
make service-up
You can use make service-down
to stop the postgres container.
- The database will be available at
localhost:5433
. The port5433
was used to avoid conflict with any locally installed postgres instance, but you can modify it in yourconfig.yml
file.
Option 2: Using a Local Postgres Database
If you already have a Postgres database installed locally, you can use it instead:
- Ensure your local Postgres instance is running and accessible.
- Ensure the username, password and database in your
config.yml
file is created for your local Postgres instance. You can usepsql
to connect to your local instance using the default user:psql -d postgres
- Update the
PORT
environment variable in theconfig.yml
file to point to the port of your local Postgres instance database.
-
Run database migrations using (Optional: It also runs on application startup):
make migrate-up
You can drop all tables in the database using
make migrate-down
. You can ignore the migrate commands because the application automatically runs your database migrations on startup. -
Install all dependencies necessary for running the commands to start up the application on main:
make install
Some of the commands are written for MacOS ARM 64 architecture. Update them accordingly for any other operating system.
-
Run the project in development mode using:
make dev
-
Run the project in production mode using:
make start
API documentation can be found in docs/
directory. To view the documentation, open the browser and go to http://127.0.0.1:8080/swagger/index.html
. The documentation is generated using swaggo with http-swagger middleware.