GithubHelp home page GithubHelp logo

j3k0 / routing-worker Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 207 KB

A CloudFlare worker that routes requests depending on the value of a query parameter

License: Other

TypeScript 96.29% JavaScript 3.71%

routing-worker's Introduction

routing-worker

A CloudFlare worker that route requests depending on the value of a query parameter

Usage

Deploy as a CloudFlare worker.

(explain how)

How does it work?

This worker will route requests to a specified backend based on a configuration stored in Cloudflare's KV store.

It reads the value of a routing key from the request: it can be a query parameter or the from the Authentication header.

Routing Key

The routing key is either a query parameter or a value extracted from the Authorization header.

Query Parameter

Customize the name of the query parameter by changing the first line of code const QUERY_PARAMETER = "my-query-parameter".

Authorization Header

The authorization header needs to be specified with this format: Basic ${base64(user + ':' + pass)}.

The user part can be used as a routing key. Enable this by setting const USE_BASIC_AUTHORIZATION_HEADER = true in the first line of code.

Environment configuration

Adding environment variables via wrangler

Environment variables are defined via the [vars] configuration in your wrangler.toml file and are always plaintext values.

# Define top-level environment variables
# under the `[vars]` block using
# the `key = "value"` format
[vars]
QUERY_PARAMETER = "my-query-parameter"
USE_BASIC_AUTHORIZATION_HEADER = "true"
DEFAULT_KEY = "$default"

# Override values for `--env production` usage
[env.production]
name = "router-worker-prod"
[env.production.vars]
QUERY_PARAMETER = "my-query-parameter"
USE_BASIC_AUTHORIZATION_HEADER = "true"
DEFAULT_KEY = "$default"

The variables can be added also from the dashboard, and/or can be adjusted and changed. 1- Go to your Workers script > Settings > Add variable under Environment Variables. 2- Input a Variable name and its value, which will be made available to your Worker. 3- If your variable is a secret, select Encrypt to protect its value. This will prevent the value from being visible via wrangler and the dashboard. 4- (Optional) To add multiple environment variables, select Add variable. 5- Select Save to implement your changes.

Selecting a backend

The routing worker will fetch the configuration from the KV store, using the Routing Key as a key. The value contains the URL (<protocol>://<host>:<port) where to send the request to. The request is then forwarded to the appropriate backend. Note: see this example, however in our case we also have to support POST requests (forwarding the request body)

In case no value is found for this routing key, configuration should be retrieved from the $default key in the KV store.

Caching

configuration will very rarely change. The worker can keep a local cache:

backendConfigurationCache: {
  [routingKey: string]: {
    url: string;
    expiresAt: Date;
  }
}

The function that loads from KV should firt check in the local cache if the value exists (and isn't expired). If so, return the cached value. If not, load from KV and update the cache.

License

(c) 2022, Fovea

routing-worker's People

Contributors

husseintaha avatar j3k0 avatar

Watchers

 avatar  avatar  avatar

routing-worker's Issues

A bit of error handling

Not in try catch block:

This line in request handler:

const urlToRoute = await getRouteBasedOnRequestParams(request);

await can throw an exception, so should be handled: move it inside the try catch block?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.