Ported from bookstore-api to Go.
This project is a backend system for managing an online bookstore. It allows customers to browse, search, and purchase books while providing functionality for inventory and customers management for admins.
- Testing: Unit and integration tests are run on GitHub Actions every time a commit is pushed.
- CI/CD Pipeline: A GitHub Actions workflow is set up to build the application to see if it compiles successfully.
- Roles: Two user roles are supported: customer and admin.
- Customer Authentication: Customers can register, log in, and update their profile information.
- Admin Management: Admins can manage customers' accounts.
- Add Books: Admins can add new books to the inventory via an API endpoint.
- Update Books: An endpoint is available for admins to update existing book details.
- Retrieve Books: Users can retrieve a list of books with filtering options; genre, author, and year.
- Add to Cart: Customers can add books to their shopping cart.
- Update Cart: Customers can update the quantity or remove items from their cart.
- Total Price Calculation: The total price of items in the cart is automatically calculated each time the cart is updated.
- Place Orders: Customers can place orders to checkout their carts.
- Inventory Deduction: The ordered quantity is deducted from the inventory upon order placement.
- Payment Gateway: Midtrans payment gateway is integrated to process order payments. Customers can pay using QRIS.
- Email Notification: Customers receive an email confirmation after placing an order.
- View and Manage Inventory: Admins can view and manage the current inventory.
- Validation and Error Handling: All API endpoints have proper validation and error handling.
- Sensitive Information: Sensitive information is securely encrypted.
- Logging: Important events and errors are logged with Morgan.
- Monitoring: Basic monitoring for API performance is implemented using Prometheus.
- Unit Tests: Important API endpoints (book and user) have unit tests.
The API is documented using Swagger and can be accessed at:
http://localhost:9000/swagger/index.html
golang-rest-api-template/
|-- bin/
|-- cmd/
| |-- server/
| |-- main.go
|-- pkg/
| |-- api/
| |-- handler.go
| |-- router.go
| |-- models/
| |-- user.go
| |-- database/
| |-- db.go
|-- scripts/
|-- Dockerfile
|-- go.mod
|-- go.sum
|-- README.md
-
bin/
: Contains the compiled binaries. -
cmd/
: Main applications for this project. The directory name for each application should match the name of the executable.main.go
: The entry point.
-
pkg/
: Libraries and packages that are okay to be used by applications from other projects.api/
: API logic.handler.go
: HTTP handlers.router.go
: Routes.
models/
: Data models.database/
: Database connection and queries.
-
scripts/
: Various build, install, analysis, etc., scripts.
- Go 1.15+ as the programming language.
- gin as the HTTP web framework.
- Docker and Docker Compose for containerization.
- PostgreSQL for the database.
- GORM as the ORM.
- Midtrans for the payment gateway.
- Swagger for API documentation.
- Clone the repository
git clone https://github.com/mufidu/bookstore-api-go.git
- Navigate to the directory
cd bookstore-api-go
- Build and run the Docker containers
make setup && make build && make up
You can set the environment variables in the .env
file. Here are some important variables:
POSTGRES_HOST
POSTGRES_DB
POSTGRES_USER
POSTGRES_PASSWORD
POSTGRES_PORT
JWT_SECRET
API_SECRET_KEY
MIDTRANS_SANDBOX_SERVER_KEY
MIDTRANS_SANDBOX_CLIENT_KEY