Refactor server variable to webserver and add new API endpoints for script handling
Some checks failed
Code quality checks / biome (push) Failing after 8s

This commit is contained in:
Seth 2025-05-03 13:39:01 -04:00
parent 09e377b9d2
commit 6a3354f597
2 changed files with 23 additions and 4 deletions

View file

@ -29,7 +29,7 @@ if (!development) {
await buildWeb() await buildWeb()
} }
const server = serve({ const webserver = serve({
routes: { routes: {
"/": async () => { "/": async () => {
if (development) { if (development) {
@ -128,8 +128,9 @@ const server = serve({
return new Response("Upgrade failed", { status: 500 }); return new Response("Upgrade failed", { status: 500 });
}, },
"/api/gc": () => { "/api/gc": async (req, server) => {
gc(true) gc(true)
return new Response(gzipSync(JSON.stringify({ data: "triggered" })), { return new Response(gzipSync(JSON.stringify({ data: "triggered" })), {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -138,6 +139,23 @@ const server = serve({
} }
}) })
}, },
"/api/script.js": async () => {
const req = await fetch("https://plausible.creations.works/js/script.js")
const script = await req.text()
return new Response(gzipSync(script), {
headers: {
"Content-Type": "application/javascript",
"Content-Encoding": "gzip",
"Cache-Control": "public, max-age=31536000",
}
})
},
"/api/script": async (req) => {
const request = new Request(req);
request.headers.delete('cookie');
return await fetch("https://plausible.creations.works/api/event", request);
}
}, },
websocket: { websocket: {
open: async (ws) => { open: async (ws) => {
@ -166,7 +184,7 @@ const lanyardSocket = new WebSocket("wss://lanyard.creations.works/socket");
const setLanyard = (data: object) => { const setLanyard = (data: object) => {
lanyard = data; lanyard = data;
return server.publish("lanyard", JSON.stringify({ type: "lanyard", data }), true); return webserver.publish("lanyard", JSON.stringify({ type: "lanyard", data }), true);
} }
lanyardSocket.onmessage = ({ data }) => { lanyardSocket.onmessage = ({ data }) => {
@ -198,7 +216,7 @@ let hrTimeout: ReturnType<typeof setTimeout>;
const setHeartrate = async (hr: number) => { const setHeartrate = async (hr: number) => {
heartrate = hr; heartrate = hr;
return server.publish("hyperate", JSON.stringify({ type: "hyperate", data: { hr } }), true); return webserver.publish("hyperate", JSON.stringify({ type: "hyperate", data: { hr } }), true);
} }
const setHrInterval = () => { const setHrInterval = () => {

View file

@ -18,6 +18,7 @@
<body class="scanlines"> <body class="scanlines">
<script src="index.tsx"></script> <script src="index.tsx"></script>
<script defer data-domain="yourdomain.com" data-api="/api/script" src="https://ipv4.army/api/script.js"></script>
</body> </body>
</html> </html>