Sunday, October 5, 2025

๐Ÿงฉ Building a Multi-Datasource Go Application

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.





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

๐Ÿงฉ Building a Multi-Datasource Go Application

In many enterprise systems, you may need to work with multiple databases — for example, customer data in PostgreSQL, legacy records in Oracl...