In many enterprise systems, you may need to work with multiple databases — for example, customer data in PostgreSQL, legacy records in Oracle, and transactional data in MySQL. Managing these connections efficiently while keeping the code clean can be challenging.
.png)
The Multi-Datasource Go project — a Go-based demo showing how to connect to MySQL, PostgreSQL, and Oracle XE within one modular application.
⚙️ Project Overview
The project follows a clean architecture pattern, ensuring clear separation between layers:
- Handlers → handle HTTP routes using Gin
- Services → contain business logic
- Repositories → handle database operations per datasource
- Models → define entity structures
Each datasource is completely independent, configured through a simple YAML file, and initialized at runtime with connection pooling.
๐งฑ Features
๐ Multiple database connections (MySQL, PostgreSQL, Oracle)
๐ RESTful API with Gin
๐ง Domain-driven design with repository pattern
๐ Configurable via YAML and Viper
๐ณ Docker Compose setup for quick local testing
⚡ Built-in health checks and auto table creation
๐️ Clean architecture (domain, repository, service layers)
๐งฐ Tech Stack
Layer Technology
Web Framework Gin
Configuration Viper
Databases MySQL • PostgreSQL • Oracle XE
Drivers go-sql-driver/mysql, pgx, go-ora
Environment Go 1.25.1 • Docker Compose
๐️ Project Structure
multi-datasource-go/
├─ cmd/api/main.go # Application entry point
├─ internal/config # Configuration loader (Viper)
├─ internal/db # Database connection pools
├─ internal/http # API routes and handlers
├─ internal/domain # Entities, repos, services
└─ internal/repo # Database-specific repositories
๐ Getting Started
1. Clone the Repository
git clone https://github.com/HenryXiloj/golang-demos.git
cd golang-demos/multi-datasource-go
2. Start Databases
docker compose -f docker-compose-multiple-db.yml up -d
3. Run the Application
go run cmd/api/main.go
๐งช Test the APIs
# MySQL
curl -X POST http://localhost:9000/api/v1/users \
-H "Content-Type: application/json" \
-d '{"name":"Henry","lastName":"x"}'
# PostgreSQL
curl -X POST http://localhost:9000/api/v2/companies \
-H "Content-Type: application/json" \
-d '{"name":"Test"}'
# Oracle
curl -X POST http://localhost:9000/api/v3/brands \
-H "Content-Type: application/json" \
-d '{"name":"Acme"}'
You’ll see all three databases responding independently with auto-created tables.
You can explore the full source code here:๐ multi-datasource-go
No comments:
Post a Comment