-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.ts
50 lines (43 loc) · 1.37 KB
/
serve.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Server from "lume/core/server.ts";
import { load } from "std/dotenv/mod.ts";
const env = await load();
const server = new Server({
root: `${Deno.cwd()}/_site`,
});
const getPath = (request: Request) => (new URL(request.url).pathname);
const slackWebhookURL = Deno.env.get("SLACK_WEBHOOK_URL") ||
env["SLACK_WEBHOOK_URL"];
server.use(async (request, next) => {
if (request.method == "POST" && getPath(request) === "/slack") {
const body = await request.text();
const response = await fetch(slackWebhookURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body,
});
if (!response.ok) {
console.error(response);
}
return new Response(body, { status: response.ok ? 200 : 500 });
}
return await next(request);
});
server.use(async (request, next) => {
if (request.method == "GET" && getPath(request) === "/api/docs.json") {
return await fetch("https://api.clipcrow.dev/docs.json");
}
return await next(request);
});
server.use(async (request, next) => {
const response = await next(request);
const { headers, status } = response;
if (status === 404) {
headers.set("content-type", "text/html; charset=utf-8");
const body = await Deno.readFile(`${Deno.cwd()}/_site/404/index.html`);
return new Response(body, { status, headers });
}
return response;
});
server.start();