Skip to content

dipendra-mule/basic-post-microservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microservices Application

A simple three-service microservice application with JWT authentication, built with Go, gRPC, and Docker.

Architecture

  • Auth Service: Handles user registration, login, and JWT token validation
  • User Service: Manages user profiles and information
  • Post Service: Handles post creation, retrieval, and deletion
  • Separate Databases: Each service has its own PostgreSQL database
  • gRPC: Internal service-to-service communication
  • HTTP REST API: External client communication

Services Overview

Auth Service (Port 8080)

  • POST /register - Register new user
  • POST /login - Login and get JWT token

User Service (Port 8081)

  • GET /users/:id - Get user details
  • PUT /users/:id - Update user information

Post Service (Port 8082)

  • POST /post - Create new post
  • GET /post/:id - Get post details
  • GET /user/:id/posts - Get user's posts
  • DELETE /post/:id - Delete post

Prerequisites

  • Docker and Docker Compose
  • Go 1.19+ (for local development)
  • Make (optional, but recommended)

Quick Start

Using Make Commands

# Clone the repository
git clone <repository-url>
cd microservices-app

# Setup and run all services
make setup
make run

# Or simply run everything with one command
make all

Manual Setup

  1. Create environment file:
cp .env.example .env
  1. Build and run services:
docker-compose up --build
  1. Verify services are running:
curl http://localhost:8080/health

Environment Configuration

Create a .env file with the following variables:

POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
JWT_SECRET=your-super-secret-jwt-key
AUTH_HTTP_PORT=8080
USER_HTTP_PORT=8081
POST_HTTP_PORT=8082

API Usage Examples

1. Register a User

curl -X POST http://localhost:8080/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password",
    "name": "John Doe"
  }'

2. Login

curl -X POST http://localhost:8080/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password"
  }'

3. Create a Post (with JWT token)

curl -X POST http://localhost:8082/posts \
  -H "Content-Type: application/json" \
  -H "Authorization: <your-jwt-token>" \
  -d '{
    "title": "My First Post",
    "content": "This is my first post content"
  }'

4. Get User Posts

curl -X GET http://localhost:8082/users/<user-id>/posts \
  -H "Authorization: <your-jwt-token>"

Make Commands

Command Description
make help Show all available commands
make all Build and run all services
make build Build all Docker images
make run Start all services
make stop Stop all services
make clean Stop and remove all containers and volumes
make logs Show logs from all services
make status Show service status
make test Run tests for all services
make lint Lint all Go code
make protoc Generate gRPC code from proto files
make db-shell Connect to database (specify DB=service-name)

Service-specific Commands

make logs-auth          # Show auth service logs
make logs-user          # Show user service logs
make logs-post          # Show post service logs
make restart-single SERVICE=auth-service  # Restart specific service
make build-single SERVICE=user-service    # Build specific service

Development

Local Development Setup

  1. Install dependencies:
make dev-deps
  1. Generate gRPC code:
make protoc
  1. Build locally:
make dev-build

Project Structure

microservices-app/
├── auth-service/
│   ├── proto/
│   ├── main.go
│   └── Dockerfile
├── user-service/
│   ├── proto/
│   ├── main.go
│   └── Dockerfile
├── post-service/
│   ├── proto/
│   ├── main.go
│   └── Dockerfile
├── docker-compose.yml
├── Makefile
└── .env

Database Information

Each service has its own PostgreSQL database:

Service Database Host Port
Auth auth_db 5432
User user_db 5433
Post post_db 5434

Connect to a database:

make db-shell DB=auth-db

Deployment

Production Deployment

  1. Update environment variables for production:
# Update .env with production values
JWT_SECRET=very-long-random-production-secret
POSTGRES_PASSWORD=strong-production-password
NODE_ENV=production
  1. Deploy:
make deploy

Scaling Services

Scale specific services:

make scale SERVICE=auth-service NUM=3

Monitoring

Check service health:

make health

View container statistics:

make stats

Troubleshooting

Common Issues

  1. Port conflicts: Change ports in .env file
  2. Database connection errors: Ensure databases are running with make status
  3. gRPC connection issues: Check service dependencies in docker-compose

Logs

View logs for debugging:

make logs
# Or for specific service
make logs-auth

License

MIT License

About

A simple three-service microservice application with JWT authentication, built with Go, gRPC, and Docker.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages