Skip to content

Instantly share code, notes, and snippets.

@jamesbibby
Created February 22, 2019 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesbibby/c29a7e6b28eb6878be66ea976a2d714b to your computer and use it in GitHub Desktop.
Save jamesbibby/c29a7e6b28eb6878be66ea976a2d714b to your computer and use it in GitHub Desktop.
API Gateway Cloudflare Worker
// The base url for the site
const baseURL = '.bibs.codes';
/**
The actual request handler
Remaps the URL from: api.bibs.codes/service_name to service_name.bibs.codes
**/
async function handleRequest(request) {
const url = new URL(request.url);
// skip the leading '/' and split the request
const parts = url.pathname.substring(1).split('/');
if (parts.length === 0 || parts[0] === '') {
// if there is no service name, return a 404
return new Response('Service Not Provided', { status: 404 });
}
const serviceURL = ''.concat(
url.protocol, // use the same protocol
'//',
parts[0], // the service name
'.bibs.codes/',
parts.slice(1).join('/'), // the rest of the path after the service name
url.search // the query string
);
return fetch(serviceURL, request);
}
/**
Register the request handler
**/
self.addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment