This is a modular and scalable REST API framework for ecommerce built using Go, Chi router, GORM ORM, PostgreSQL, Redis, and JWT authentication. It supports features like user registration, product management, comments, ratings, dynamic roles, and caching.
This application serves as a base framework for building large-scale REST APIs. It includes:
- User Management: Registration, login, and role-based access control.
- Product Management: CRUD operations with pagination, filtering, and promoted products.
- Comment System: Users can submit comments and ratings; admins can publish or reject comments.
- Dynamic Roles: Admins can create, update, or delete roles dynamically.
- Caching: Redis is used to cache frequently accessed data (e.g., product lists, published comments).
- Scalability: Modular design ensures easy extension and maintenance.
- Authentication: JWT-based authentication with role-based access control.
- Authorization: Fine-grained permissions for different user roles (
admin
,seller
,user
). - Pagination: Efficiently fetch large datasets with pagination.
- Caching: Use Redis to cache API responses and query results.
- Dynamic Roles: Admins can create new roles dynamically.
- Logging: Structured logging with
zap
for better observability. - Graceful Shutdown: Ensures no data loss during shutdowns.
- Docker Support: Easily deploy the application using Docker and Docker Compose.
The application follows a clean and modular architecture:
- Handlers: Handle HTTP requests and responses.
- Services: Encapsulate business logic.
- Repositories: Interact with the database using GORM.
- Cache: Centralized caching mechanism using Redis.
- Middleware: Handles authentication, authorization, and logging.
- Models: Define database schemas and validation rules.
cmd
: Entry point of the application.config
: Load configuration settings from.env
.internal
: Core application logic (handlers, services, repositories, models).pkg
: Utility packages (cache, logger, response, validation, etc.).docker-compose.yml
: Defines the application's services (app, PostgreSQL, Redis).
Before running the application, ensure you have the following installed:
- Go: Version 1.20 or higher.
- Docker: For running the application in containers.
- Make: Optional, but useful for running setup scripts.
git clone https://github.com/alikhalafnejad/shopline.git
cd shopline
Run the following command to install Go dependencies:
go mod tidy
Create a .env
file in the root directory and configure the required variables:
DEBUG=true
# Database Settings
DB_HOST=localhost
DB_PORT=5432
DB_USER=myuser
DB_PASSWORD=mypassword
DB_NAME=mydb
# Redis Settings
REDIS_ADDR=localhost:6379
REDIS_PASSWORD=
REDIS_DB=0
# JWT Settings
JWT_SECRET_KEY=my_secret_key
# Pagination Defaults
DEFAULT_PAGE=1
DEFAULT_LIMIT=10
# Cache TTL
CACHE_TTL_PRODUCT=5m
CACHE_TTL_COMMENT=10m
Apply database migrations to create tables:
go run migrations/migrate.go
You can start the application using Docker Compose:
docker-compose up --build
Alternatively, run it locally:
go run cmd/main.go
The server will start on http://localhost:8080
.
Register a new user by sending a POST request:
POST /api/v1/register
Content-Type: application/json
{
"username": "john_doe",
"email": "[email protected]",
"password": "securepassword123"
}
Login to get a JWT token:
POST /api/v1/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "securepassword123"
}
- Get Products: Fetch paginated products.
GET /api/v1/products?page=1&limit=10 Authorization: Bearer <token>
- Create Product: Add a new product (requires seller role).
POST /api/v1/seller/products Authorization: Bearer <seller_token> Content-Type: application/json { "name": "Laptop", "price": 999.99, "category_id": 1 }
- Add Comment: Submit a comment for a product.
POST /api/v1/products/1/comments Authorization: Bearer <user_token> Content-Type: application/json { "text": "Great product!", "rating": 5 }
- Publish Comment: Approve a comment (requires admin role).
PUT /api/v1/admin/comments/1/publish Authorization: Bearer <admin_token>
- Reject Comment: Reject a comment (requires admin role).
PUT /api/v1/admin/comments/1/reject Authorization: Bearer <admin_token>
Admins can create new roles dynamically:
POST /api/v1/admin/roles
Authorization: Bearer <admin_token>
Content-Type: application/json
{
"name": "moderator",
"description": "Can approve or reject comments."
}
The application uses environment variables for configuration. You can customize these values in the .env
file.
Variable | Default Value | Description |
---|---|---|
DEBUG |
true |
Enable debug logging. |
DB_HOST |
localhost |
PostgreSQL host address. |
DB_PORT |
5432 |
PostgreSQL port. |
DB_USER |
myuser |
PostgreSQL username. |
DB_PASSWORD |
mypassword |
PostgreSQL password. |
DB_NAME |
mydb |
PostgreSQL database name. |
REDIS_ADDR |
localhost:6379 |
Redis address. |
REDIS_PASSWORD |
Redis password (leave empty if none). | |
REDIS_DB |
0 |
Redis database index. |
JWT_SECRET_KEY |
my_secret_key |
Secret key for JWT token generation. |
DEFAULT_PAGE |
1 |
Default page number for pagination. |
DEFAULT_LIMIT |
10 |
Default limit for pagination. |
CACHE_TTL_PRODUCT |
5m |
Cache expiration time for products. |
CACHE_TTL_COMMENT |
10m |
Cache expiration time for comments. |
Use Docker Compose to build and run the application along with PostgreSQL and Redis:
docker-compose up --build
Once the containers are running, access the application at:
- API:
http://localhost:8080
- PostgreSQL:
postgres://myuser:mypassword@localhost:5432/mydb
- Redis:
redis://localhost:6379
To stop the application:
docker-compose down
To contribute to this project:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes and push them to your fork.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
If you have any questions or need assistance, feel free to reach out:
- Email: [email protected]
- GitHub: @alikhalafnejad