-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
95 lines (87 loc) · 2.87 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { add } from "./api/add.ts";
import { getAlias, getId } from "./api/get.ts";
import { notFound, redirect } from "./util/responses.ts";
import { Index } from "./pages/index.tsx";
import { Api } from "./pages/api.tsx";
import { Admin } from "./pages/admin.tsx";
import { Alias } from "./pages/alias.tsx";
import {
ALIAS_NAME_REGEX,
ALIAS_PATH,
EXTENSION_FROM_CONTENT_TYPE,
ID_PATH,
} from "./util/constants.ts";
import { Match, router, serve } from "./deps.ts";
import { decodeUTF8, readToUint8Array } from "./util/util.ts";
import { Code } from "./pages/code.tsx";
import { request } from "./api/alias/request.ts";
import { release } from "./api/alias/release.ts";
import { getIdFromAlias } from "./util/alias.ts";
import { login } from "./api/login.ts";
import { callback } from "./api/login/callback.ts";
import {
completionsAlias,
completionsSchema,
completionsTags,
} from "./api/completions.ts";
import { approve } from "./api/alias/approve.ts";
import { deny } from "./api/alias/deny.ts";
import { requests } from "./api/alias/requests.ts";
import { list } from "./api/alias/list.ts";
import { S3_CLIENT } from "./util/clients.ts";
import { jsx } from "./util/jsx.ts";
import { Head } from "./components/head.tsx";
async function unknownHandler(
req: Request,
match: Match,
): Promise<Response> {
let { id } = match.params;
const { alias, tag } = match.params;
const accept = req.headers.get("accept");
const isHtml = accept && accept.indexOf("html") >= 0;
if (isHtml) {
if (alias !== undefined && tag !== undefined) {
id = await getIdFromAlias(alias, tag);
}
if (id === undefined) {
return notFound();
}
const file = await S3_CLIENT.getObject(id);
if (file === undefined) {
return notFound();
}
const body = await readToUint8Array(file.body);
const code = decodeUTF8(body);
const language = EXTENSION_FROM_CONTENT_TYPE[
file.contentType! as keyof typeof EXTENSION_FROM_CONTENT_TYPE
];
return jsx(Code({ code, language }), Head());
} else {
return redirect(`/api/get${match.path}`);
}
}
serve(router({
"/": (_req) => jsx(Index(), Head()),
"/admin": (_req) => jsx(Admin(), Head()),
"/alias": (_req) => jsx(Alias(), Head()),
"/api": (_req) => jsx(Api(), Head()),
"/api/login": login,
"/api/login/callback": callback,
"/api/add": add,
"/api/alias/request": request,
"/api/alias/release": release,
"/api/alias/requests": requests,
"/api/alias/approve": approve,
"/api/alias/deny": deny,
"/api/alias/list": list,
"/api/completions": completionsAlias,
[`/api/completions/:alias(${ALIAS_NAME_REGEX.source})/tags`]:
completionsTags,
[`/api/get/${ID_PATH}`]: getId,
[`/api/get/${ALIAS_PATH}`]: getAlias,
[`/${ID_PATH}`]: unknownHandler,
[`/${ALIAS_PATH}`]: unknownHandler,
"/.well-known/deno-import-intellisense.json": completionsSchema,
}, (_req) => {
return notFound();
}));