isAdrisal
isAdrisal
DDeno
Created by bwkiwi on 8/22/2024 in #help
Deno KV performance within Deno Deploy Platform (Free Tier)
https://deno.com/blog/kv#run-locally-or-managed Deno KV uses sqlite on your disk when running locally, which is why response times are near-instant
2 replies
DDeno
Created by Adam Argyle on 6/8/2024 in #help
.well-known/ for Fediverse integration
Hey @Adam Argyle! I'm sure there's a better way than this, but I'd do it by creating a middleware in routes/_middleware.ts like this:
import { FreshContext } from "$fresh/server.ts";

const redirects: [URLPattern, URLPatternInit][] = [
[
new URLPattern({ pathname: "/.well-known/host-meta" }),
{ protocol: "https:", hostname: "fed.brid.gy", port: undefined },
],
[
new URLPattern({ pathname: "/.well-known/webfinger" }),
{ protocol: "https:", hostname: "fed.brid.gy", port: undefined },
],
];

export async function handler(req: Request, ctx: FreshContext) {
// Handle route redirects
if (ctx.destination === "route" || ctx.destination === "notFound") {
const matchingRedirect = redirects?.find((entry) => entry[0].test(ctx.url));

if (matchingRedirect) {
const destinationPattern = matchingRedirect[1];
const destinationUrl = buildUrl(
new URL(ctx.url),
destinationPattern,
);

return Response.redirect(destinationUrl.href, 301);
}
}

// Return other requests as normal
const resp = await ctx.next();
return resp;
}

function buildUrl(url: URL, pattern: URLPatternInit) {
return Object.assign(url, pattern);
}
import { FreshContext } from "$fresh/server.ts";

const redirects: [URLPattern, URLPatternInit][] = [
[
new URLPattern({ pathname: "/.well-known/host-meta" }),
{ protocol: "https:", hostname: "fed.brid.gy", port: undefined },
],
[
new URLPattern({ pathname: "/.well-known/webfinger" }),
{ protocol: "https:", hostname: "fed.brid.gy", port: undefined },
],
];

export async function handler(req: Request, ctx: FreshContext) {
// Handle route redirects
if (ctx.destination === "route" || ctx.destination === "notFound") {
const matchingRedirect = redirects?.find((entry) => entry[0].test(ctx.url));

if (matchingRedirect) {
const destinationPattern = matchingRedirect[1];
const destinationUrl = buildUrl(
new URL(ctx.url),
destinationPattern,
);

return Response.redirect(destinationUrl.href, 301);
}
}

// Return other requests as normal
const resp = await ctx.next();
return resp;
}

function buildUrl(url: URL, pattern: URLPatternInit) {
return Object.assign(url, pattern);
}
It's just a simple little redirect handler that uses web standard URL Patterns to construct the redirect matching rules.
5 replies