Skip to content

A high-performance API Gateway written in pure Go, utilizing the Lura framework for efficient, scalable, and modular API management.

License

Notifications You must be signed in to change notification settings

omarfawzi/API-Gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

60 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

API Gateway

Go CI Docker Build Codecov

API Gateway is a high-performance, configurable API gateway built on top of Lura (KrakenD).

graph LR
    A[Client] --> B[API Gateway]

    B --> C1[REST]
    B --> C2[REST]
    B --> D[gRPC]

    C1 --> E1[(Resource)]
    C2 --> E2[(Resource)]
    D --> F[(Resource)]

    B:::gateway
    C1:::rest
    C2:::rest
    D:::grpc

    classDef gateway fill:#dff0ff,stroke:#333,stroke-width:2px;
    classDef rest fill:#ffe5e5,stroke:#ff9966,stroke-width:2px;
    classDef grpc fill:#e0eaff,stroke:#6699cc,stroke-width:2px;
Loading

Table of Contents

Clone the repository

git clone git@github.com:omarfawzi/API-Gateway.git

Features

  • Proxy requests to backend services
  • Configurable via Lura v3 YAML configuration
  • Debug and release modes
  • Integrated Sentry error tracking
  • gRPC backend support using declarative configuration
  • Per-path HTTP/1.x or HTTP/2 control via config

Configuration

The gateway uses Lura Configuration for setting up the gateway config.

Server Handlers

Server Handlers

  • Namespace: github_com/devopsfaith/krakend/transport/http/server/handler.
  • Location: root level of the config file.

To define custom server handlers, you need to do the following:

  1. Define your handler module under internal directory.
  2. Add your handler.go implementation of github.com/luraproject/lura/v2/transport/http/server/plugin/plugin.Registerer.
  3. Define the provider.go method returning instance of your handler.
  4. Register your server handler internal/lura/servers/provider.go
  5. Provide your handler from step 3 to ProvideServerHandlerRegistry in internal/wire.go.

All custom server handlers will still be handled by the Namespace github_com/devopsfaith/krakend/transport/http/server/handler.

version: 3
...
extra_config:
  github_com/devopsfaith/krakend/transport/http/server/handler:
    name:
      - errors-server-handler

Proxy Req/Resp Modifiers

Proxy Handlers

  • Namespace: github.com/devopsfaith/krakend/proxy/plugin.
  • Location: endpoint level of the config file.

To define custom proxy handlers, you need to do the following:

  1. Define your handler module under internal directory.
  2. Add your handler.go implementation of github.com/luraproject/lura/v2/proxy/plugin.Registerer.
  3. Define the provider.go method returning instance of your handler.
  4. Register your server handler internal/lura/proxy/provider.go
  5. Provide your handler from step 3 to ProvideProxyHandlerRegistry in internal/wire.go.

All custom proxy handlers will still be handled by the Namespace github.com/devopsfaith/krakend/proxy/plugin.

Client Handlers

Client Handlers

  • Namespace: github.com/devopsfaith/krakend/transport/http/client/executor.
  • Location: backend level of the config file.

To define custom client handlers, you need to do the following:

  1. Define your handler module under internal directory.
  2. Add your handler.go implementation of github.com/luraproject/lura/v2/transport/http/client/plugin.Registerer.
  3. Define the provider.go method returning instance of your handler.
  4. Register your server handler internal/lura/client/provider.go
  5. Provide your handler from step 3 to ProvideClientHandlerRegistry in internal/wire.go.

All custom client handlers will still be handled by the Namespace github.com/devopsfaith/krakend/transport/http/client/executor.

Backend Middlewares

You can define backend middlewares and provide them in internal/lura/proxy/provider::ProvideProxyFactory, and then assign them to specific backend as needed.

endpoint: /users
method: GET
output_encoding: json
input_headers:
  - Authorization
backend:
  - url_pattern: /users
    method: GET
    host:
      - 'https://jsonplaceholder.typicode.com'
    encoding: json
    extra_config:
      github.com/devopsfaith/krakend-circuitbreaker/gobreaker:
        name: users-circuit-breaker
        interval: 60
        timeout: 1
        max_errors: 5
        log_status_change: true

For a list of opensource plugins, check https://github.com/krakend/krakend-ce/blob/master/backend_factory.go.

gRPC Support

The gateway can proxy gRPC backends using a declarative configuration section. Example:

endpoints:
  - endpoint: /v1/grpc/dummy
    method: POST
    backend:
       - host:
           - grpcb.in:9000
         extra_config:
           plugins/grpc:
             method: grpcbin.GRPCBin/DummyUnary
             descriptor_file: ./proto/grpcb/grpcbin.pb

To generate the .pb files, add your .proto file under proto directory then run

make descriptors

Dynamic Configuration File

We use Gomplate to dynamically generate the Lura configuration file (config/config.json) from a template (config/config.yaml).

This approach provides flexibility by allowing values to be injected through environment variables.

πŸ“„ Example

version: 3
name: api-gateway
port: {{ getenv "APP_PORT" | default "8080" }}

In this example, the application port is sourced from the APP_PORT environment variable, defaulting to 8080 if not set.

This method is particularly useful for securely passing secrets or environment-specific values. For instance, you can inject Basic Auth headers using both Martian and Gomplate:

backend:
  - encoding: json
    method: GET
    url_pattern: /comments
    extra_config:
      github.com/devopsfaith/krakend-martian:
        header.Modifier:
          scope: [request]
          name: X-Custom-Env
          value: {{ getenv "MY_ENV_TAG" | default "test-env" }}
      backend/http/client:
        version: "1"

This forces HTTP/1.1 for that backend. Use version: "2" or omit the field to enable HTTP/2 when supported.

Gomplate is not used at runtime or during the application bootstrap. It runs only as an entrypoint in the container to generate the final configuration file.


Build & Run

πŸ”§ Running Without Docker

make run CONFIG_TEMPLATE_FILE=api.yaml

🐳 Running with Docker (recommended)

make docker

Diagram

To generate diagrams out of the config file, you can try

make draw-krakend CONFIG_TEMPLATE_FILE=api.yaml

this would generate a diagram like the one below

API Gateway Configuration

About

A high-performance API Gateway written in pure Go, utilizing the Lura framework for efficient, scalable, and modular API management.

Resources

License

Stars

Watchers

Forks

Packages