Adam Argyle
Adam Argyle4mo ago

.well-known/ for Fediverse integration

I'm trying to follow the instructions here: https://fed.brid.gy/docs#fediverse-enhanced and am using Deploy and Fresh and not finding any good resources on how to do this. would love some tips or links. thanks!
4 Replies
Deno AI Helper
Deno AI Helper4mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided, it seems like you're trying to integrate Fediverse functionality into a Deno Deploy project using Fresh. If you're looking to serve files from a .well-known/ directory, you would typically set up routing in your Fresh project to respond to requests for paths under that directory. Here's a general idea of what you might do, although please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice:
// routes/_middleware.ts or a specific route like routes/.well-known/mod.ts

import { MiddlewareHandlerContext } from "$fresh/server.ts";

export async function handler(
req: Request,
ctx: MiddlewareHandlerContext
) {
const url = new URL(req.url);
if (url.pathname.startsWith("/.well-known/")) {
// Handle serving files from the .well-known directory
// You might read the file from the file system or construct a response directly
const filePath = `./static${url.pathname}`;
const file = await Deno.readFile(filePath);
return new Response(file, {
headers: {
// Set appropriate headers, such as Content-Type
},
});
}
// Continue with the regular request handling if not in the .well-known directory
return ctx.next();
}
// routes/_middleware.ts or a specific route like routes/.well-known/mod.ts

import { MiddlewareHandlerContext } from "$fresh/server.ts";

export async function handler(
req: Request,
ctx: MiddlewareHandlerContext
) {
const url = new URL(req.url);
if (url.pathname.startsWith("/.well-known/")) {
// Handle serving files from the .well-known directory
// You might read the file from the file system or construct a response directly
const filePath = `./static${url.pathname}`;
const file = await Deno.readFile(filePath);
return new Response(file, {
headers: {
// Set appropriate headers, such as Content-Type
},
});
}
// Continue with the regular request handling if not in the .well-known directory
return ctx.next();
}
This is a very basic example and you would need to adjust it to fit your specific use case, including error handling and setting the correct MIME types in the response headers. Since this is not directly from the Deno documentation, please make sure to refer to the Fresh documentation and Deno Deploy documentation for more accurate and detailed information on how to implement routing and static
isAdrisal
isAdrisal4mo ago
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.
Adam Argyle
Adam Argyle4mo ago
ty for this, confirms my suspicions on implementations. i'll give a whirl soon and see if it passes. thanks for taking the time!