This is an API Gateway that can be deployed to the Edge with Cloudflare Workers.
Just create a configuration file listing the gateway features of your choice and deploy the API Gateway to Cloudlfare's Edge Network which has more than 250 locations around the world, so your API Gateway is running as close as possible to your users, improving your API response times to the milliseconds and caching your responses as close as possible to the user.
Clone the repository and edit the src/config.json
adding the routes
and policies
of your API Gateway.
npx wrangler publish
That's it! That's how easy to have an API Gateway on the Edge.
Run the project locally for testing and development with:
npx wrangler dev --local
An object defining a policy that will then be applied to Requests or Responses from the gateway.
{
"name": "policyName",
"type": "type",
"options": {
"option1": "123",
"option2": "456"
}
}
-
name
: policies can have any custom name that can be referenced to from theroutes
object. You can declare two policies of the same type with different configuration (for example, if you wanted to have different auth scopes with the same auth provider). -
type
: must be the name of the function handler fromsrc/policies/request/index.ts
for Requests orsrc/policies/response/index.ts
for Responses -
options
: sn object of arbitrary elements that define the options that will be passed to each policy handler.
An object defining a route that will be handled by the gateway.
{
"path": "/hello",
"method": "GET",
"origin": {
"type": "url",
"options": {
"url": "https://jsonplaceholder.typicode.com/todos/1"
}
},
"policies": {
"request": ["company-scope-auth"],
"response": ["cors"]
}
}
path
: the pathname from the URL (wildcards not yet supported)method
: the HTTP methodorigin
: an object containing the details of the handler of the requestpolicies/request
: an array of policy names that will be applied at incoming requestspolicies/response
: an array of policy names that will be applied after the origin handled the request
Describes the handler of the request. Right now it only support url
but we will add support to specific cloud providers or Kubernetes services.
{
"type": "url",
"options": {
"url": "https://jsonplaceholder.typicode.com/todos/1"
}
}
type
: must be the same name as the function handler from src/origins/index.ts
.
options
: an object of arbitrary elements that define the options that will be passed to the origin handler.
An example gateway configuration looks like the following.
{
"routes": [
{
"path": "/hello",
"method": "GET",
"origin": {
"type": "url",
"options": {
"url": "https://jsonplaceholder.typicode.com/todos/1"
}
},
"policies": {
"request": ["company-scope-auth"],
"response": ["cors"]
}
}
],
"policies": [
{
"name": "company-scope-auth",
"type": "auth0jwt",
"options": {
"issuer": "https://aabedraba.eu.auth0.com/",
"audience": "https://company"
}
},
{
"name": "cors-rule",
"type": "cors",
"options": {
"allowedOrigins": ["https://example.com"]
}
}
]
}
TODOs:
- Admin logs: need to be able to see everything going through the gateway for debuggability
- Move all envs to Cloudflare Workers secrets