Skip to content

Commit

Permalink
uhhhhhhhhhhhhhhhhhhhhhhhhhhb hgvyujhbuiyxfgh
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Dec 18, 2021
1 parent db91aa5 commit 0c12660
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
Empty file added fart_server/serve.test.ts
Empty file.
67 changes: 38 additions & 29 deletions fart_server/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,47 @@ import { redirectToDenoDeployPreviewUrl } from "./bonus_features/versions/mod.ts
import { redirectIfShortlink } from "./bonus_features/shortlinks/mod.ts";
import { getSize, inject, register } from "./utils.ts";

export const handleRequest = async (event: Deno.RequestEvent) => {
const { request, respondWith } = event;
respondWith(await inject(request));
};

// redirect to another server running a different version of the Fart library
register(redirectToDenoDeployPreviewUrl);
const middleware = [
// redirect to another server running a different version of the Fart library
redirectToDenoDeployPreviewUrl,
// redirect to an external URL
redirectIfShortlink,
// show how many handlers are registered
(request) => {
if (new URL(request.url).pathname === "/debug/size") {
return new Response(String(getSize()));
}
return null;
},
// show deployment ID if running on Deno Deploy
(request) => {
if (new URL(request.url).pathname === "/debug/deployment") {
return new Response(String(Deno.env.get("DENO_DEPLOYMENT_ID")));
}
return null;
},
];

// redirect to an external URL
register(redirectIfShortlink);
export const setup = () => {
if (getSize() === middlware.length) return;
clear();
register(...middleware);
};

// show how many handlers are registered
register((request) => {
if (new URL(request.url).pathname === "/debug/size") {
return new Response(String(getSize()));
}
return null;
});
export const handleRequest = async (event: Deno.RequestEvent) => {
setup();
event.respondWith(await inject(event.request));
};

// show deployment ID if running on Deno Deploy
register((request) => {
if (new URL(request.url).pathname === "/debug/deployment") {
return new Response(String(Deno.env.get("DENO_DEPLOYMENT_ID")));
export const serve = async () => {
const port = parseInt(Deno.env.get("PORT") || "8080");
console.log(`Access HTTP webserver at: http://localhost:${port}/`);
for await (const connection of Deno.listen({ port })) {
for await (const event of Deno.serveHttp(connection)) {
await handleRequest(event);
}
}
return null;
});
};

if (Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined) {
// add the fetch listener if running on Deno Deploy
Expand All @@ -37,11 +52,5 @@ if (Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined) {
);
} else if (import.meta.main) {
// serve the HTTP server if running locally
const port = parseInt(Deno.env.get("PORT") || "8080");
console.log(`Access HTTP webserver at: http://localhost:${port}/`);
for await (const connection of Deno.listen({ port })) {
for await (const event of Deno.serveHttp(connection)) {
await handleRequest(event);
}
}
await serve();
}
16 changes: 5 additions & 11 deletions fart_server/watch_serve.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
const makeWorker = () => {
const workerPath = new URL("./serve.ts", import.meta.url).href;
return new Worker(workerPath, { type: "module", deno: true })
const restartServer = async () => {
console.log(">>> restarting server");
const { serve } = await import("./serve.ts");
serve();
};

const resetWorker = () => {
worker.terminate();
worker = makeWorker();
}

let worker = makeWorker();

for await (const event of Deno.watchFs(".")) {
console.log(">>>> event", event);
resetWorker();
restartServer();
}

1 comment on commit 0c12660

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on 0c12660 Dec 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

failed to fetch 'https://raw.githubusercontent.com/EthanThatOneKid/fart/0c12660fd4f4a498fa6c81c4f33db730bc70e01c/fart_server/handle_request.ts': HTTP status client error (404 Not Found) for url (https://raw.githubusercontent.com/EthanThatOneKid/fart/0c12660fd4f4a498fa6c81c4f33db730bc70e01c/fart_server/handle_request.ts)

Please sign in to comment.