The API gateway in the microservice architecture represents a central entry point.
This project is still in development.
- OAuth2
- Individual scopes for services and routes
- Event-Handling with response
- Metrics for Prometheus
- Composer
- NPM
At first, you must fork this project.
- cd project/
- composer install
- npm install
A service for the gateway must be configured in the config of the gateway packages.
app/config/gateway.php
Key | Value | Description |
---|---|---|
scopes | ARRAY | Scope for OAuth2 |
services | ARRAY | Services |
services.* | STRING | Name of the service |
services.*.scheme | http or https | Scheme |
services.*.host | STRING | Host |
services.*.port | INTEGER | Port |
services.*.timeout | INTEGER | Timeout for curl |
services.*.http_methods | ARRAY | Valid http methods |
services.*.http_methods.* | GET, POST, PUT, DELETE | HTTP-Method |
services.*.events | ARRAY | Events ( Example: [NAME => CLASS] ) |
services.*.scopesByRoutes | ARRAY | Scopes by routes ( Example: [REGEX => NAME OF THE SCOPE] ) |
The event class must be an instance of App\Providers\Gateway\Events\GatewayEvent
Must be an entry in the scopes array.
To use the gateway, an account must be created first:
If an account already exists:
After the login there are two methods available to create a token:
- OAuth
- Personal Access
Before the token can be created, a client (OAuth Client | Personal Access Client) must be applied.
The authentication or identify is done via the authorization header.
KEY: Authorization
VALUE: Bearer TOKEN
If the authentication of the client fails this response will be shown:
401: Unautzorized
The routing of the gateway determines which service and which endpoint is requested.
api/{service}/{endpoint}
The service, which is selected via the route must be configured.
The end point is the core part of the request to the respective service. This part of the URL is used to build the request URL to the service.
api/data/endpoint/object/1
- The configured service "data" should be used.
- The service is queried with the endpoint "endpoint/object/1" (E.g. https://gateway.de/endpoint/object/1).
A service events in the gateway can trigger by using the key _events in the response. These events are processed synchronously and need to be successfully executed to create a successfully response.
[
...,
"_events" => [
"EVENT" => [
"key1" => "value1",
"key2" => "value2"
]
]
]
An event runs only if it from the abstract class * GatewayEvent * inherits.
app/Providers/Gateway/Events/GatewayEvent.php
- Check whether the event is an instance of GatewayEvent.
- Event will be triggered
- Validation of the event payload
- Execute the event
The metrics are formatted for Prometheus.
All additional information about the package is available at Github. LaravelPrometheusExporter
triadev/pe/metrics
- gateway_{SERVICE}
- gateway_{SERVICE}_http_method_{HTTP-METHOD}
- gateway_{SERVICE}_endpoint_{ENDPOINT}
- gateway_dispatch_{SERVICE}_endpoint_{ENDPOINT}
- gateway_error_service_not_exist
- gateway_error_http_method_not_accept
- gateway_event_{EVENT}
- gateway_error_event_{EVENT}
- gateway_error_{SERVICE}_scope_{SCOPE}
- gateway_curl_total_time
- gateway_curl_namelookup_time
- gateway_curl_connect_time
- gateway_curl_pretransfer_time
- gateway_curl_starttransfer_time
ENDPOINT without special chars: /test/gateway => testgateway
If you do find an issue, please feel free to report it with GitHub's bug tracker for this project.
Alternatively, fork the project and make a pull request. :)
The code for LaravelApiGateway is distributed under the terms of the MIT license (see LICENSE).