Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Gateway

A simple reverse proxy API gateway for routing requests to multiple backend services from a single endpoint.

Features

  • Single entry point for multiple backend services
  • Hot reloading with Air during development
  • Request logging
  • Health check endpoint
  • Dynamic service configuration via YAML
  • Language-agnostic: works with any HTTP backend service (Go, Node.js, Python, Ruby, Java, .NET, etc.)

Architecture

flowchart TD
    subgraph Mobile App
        Client[Mobile App]
    end

    subgraph API Gateway
        Gateway[API Gateway<br/>:8080]
        HealthCheck("Health Check<br/>(/health)")
        Router[Request Router]
    end

    subgraph Backend Services
        Service1[Service 1<br/>localhost:3001]
        Service2[Service 2<br/>localhost:3002]
    end

    Client -->|HTTP Requests :8080| Gateway
    Gateway -->|/health| HealthCheck
    Gateway --> Router
    Router -->|/api/service1/*| Service1
    Router -->|/api/service2/*| Service2

    style Gateway fill:#4a90d9,stroke:#2c5282,stroke-width:2px
    style Service1 fill:#48bb78,stroke:#2f855a,stroke-width:2px
    style Service2 fill:#48bb78,stroke:#2f855a,stroke-width:2px
    style Client fill:#ed8936,stroke:#c05621,stroke-width:2px
Loading

API Gateway Overview

Request Flow

sequenceDiagram
    participant Client as Mobile App
    participant Gateway as API Gateway
    participant S1 as Service 1 (:3001)
    participant S2 as Service 2 (:3002)

    Client->>Gateway: GET /api/service1/users
    Gateway->>S1: Forward request
    S1-->>Gateway: Response
    Gateway-->>Client: Return response

    Client->>Gateway: POST /api/service2/orders
    Gateway->>S2: Forward request
    S2-->>Gateway: Response
    Gateway-->>Client: Return response

    Note over Client,Gateway: Health Check
    Client->>Gateway: GET /health
    Gateway-->>Client: OK
Loading

Project Structure

api-gateway/
├── .air.toml          # Air configuration for hot reloading
├── config/
│   ├── config.go     # Configuration loader
│   └── config.yaml    # Default configuration
├── go.mod             # Go module file
├── main.go            # Main gateway application
├── README.md          # This file
└── tmp/               # Auto-generated by Air (gitignored)

Adding More Services

To add a new service, simply add it to config/config.yaml:

services:
  - name: go-service
    path: /api/go-service/
    url: http://localhost:3001
  - name: nodejs-service
    path: /api/nodejs/
    url: http://localhost:3002
  - name: python-service
    path: /api/python/
    url: http://localhost:3003

The gateway is language-agnostic - it forwards HTTP requests to any backend service regardless of the language or framework used. Add services written in Go, Node.js, Python, Ruby, Java, .NET, or any other technology that exposes an HTTP endpoint.

No code changes required. Air will automatically reload the gateway with the new configuration.

Request Logging

All incoming requests and responses are logged to the console with the following information:

  • Client IP - IP address and port of the requesting client
  • Request method and path - e.g., GET /api/service1/users
  • Target backend - which service the request is proxied to
  • Response status code - HTTP status returned by the backend
  • Response duration - time taken to process the request

Example Log Output

[10.0.2.2:52341] Incoming request: GET /api/service1/users
[10.0.2.2:52341] Proxying to service1: GET /api/service1/users
[10.0.2.2:52341] GET /api/service1/users - 200 - 1.245ms

This helps you monitor traffic and diagnose issues during development.

Mobile App Configuration

For Android Emulator, use the special IP address to access the host machine:

http://10.0.2.2:8080

The Android emulator uses 10.0.2.2 as a special alias to your host machine's loopback interface (equivalent to 127.0.0.1).

For physical devices or other emulators, use your machine's IP address:

http://YOUR_IP:8080

Where YOUR_IP is your local machine's IP address (e.g., 192.168.1.100).

To find your IP:

  • Linux/Mac: ifconfig or ip addr
  • Windows: ipconfig

Troubleshooting

"Connection refused" errors

  • Ensure your backend services are running on the specified ports
  • Check that services are listening on localhost or 0.0.0.0

Mobile app can't connect

  • For Android Emulator, use http://10.0.2.2:8080 instead of localhost
  • For physical devices, verify your machine's IP address
  • Ensure the gateway is listening on 0.0.0.0:8080 (not just localhost)
  • Check firewall settings allow connections on port 8080

Air not found

go install github.com/air-verse/air@latest
export PATH=$PATH:$(go env GOPATH)/bin

License

See LICENSE for details.

About

A simple reverse proxy API gateway for routing requests to multiple backend services from a single endpoint.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages