Loading repository data…
Loading repository data…
afteracademy / repository
gomicro is a Go microservices architecture using goserve micro framework. The blogging platform example is built using Kong API gateway, NATS, Mongo, Redis, and Docker. It implements authentication, authorization, and apikey protection.
A complete microservices implementation using GoServe framework, Kong API Gateway, NATS messaging, PostgreSQL, MongoDB, and Redis.
This project demonstrates a production-ready microservices architecture built with the GoServe framework. It breaks down a monolithic blogging platform into independent services using Kong as API Gateway and NATS for inter-service communication. Each service maintains its own database and cache, showcasing true microservices best practices with service isolation, independent scaling, and fault tolerance.
The architecture implements authentication, authorization, and API key validation across distributed services while maintaining clean separation of concerns and independent deployability.
| Component | Technology |
|---|---|
| Language | Go 1.21+ |
| Framework | GoServe v2 |
| API Gateway | Kong |
| Message Broker | NATS |
| Web Framework | Gin |
| Auth Database | PostgreSQL (pgx) |
| Blog Database | MongoDB (mongo-driver) |
| Cache | Redis (go-redis) |
| Migrations | golang-migrate |
| Authentication | JWT tokens |
| Validation | validator |
| Configuration | Environment variables |
1. Clone the Repository
git clone https://github.com/afteracademy/gomicro.git
cd gomicro
2. Generate RSA Keys
go run .tools/rsa/keygen.go
3. Create Environment Files
go run .tools/copy/envs.go
4. Start with Docker Compose
# Full stack with all services
docker compose up --build
The API will be available at: http://localhost:8000 (via Kong Gateway)
5. Health Check
# Check Kong Gateway
curl http://localhost:8001/status
# Check NATS
curl http://localhost:8222/varz
| Service | URL | Description |
|---|---|---|
| Kong Gateway | http://localhost:8000 | API Entry Point |
| Kong Admin | http://localhost:8001 | Kong Configuration |
| NATS Monitoring | http://localhost:8222 | NATS Dashboard |
| PostgreSQL | localhost:5432 | Auth Database |
| MongoDB | localhost:27017 | Blog Database |
| Redis (Auth) | localhost:6379 | Auth Cache |
| Redis (Blog) | localhost:6380 | Blog Cache |
Development Tools (with docker-compose.dev.yml):
| Tool | URL | Purpose |
|---|---|---|
| pgAdmin | http://localhost:5050 | PostgreSQL Management |
| Mongo Express | http://localhost:8082 | MongoDB Management |
| Redis Commander | http://localhost:8083 | Redis Management |
If you encounter issues:
docker compose logs -f [service_name]docker compose down -v && docker compose up --buildFor detailed setup, usage, and troubleshooting: README-DOCKER.md
docker compose up --build
Starts all services with Kong, NATS, and shared databases.
Adds pgAdmin, Mongo Express, and Redis Commander for database management.
# Auth service only
cd auth_service && docker compose up --build
# Blog service only
cd blog_service && docker compose up --build
Runs a single service in isolation for fast iteration and debugging.
docker compose -f docker-compose-load-balanced.yml up --build
Runs 2 instances of each service behind Kong for production-like setup.
This project follows microservices best practices:
1. Without Load Balancing
2. With Load Balancing
Client → Kong Gateway → API Key Validation → Service → NATS → Response
auth:8080/verify/apikeyDesign Philosophy: This distributed authentication/authorization gives each service autonomy to define public, protected, and restricted APIs independently while maintaining centralized user management.
Synchronous (HTTP)
Asynchronous (NATS)
gomicro-network) for service discoverygomicro/
├── auth_service/ # Authentication & Authorization Service
│ ├── api/
│ │ ├── auth/ # Auth endpoints (signup, signin, refresh)
│ │ │ ├── dto/ # Request/response DTOs
│ │ │ ├── message/ # NATS message definitions
│ │ │ ├── middleware/ # Auth & authorization middleware
│ │ │ └── model/ # PostgreSQL models
│ │ └── user/ # User management endpoints
│ ├── cmd/main.go # Service entry point
│ ├── migrations/ # PostgreSQL migrations
│ ├── startup/ # Server initialization
│ └── docker-compose.yml # Standalone development
│
├── blog_service/ # Blog Management Service
│ ├── api/
│ │ ├── author/ # Author-specific blog operations
│ │ ├── blog/ # Blog CRUD operations
│ │ ├── blogs/ # Blog listing & search
│ │ └── editor/ # Editorial operations
│ ├── cmd/main.go # Service entry point
│ ├── startup/ # Server initialization & indexes
│ └── docker-compose.yml # Standalone development
│
├── kong/ # API Gateway Configuration
│ ├── kong.yml # Kong declarative config
│ ├── kong-load-balanced.yml # Load balanced config
│ └── apikey_auth_plugin/ # Custom Go plugin for API key validation
│
├── docker-compose.yml # Full stack orchestration
├── docker-compose.dev.yml # Development tools
├── docker-compose-load-balanced.yml # Production setup
└── README-DOCKER.md # Detailed Docker documentation
| Directory | Purpose |
|---|---|
| api/ | Feature-based API implementations |
| cmd/ | Application entry point (main.go) |
| common/ | Shared code across APIs |
| config/ | Environment configuration |
| keys/ | RSA keys for JWT signing |
| migrations/ | Database migration files (auth service) |
| startup/ | Server, DB, Redis, NATS initialization |
| utils/ | Utility functions |
| Directory | Purpose |
|---|---|
| .extra/ | Database init scripts, documentation, assets |
| .tools/ | RSA key generator, env file copier |
| .vscode/ | Editor configuration, debug settings |
| .docs/ | Architecture diagrams, banners |
Create message types for inter-service communication:
package message
type TokenValidation struct {
AccessToken string `json:"accessToken,omitempty"`
}
func NewTokenValidation(token string) *TokenValidation {
return &TokenValidation{
AccessToken: token,
}
}
Controllers implement micro.Controller to handle both HTTP and NATS requests:
package auth
import (
"github.com/gin-gonic/gin"
"github.com/afteracademy/gomicro/auth_service/api/auth/message"