This is a simple online shop backend written in Go. It is a RESTful API that allows you to manage products, customers, orders and order items.
My personal goal with this project is to learn about Go and how to build a RESTful API with it.
- Go 1.21+
- PostgreSQL 16+
- libvips 8.15.2
This project uses libvips to compress and convert images to WebP format. Make sure to install it on your system.
- Clone the repository
- Run
go mod download
to download all dependencies - Create a
.env
file in the root directory and add the following environment variables:
JWT_SECRET=secret
JWT_EXPIRATION=duration ; value for `time.ParseDuration`, default is 24h
export DATABASE_URL=postgres://username:password@localhost:1900/dbname
export SERVER_HOST=0.0.0.0
export SERVER_PORT=8080
- Install
docker-compose
and rundocker-compose up -d
to install and start the PostgreSQL database - Run
go run .
to start the server
Note: If you have
make
util installed, you can also runmake setup
to automatically install the dependencies, create the.env
file and start the database.
- go-task used to simplify the development process by providing a hot-reload feature.
- dbmate is used to manage the database migrations.
The project is organized into the following packages for clarity and maintainability:
This package contains all the API handlers and routes.
This package contains the configuration for the application. It reads the environment variables and convert into an internal configuration struct.
All database-related code is contained in this package, including the initialization of the database connection and the database models.
This package contains all the database migrations. The dbmate tool is used to organize and execute these migrations.
This package contains all the tools and utilities used in the project. For example, the image
package contains the image compression and conversion logic.
API versioning is used in this project, with the current version being v1. To see the full list of available endpoints, run the server and navigate to /api/v1/docs in your browser. Below is a summary of the main endpoints:
POST /api/v1/auth/login
- Authenticate a customer or employee and receive a JWT token.POST /api/v1/auth/customer/signup
- Register a new customer accountPOST /api/v1/auth/employee/signup
- Register a new employee accountPOST /api/v1/auth/verify
- Verify a JWT token
GET /api/v1/products
- Get all products (public access)POST /api/v1/products
- Create a new product (admin users only)PUT /api/v1/products/{id}
- Update a product (admin users only)DELETE /api/v1/products/{id}
- Delete a product (admin users only)
GET /api/v1/orders
- Get all orders (admin users or customers only)POST /api/v1/orders
- Create a new order (customers only)PUT /api/v1/orders/{id}
- Update an order (admin users or customers only)
POST /api/v1/file/upload
- Upload a new file. Files stored as a compressed WEBP file. Supported formats are PNG, JPEG, JPG, and WEBP (authenticated users only).GET /static/files/{filename}
- Receive a file by its filename