feat: cors and more headers
All checks were successful
ci / docker (push) Successful in 4m31s

This commit is contained in:
Tine 2024-06-11 17:57:04 +02:00
parent 3718e5a47f
commit 9082287ad3
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw
5 changed files with 22 additions and 31 deletions

View file

@ -2,17 +2,35 @@ import { Hono } from "https://deno.land/x/hono@v4.2.5/mod.ts";
import {
serveStatic,
logger,
cors,
etag,
secureHeaders,
cache,
} from "https://deno.land/x/hono@v4.2.5/middleware.ts";
const cache = await Deno.openKv();
const app = new Hono();
app.use(logger());
app.use(etag());
app.use(secureHeaders());
app.use(
cache({
cacheName: "tjo-space",
cacheControl: "public, max-age=3600",
wait: true,
}),
);
// Allow CORS for tjo.space
app.use(cors({ origin: "https://tjo.space" }));
// But, /.well-known requests from anywhere.
app.use("/.well-known/*", cors({ origin: "*" }));
// .well-known for matrix
app.get("/.well-known/matrix/client", (c) =>
c.json({
"m.homeserver": {
base_url: "https://matrix.chat.tjo.space",
server_name: "tjo.space",
},
}),
);
@ -58,12 +76,13 @@ app.get("/.well-known/webfinger", (c) => {
});
});
const kvCache = await Deno.openKv();
// Serve plausible analytics
app.get("/js/script.js", async (c) => {
c.header("Content-Type", "text/javascript");
// Check if we have the script cached
const cachedScript = await cache.get<string>(["plausible", "script"]);
const cachedScript = await kvCache.get<string>(["plausible", "script"]);
if (cachedScript.value) {
return c.body(cachedScript.value);
}
@ -73,7 +92,7 @@ app.get("/js/script.js", async (c) => {
// Cache for 24 hours if we got the script
if (response.status === 200) {
await cache.set(["plausible", "script"], script, {
await kvCache.set(["plausible", "script"], script, {
expireIn: 1000 * 60 * 60 * 24,
});
}

View file

@ -1,6 +0,0 @@
{
"m.homeserver": {
"base_url": "https://matrix.chat.tjo.space"
}
}

View file

@ -1,3 +0,0 @@
{
"m.server": "matrix.chat.tjo.space:443"
}

View file

@ -1,10 +0,0 @@
{
"contacts": [
{
"matrix_id": "@tine:tjo.space",
"email_address": "tine@tjo.space",
"role": "m.role.admin"
},
],
"support_page": "https://tjo.space"
}

View file

@ -1,9 +0,0 @@
{
"subject": "acct:tine@tjo.space",
"links": [
{
"rel": "http://openid.net/specs/connect/1.0/issuer",
"href": "https://id.tjo.space/application/o/tailscalecom/.well-known/openid-configuration"
}
]
}