Zion API

π Overview
Zion API is a modern, high-performance RESTful API built with Go and PostgreSQL. It follows Clean Architecture principles to ensure maintainability, testability, and scalability. The API provides robust endpoints for managing products and customers with authentication powered by Clerk.
β¨ Features
- Clean Architecture: Domain-driven design with clear separation of concerns
- RESTful API: Well-designed endpoints following REST principles
- Authentication: Secure JWT-based authentication with Clerk
- Database: PostgreSQL for reliable data persistence
- Query Builder: Type-safe SQL queries with Squirrel
- Migration Tools: Database versioning and migrations
- Data Import: Tools for importing legacy data from MySQL
- CORS Support: Cross-Origin Resource Sharing enabled
- Pagination: Efficient data retrieval with pagination support
- Error Handling: Comprehensive error handling and reporting
ποΈ Architecture
The project follows Clean Architecture principles with the following layers:
zion-api/
βββ cmd/ # Application entry points
β βββ api/ # Main API server
β βββ scripts/ # Utility scripts (data migration, etc.)
βββ internal/ # Private application code
β βββ domain/ # Business entities and interfaces
β βββ usecase/ # Business logic
β βββ controller/ # HTTP request handlers
β βββ middleware/ # HTTP middleware
β βββ infra/ # Infrastructure implementations
β βββ database/ # Database connection and migrations
β βββ repository/ # Data access implementations
βββ Makefile # Build and development commands
π οΈ Technology Stack
- Go: Fast and efficient programming language
- Gin: High-performance HTTP web framework
- PostgreSQL: Advanced open-source relational database
- pgx: PostgreSQL driver and toolkit
- Squirrel: Fluent SQL generation library
- JWT: JSON Web Tokens for authentication
- Clerk: Authentication and user management
- godotenv: Environment variable management
- testify: Testing toolkit
π¦ Getting Started
Prerequisites
- Go 1.18 or higher
- PostgreSQL 13 or higher
- Make (optional, for using Makefile commands)
Installation
-
Clone the repository:
git clone https://github.com/deividr/zion-api.git
cd zion-api
-
Install dependencies:
go mod download
-
Set up environment variables:
cp .env.example .env
# Edit .env with your configuration
-
Run database migrations:
make migration_up
-
Start the server:
make run
The API will be available at http://localhost:8000.
π API Documentation
Authentication
All protected endpoints require a valid JWT token in the Authorization header:
Authorization: Bearer <your_jwt_token>
Endpoints
Products
| Method |
Endpoint |
Description |
| GET |
/products |
List all products (with pagination) |
| GET |
/products/:id |
Get product by ID |
| POST |
/products |
Create a new product |
| PUT |
/products/:id |
Update a product |
| DELETE |
/products/:id |
Delete a product (soft delete) |
Customers
| Method |
Endpoint |
Description |
| GET |
/customers |
List all customers (with pagination) |
| GET |
/customers/:id |
Get customer by ID |
| POST |
/customers |
Create a new customer |
| PUT |
/customers/:id |
Update a customer |
| DELETE |
/customers/:id |
Delete a customer (soft delete) |
π§ͺ Testing
Run the test suite:
make test
Run tests with coverage:
make test_coverage
π οΈ Development
Makefile Commands
| Command |
Description |
make run |
Start the API server |
make build |
Build the application |
make test |
Run tests |
make migration_up |
Apply database migrations |
make migration_down |
Rollback database migrations |
make load_products |
Import products from legacy database |
make load_customers |
Import customers from legacy database |
Adding a New Entity
- Define the entity in the domain layer
- Create repository interfaces in the domain layer
- Implement the repository in the infrastructure layer
- Create use cases in the usecase layer
- Create controllers in the controller layer
- Register routes in the main.go file
π Data Migration
The project includes scripts to migrate data from a legacy MySQL database to PostgreSQL:
# Import products
make load_products
# Import customers
make load_customers
π€ Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add some amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgements
π TODO
[] - Mover as entidades que estΓ£o soltas na pasta domain para um pasta entities;
Built with β€οΈ by Deivid Rodrigues