import { config as dotenv } from "https://deno.land/x/dotenv@v3.2.0/mod.ts";
import { BASE_URL, Collection, createRestManager } from "./mod.ts";
dotenv({ export: true, path: `${Deno.cwd()}/.env` });
const col = new Collection<string, number>();
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;
const rest = createRestManager({ token, secretKey: REST_AUTHORIZATION_KEY, customUrl: PROXY_REST_URL, debug(text) { if (text.startsWith("[REST - RequestCreate]")) { const aaa = text.split(" "); const method = aaa[4]; const url = aaa[7];
col.set(method + url, Date.now());
}
if (text.startsWith("[REST - processGlobalQueue] rate limited, running setTimeout.")) { console.log("[POSSIBLE BUCKET ISSUE]"); } }, fetching(options) { },});
const server = Deno.listen({ port: REST_PORT });console.log( `HTTP webserver running. Access it at: ${PROXY_REST_URL}`,);
for await (const conn of server) { handleRequest(conn);}
async function handleRequest(conn: Deno.Conn) { const httpConn = Deno.serveHttp(conn); 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";