-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
rest.ts
110 lines (98 loc) · 3.03 KB
/
rest.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// START FILE FOR REST PROCESS
import { BASE_URL, createRestManager } from "../mod.ts";
import { dotenv } from "./deps.ts";
dotenv({ export: true, path: `${Deno.cwd()}/.env` });
const token = Deno.env.get("GAMER_TOKEN");
if (!token) throw new Error("Token was not provided.");
const REST_AUTHORIZATION_KEY = Deno.env.get("PROXY_REST_SECRET");
const PROXY_REST_URL = Deno.env.get("PROXY_REST_URL");
const REST_PORT = Number(PROXY_REST_URL?.substring(PROXY_REST_URL.lastIndexOf(":") + 1)) ?? 8080;
// CREATES THE FUNCTIONALITY FOR MANAGING THE REST REQUESTS
const rest = createRestManager({
token,
secretKey: REST_AUTHORIZATION_KEY,
customUrl: PROXY_REST_URL,
});
// START LISTENING TO THE URL(localhost)
const server = Deno.listen({ port: REST_PORT });
console.log(
`Rest Proxy running. Access it at: ${PROXY_REST_URL}`,
);
// Connections to the server will be yielded up as an async iterable.
for await (const conn of server) {
// In order to not be blocking, we need to handle each connection individually
// in its own async function.
handleRequest(conn);
}
async function handleRequest(conn: Deno.Conn) {
// This "upgrades" a network connection into an HTTP connection.
const httpConn = Deno.serveHttp(conn);
// Each request sent over the HTTP connection will be yielded as an async
// iterator from the HTTP connection.
for await (const requestEvent of httpConn) {
if (
!REST_AUTHORIZATION_KEY ||
REST_AUTHORIZATION_KEY !==
requestEvent.request.headers.get("AUTHORIZATION")
) {
return requestEvent.respondWith(
new Response(JSON.stringify({ error: "Invalid authorization key." }), {
status: 401,
}),
);
}
try {
const text = await requestEvent.request.text();
const json = text ? JSON.parse(text) : undefined;
if (json?.file) {
json.file = await Promise.all(json.file.map(async (f: any) => ({
name: f.name,
blob: await (await fetch(f.blob)).blob(),
})));
}
const result = await rest.runMethod(
rest,
requestEvent.request.method as RequestMethod,
`${BASE_URL}${
requestEvent.request.url.substring(
`http://localhost:${REST_PORT}`.length,
)
}`,
json,
);
if (result) {
requestEvent.respondWith(
new Response(JSON.stringify(result), {
status: 200,
}),
);
} else {
requestEvent.respondWith(
new Response(undefined, {
status: 204,
}),
);
}
} catch (error) {
console.log(
"CATCH",
requestEvent.request.url,
requestEvent.request.method,
requestEvent.request.body,
error.code,
error,
);
requestEvent.respondWith(
new Response(
JSON.stringify({
message: error.message,
}),
{
status: error.code ?? 469,
},
),
);
}
}
}
type RequestMethod = "POST" | "PUT" | "DELETE" | "PATCH";