A RESTful API for e-commerce backend with user authentication, product management, cart operations, and order processing.
- User Authentication: JWT-based authentication with user registration and login
- Product Management: CRUD operations for products (admin only)
- Shopping Cart: Add and remove items from cart
- Order Processing: Place, deliver, and reject orders
- Role-based Access: Admin and user roles with different permissions
- Swagger Documentation: Complete API documentation
- Framework: Gin (Go web framework)
- Database: PostgreSQL with GORM ORM
- Authentication: JWT tokens
- Documentation: Swagger/OpenAPI 3.0
- Environment: Environment variables with godotenv
- Go 1.24.1 or higher
- PostgreSQL database
- Git
- Clone the repository:
git clone <repository-url>
cd go-backend-starter- Install dependencies:
go mod tidy- Set up environment variables:
Create a
.envfile in the root directory:
PORT=8080
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=your_db_name
JWT_SECRET=your_jwt_secret_key- Run the application:
go run main.goThe server will start on http://localhost:8080
Once the application is running, you can access the interactive API documentation at:
http://localhost:8080/swagger/index.html
This provides a complete interactive documentation where you can:
- View all available endpoints
- Test API calls directly from the browser
- See request/response schemas
- Authenticate with JWT tokens
POST /users/register- Register a new userPOST /users/login- User login
GET /users/mine- Get current user accountPUT /users/update/user/{id}- Update user (admin only)DELETE /users/delete/myAccount- Delete current user accountGET /users/all- Get all users (admin only)
GET /products/all- Get all products (public)GET /products/{id}- Get specific product (public)POST /products/create- Create product (admin only)PUT /products/update/{id}- Update product (admin only)DELETE /products/delete/{id}- Delete product (admin only)
POST /carts/add- Add item to cartDELETE /carts/remove- Remove item from cart
POST /orders/place-order- Place new orderPUT /orders/deliver- Deliver order (admin only)DELETE /orders/reject- Reject order (admin only)POST /orders/pay- Pay for an order (virtual payment)
The API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:
- Register or login to get a JWT token
- Include the token in the Authorization header:
Authorization: Bearer <your_jwt_token>
id: Primary keyname: User's full nameemail: Unique email addresspassword: Hashed passwordrole: User role (user/admin)cart: Associated cart ID
id: Primary keyname: Product namedescription: Product descriptionprice: Product pricestock_qty: Available stock quantity
id: Primary keyuser_id: Associated user IDcart_items: Array of cart items
id: Primary keyuser_id: Associated user IDstatus: Order status (PENDING/DELIVERED)cart: Associated cart ID
id: Primary keyorder_id: Associated order IDamount: Payment amountstatus: Payment status (e.g., PAID)payment_method: Payment method (e.g., virtual_card)transaction_id: Transaction referencecreated_at,updated_at: Timestamps
If you modify the API endpoints or add new ones, regenerate the Swagger documentation:
~/go/bin/swag initThis will update the documentation files in the docs/ directory.
├── main.go # Application entry point
├── go.mod # Go module file
├── go.sum # Go module checksums
├── docs/ # Generated Swagger documentation
├── api/ # API controllers
│ ├── users/ # User management
│ ├── products/ # Product management
│ ├── carts/ # Cart operations
│ └── orders/ # Order processing
├── database/ # Database models and connection
├── middleware/ # HTTP middleware
├── routes/ # Route definitions
└── utils/ # Utility functions
This project is licensed under the Apache 2.0 License.
This project uses a simulated (virtual) payment system for demonstration and development purposes. When a user pays for an order, the backend calculates the total amount, generates a fake transaction ID, and marks the order as paid. No real money is transferred and no external payment provider is contacted.
How to extend for real payments:
- Replace the logic in
utils/payment.gowith integration to a real payment provider (e.g., Stripe, PayPal). - Ensure to handle payment confirmation, webhooks, and error scenarios securely.
- Never store real payment credentials or secrets in the codebase; always use environment variables.