mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(infra): add current CF workers
This commit is contained in:
parent
e1fb918ac0
commit
3ffc0d2091
3 changed files with 57 additions and 0 deletions
1
infra/cloudflare-workers/README.md
Normal file
1
infra/cloudflare-workers/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
Currently manually copy/paste/deploy via Cloudflare Portal UI.
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* Cloudflare Worker for api.filterlists.com
|
||||
*
|
||||
* This worker enables two key functionalities:
|
||||
* 1. Azure App Service Free Tier Custom Domain: Proxies requests to `app-filterlists-directory-prod.azurewebsites.net`, allowing a custom domain (`api.filterlists.com`) with Azure's free tier.
|
||||
* 2. Respect Origin Cache-Control: Ensures Cloudflare's edge cache (via Cache API) honors the `Cache-Control` headers sent by the origin for all successful responses, optimizing caching for JSON APIs and static assets.
|
||||
*/
|
||||
export default {
|
||||
async fetch(request, _, ctx) {
|
||||
const cache = caches.default;
|
||||
const cacheKey = request;
|
||||
|
||||
let response = await cache.match(cacheKey);
|
||||
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
|
||||
const url = new URL(request.url);
|
||||
const azureHostname = "app-filterlists-directory-prod.azurewebsites.net";
|
||||
const originUrl = `https://${azureHostname}${url.pathname}${url.search}`;
|
||||
const originRequest = new Request(originUrl, request);
|
||||
|
||||
originRequest.headers.set("Host", azureHostname);
|
||||
|
||||
response = await fetch(originRequest);
|
||||
|
||||
if (response.ok) {
|
||||
const responseToCache = response.clone();
|
||||
ctx.waitUntil(cache.put(cacheKey, responseToCache));
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
};
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Cloudflare Worker for next.filterlists.com
|
||||
*
|
||||
* Azure App Service Free Tier Custom Domain: Proxies requests to `app-filterlists-web-prod-h2hecsdqbrach3dt.eastus2-01.azurewebsites.net`, allowing a custom domain (`next.filterlists.com`) with Azure's free tier.
|
||||
*/
|
||||
export default {
|
||||
async fetch(request, _, __) {
|
||||
const url = new URL(request.url);
|
||||
const azureHostname = "app-filterlists-web-prod-h2hecsdqbrach3dt.eastus2-01.azurewebsites.net";
|
||||
const originUrl = `https://${azureHostname}${url.pathname}${url.search}`;
|
||||
const originRequest = new Request(originUrl, request);
|
||||
|
||||
// re-write Host header to trick App Service free tier into supporting custom domain
|
||||
originRequest.headers.set("Host", azureHostname);
|
||||
|
||||
// delete cookie to trick Cloudflare into caching static HTML
|
||||
originRequest.headers.delete('cookie');
|
||||
|
||||
return await fetch(originRequest);
|
||||
},
|
||||
};
|
||||
Loading…
Reference in a new issue