Skip to content

ozturkeniss/FiberV2-MicroStack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Fiber Enchanted 🚀

A modern, scalable e-commerce microservices architecture built with Go Fiber and Next.js.

Architecture Overview

graph TB
    Client[Next.js Client<br/>Port: 3000]
    Gateway[API Gateway<br/>Port: 8000]
    User[User Service<br/>Port: 8001]
    Product[Product Service<br/>Port: 8002]
    Basket[Basket Service<br/>Port: 8003]
    Payment[Payment Service<br/>Port: 8004]
    
    Client -->|HTTP/REST| Gateway
    Gateway -->|Proxy| User
    Gateway -->|Proxy| Product
    Gateway -->|Proxy| Basket
    Gateway -->|Proxy| Payment
    
    Product -.->|gRPC Auth| User
    Basket -.->|gRPC Auth| Product
    Payment -.->|gRPC Auth| User
    
    style Gateway fill:#ff6b6b
    style User fill:#4ecdc4
    style Product fill:#45b7d1
    style Basket fill:#96ceb4
    style Payment fill:#ffeaa7
    style Client fill:#dfe6e9
Loading

Microservices Stack

graph LR
    subgraph "API Gateway :8000"
        GW[Fiber v2 Gateway]
        GW_Features[Rate Limiting<br/>JWT Auth<br/>CORS<br/>Metrics<br/>Health Checks]
    end
    
    subgraph "User Service :8001"
        US[Fiber v2]
        US_DB[(PostgreSQL<br/>:5432)]
        US_GRPC[gRPC Server<br/>:50051]
    end
    
    subgraph "Product Service :8002"
        PS[Fiber v2]
        PS_DB[(PostgreSQL<br/>:5433)]
        PS_GRPC[gRPC Client]
    end
    
    subgraph "Basket Service :8003"
        BS[Fiber v2]
        BS_Redis[(Redis<br/>:6379)]
        BS_GRPC[gRPC Client]
    end
    
    subgraph "Payment Service :8004"
        PayS[Fiber v2]
        PayS_DB[(MariaDB<br/>:3306)]
        PayS_GRPC[gRPC Client]
        PayS_Kafka[Kafka Events]
    end
    
    GW --> US
    GW --> PS
    GW --> BS
    GW --> PayS
    
    PS_GRPC -.->|Auth| US_GRPC
    BS_GRPC -.->|Auth| US_GRPC
    PayS_GRPC -.->|Auth| US_GRPC
Loading

Data Flow

sequenceDiagram
    participant C as Client
    participant G as API Gateway
    participant U as User Service
    participant P as Product Service
    participant B as Basket Service
    participant Pay as Payment Service
    
    C->>G: POST /auth/login
    G->>U: Proxy to User Service
    U-->>G: JWT Token
    G-->>C: JWT Token
    
    C->>G: GET /products (+ JWT)
    G->>G: Validate JWT
    G->>P: Proxy with JWT
    P-->>G: Products List
    G-->>C: Products List
    
    C->>G: POST /basket/items (+ JWT)
    G->>G: Validate JWT
    G->>B: Proxy with JWT
    B->>U: gRPC Auth Validation
    U-->>B: Auth OK
    B-->>G: Item Added
    G-->>C: Success
    
    C->>G: POST /orders (+ JWT)
    G->>G: Validate JWT
    G->>Pay: Proxy with JWT
    Pay->>U: gRPC Auth Validation
    U-->>Pay: Auth OK
    Pay->>Pay: Create Order
    Pay->>Pay: Publish Kafka Event
    Pay-->>G: Order Created
    G-->>C: Order Response
Loading

Technology Stack

Backend (Go)

  • Framework: Fiber v2
  • Databases: PostgreSQL, MariaDB, Redis
  • Communication: gRPC, HTTP/REST
  • Event Streaming: Kafka (Sarama)
  • Documentation: Swagger/OpenAPI
  • Authentication: JWT

Frontend

  • Framework: Next.js 14
  • Language: TypeScript

Infrastructure

  • Containerization: Docker Compose
  • Build Tool: Make
  • Testing: Shell Scripts

Quick Start

Prerequisites

  • Go 1.21+
  • Docker & Docker Compose
  • Node.js 18+ (for client)
  • Make

1. Start Infrastructure

cd go-server
docker-compose up -d

This starts:

  • PostgreSQL (User Service - :5432)
  • PostgreSQL (Product Service - :5433)
  • Redis (Basket Service - :6379)
  • MariaDB (Payment Service - :3306)

2. Build All Services

make build-all

3. Start All Services

./integrations/start-all-services.sh

Services will be available at:

4. Run Integration Tests

# Test via API Gateway
./integrations/test-gateway-flow.sh

# Test all services individually
./integrations/test-all.sh

5. Stop All Services

./integrations/stop-all-services.sh

API Gateway Features

graph TD
    Request[Incoming Request] --> RateLimit[Rate Limiter<br/>100 req/min]
    RateLimit --> Auth{Auth<br/>Required?}
    Auth -->|Yes| JWT[JWT Validation]
    Auth -->|No| Route[Route Matching]
    JWT --> Route
    Route --> Proxy[HTTP Proxy]
    Proxy --> Backend[Backend Service]
    Backend --> Response[Response]
    Response --> Metrics[Metrics Collection]
    Metrics --> Client[Client Response]
Loading

Gateway Endpoints

  • Health: GET /health
  • Metrics: GET /metrics
  • API Base: /api/v1/*

Service Responsibilities

User Service

  • User registration & authentication
  • JWT token generation
  • gRPC auth server for other services
  • User profile management

Product Service

  • Product & category management
  • Product search & filtering
  • Inventory management
  • gRPC client for auth

Basket Service

  • Shopping cart operations
  • Redis-based session storage
  • TTL-based cart expiration (1 day)
  • gRPC client for auth

Payment Service

  • Order creation & management
  • Payment processing simulation
  • Kafka event publishing
  • MariaDB for transactional data

Event-Driven Architecture

graph LR
    Pay[Payment Service] -->|Publish| Kafka[Kafka Topics]
    Kafka -->|order.completed| Consumer1[Order Consumer]
    Kafka -->|basket.cleared| Consumer2[Basket Consumer]
    Kafka -->|product.stock.updated| Consumer3[Stock Consumer]
    
    style Kafka fill:#ff6b6b
Loading

Kafka Events

  • order.completed: Triggered on successful payment
  • basket.cleared: User's basket cleared after order
  • product.stock.updated: Product inventory updated

Development

Generate Swagger Docs

make swagger

Run Individual Service

make run-user
make run-product
make run-basket
make run-payment
make run-gateway

Build Individual Service

make build-user
make build-product
make build-basket
make build-payment
make build-gateway

Generate Protobuf

make proto

Project Structure

fiber-enchanted/
├── client/                 # Next.js frontend
├── go-server/
│   ├── api/proto/         # Protocol Buffer definitions
│   ├── cmd/               # Service entry points
│   │   ├── userservice/
│   │   ├── productservice/
│   │   ├── basketservice/
│   │   ├── paymentservice/
│   │   └── gateway/
│   ├── fiber-gateway/     # API Gateway implementation
│   ├── event-driven/      # Kafka publishers/consumers
│   ├── internal/          # Service implementations
│   ├── integrations/      # Integration test scripts
│   ├── docker-compose.yml
│   └── Makefile
└── README.md

Testing Strategy

graph TD
    Test[Integration Tests] --> Direct[Direct Service Tests]
    Test --> Gateway[Gateway Proxy Tests]
    
    Direct --> UserTest[test-user-service.sh]
    Direct --> ProductTest[test-product-service.sh]
    Direct --> BasketTest[test-basket-service.sh]
    Direct --> PaymentTest[test-payment-service.sh]
    
    Gateway --> GatewayTest[test-gateway-flow.sh]
    
    Test --> Full[test-all.sh<br/>Complete Test Suite]
Loading

Authentication Flow

sequenceDiagram
    participant C as Client
    participant G as Gateway
    participant U as User Service
    participant S as Protected Service
    
    C->>G: POST /auth/login
    G->>U: Forward Request
    U->>U: Validate Credentials
    U->>U: Generate JWT
    U-->>G: JWT Token
    G-->>C: JWT Token
    
    C->>G: GET /protected/resource<br/>(Authorization: Bearer JWT)
    G->>G: Validate JWT<br/>(Check signature)
    G->>S: Proxy Request<br/>(Forward JWT)
    S->>U: gRPC ValidateToken
    U-->>S: User Info
    S-->>G: Protected Resource
    G-->>C: Resource Data
Loading

Gateway Middleware Stack

graph TD
    Request[Request] --> Recovery[Panic Recovery]
    Recovery --> RequestID[Request ID Generator]
    RequestID --> Logger[Request Logger]
    Logger --> CORS[CORS Handler]
    CORS --> Compress[Response Compression]
    Compress --> RateLimit[Rate Limiter]
    RateLimit --> Metrics[Metrics Collector]
    Metrics --> Auth{Auth<br/>Middleware}
    Auth -->|Protected| JWT[JWT Validation]
    Auth -->|Public| Route[Route Handler]
    JWT --> Route
    Route --> Proxy[HTTP Proxy]
    Proxy --> Backend[Backend Service]
Loading

Monitoring

Health Checks

curl http://localhost:8000/health

Returns service health status for all microservices.

Metrics

curl http://localhost:8000/metrics

Provides:

  • Total requests
  • Active requests
  • Error count
  • Response times per service

License

MIT License - see LICENSE file for details.

Contributing

This is a demonstration project showcasing microservices architecture with Go Fiber.


Built with ❤️ using Go Fiber v2

About

A modern, scalable e-commerce microservices architecture built with Go Fiber and Next.js.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published