Skip to content

Commit

Permalink
Added effection-contribs section
Browse files Browse the repository at this point in the history
  • Loading branch information
taras committed Dec 14, 2024
1 parent 9956833 commit ed83fe2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions www/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { docsRoute } from "./routes/docs-route.tsx";
import { indexRoute } from "./routes/index-route.tsx";
import { v2docsRoute } from "./routes/v2docs-route.tsx";
import { assetsRoute } from "./routes/assets-route.ts";
import { proxyRoute } from "./routes/proxy-route.ts";

import { config } from "./tailwind.config.ts";

Expand All @@ -17,6 +18,7 @@ import { loadDocs } from "./docs/docs.ts";
import { loadV2Docs } from "./docs/v2-docs.ts";

await main(function* () {
let proxies = proxySites();
let v2docs = yield* loadV2Docs({
fetchEagerly: !!Deno.env.get("V2_DOCS_FETCH_EAGERLY"),
revision: Number(Deno.env.get("V2_DOCS_REVISION") ?? 4),
Expand All @@ -27,6 +29,7 @@ await main(function* () {
let revolution = createRevolution({
app: [
route("/", indexRoute()),
route("/contribs(.*)", proxyRoute(proxies.contribs)),
route("/docs/:id", docsRoute(docs)),
route("/assets(.*)", assetsRoute("assets")),
route("/V2(.*)", v2docsRoute(v2docs)),
Expand All @@ -44,3 +47,14 @@ await main(function* () {

yield* suspend();
});


function proxySites() {
return {
contribs: {
prefix: "contribs",
website: Deno.env.get("CONTRIBS_URL") ?? "https://effection-contribs.deno.dev",
}
} as const;
}

1 change: 1 addition & 0 deletions www/routes/index-route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function indexRoute(): SitemapRoute<JSXElement> {
navLinks={[
<a href="/docs/installation">Guides</a>,
<a href="https://deno.land/x/effection/mod.ts">API</a>,
<a href="/contribs">Contribs</a>,
<a
class="flex flex-row"
href="https://github.com/thefrontside/effection"
Expand Down
58 changes: 58 additions & 0 deletions www/routes/proxy-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { HTTPMiddleware } from "revolution";
import { call, Operation } from "effection";

export interface ProxyRouteOptions {
website: string;
prefix: string;
root?: string;
}

export function proxyRoute(options: ProxyRouteOptions): HTTPMiddleware {
return function* proxy(request): Operation<Response> {
let website = new URL(options.website);

let target = new URL(request.url);

let prefix = new RegExp(`^\/${options.prefix}\/?`);
target.pathname = target.pathname.replace(prefix, options.root ?? "/");

target.hostname = website.hostname;
target.port = website.port;
target.protocol = website.protocol;

let base = new URL(`/${options.prefix}`, request.url);

let headers: Record<string, string> = {
"X-Base-Url": base.toString(),
};
for (let [key, value] of request.headers.entries()) {
headers[key] = value;
}

let response = yield* call(fetch(target, {
redirect: "manual",
headers,
}));

if (response.status === 301) {
let location = response.headers.get("location");
if (location?.startsWith(String(website))) {
let headers: Record<string, string> = {};
for (let [key, value] of request.headers.entries()) {
headers[key] = value;
}

let url = new URL(request.url);
headers.location = location.replace(target.origin, url.origin);

response = new Response(null, {
status: response.status,
statusText: response.statusText,
headers,
});
}
}

return response;
};
}

0 comments on commit ed83fe2

Please sign in to comment.