1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-07 11:22:35 +01:00

🎨 move test client in different file

This commit is contained in:
Flam3rboy 2021-08-09 12:58:05 +02:00
parent c553a61065
commit 3df58261fa
2 changed files with 79 additions and 77 deletions

View File

@ -10,10 +10,10 @@ import i18nextBackend from "i18next-node-fs-backend";
import { ErrorHandler } from "./middlewares/ErrorHandler";
import { BodyParser } from "./middlewares/BodyParser";
import express, { Router, Request, Response } from "express";
import fetch, { Response as FetchResponse } from "node-fetch";
import mongoose from "mongoose";
import path from "path";
import RateLimit from "./middlewares/RateLimit";
import TestClient from "./middlewares/TestClient";
// this will return the new updated document for findOneAndUpdate
mongoose.set("returnOriginal", false); // https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate
@ -29,14 +29,6 @@ declare global {
}
}
const assetCache = new Map<
string,
{
response: FetchResponse;
buffer: Buffer;
}
>();
export class FosscordServer extends Server {
public declare options: FosscordServerOptions;
@ -90,21 +82,21 @@ export class FosscordServer extends Server {
this.app.use(i18nextMiddleware.handle(i18next, {}));
const app = this.app;
const prefix = Router();
const api = Router();
// @ts-ignore
this.app = prefix;
prefix.use(RateLimit({ bucket: "global", count: 10, window: 5, bot: 250 }));
prefix.use(RateLimit({ bucket: "error", count: 5, error: true, window: 5, bot: 15, onylIp: true }));
prefix.use("/guilds/:id", RateLimit({ count: 5, window: 5 }));
prefix.use("/webhooks/:id", RateLimit({ count: 5, window: 5 }));
prefix.use("/channels/:id", RateLimit({ count: 5, window: 5 }));
this.app = api;
api.use(RateLimit({ bucket: "global", count: 10, window: 5, bot: 250 }));
api.use(RateLimit({ bucket: "error", count: 5, error: true, window: 5, bot: 15, onylIp: true }));
api.use("/guilds/:id", RateLimit({ count: 5, window: 5 }));
api.use("/webhooks/:id", RateLimit({ count: 5, window: 5 }));
api.use("/channels/:id", RateLimit({ count: 5, window: 5 }));
this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/"));
app.use("/api/v8", prefix);
app.use("/api/v9", prefix);
app.use("/api", prefix); // allow unversioned requests
app.use("/api/v8", api);
app.use("/api/v9", api);
app.use("/api", api); // allow unversioned requests
prefix.get("*", (req: Request, res: Response) => {
api.get("*", (req: Request, res: Response) => {
res.status(404).json({
message: "404: Not Found",
code: 0
@ -113,64 +105,8 @@ export class FosscordServer extends Server {
this.app = app;
this.app.use(ErrorHandler);
const indexHTML = fs.readFileSync(path.join(__dirname, "..", "client_test", "index.html"), { encoding: "utf8" });
TestClient(this.app);
this.app.use("/assets", express.static(path.join(__dirname, "..", "assets")));
this.app.get("/assets/:file", async (req: Request, res: Response) => {
delete req.headers.host;
var response: FetchResponse;
var buffer: Buffer;
const cache = assetCache.get(req.params.file);
if (!cache) {
response = await fetch(`https://discord.com/assets/${req.params.file}`, {
// @ts-ignore
headers: {
...req.headers
}
});
buffer = await response.buffer();
} else {
response = cache.response;
buffer = cache.buffer;
}
response.headers.forEach((value, name) => {
if (
[
"content-length",
"content-security-policy",
"strict-transport-security",
"set-cookie",
"transfer-encoding",
"expect-ct",
"access-control-allow-origin",
"content-encoding"
].includes(name.toLowerCase())
) {
return;
}
res.set(name, value);
});
assetCache.set(req.params.file, { buffer, response });
return res.send(buffer);
});
this.app.get("*", (req: Request, res: Response) => {
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
res.set("content-type", "text/html");
res.send(
indexHTML
.replace(
/CDN_HOST: ".+"/,
`CDN_HOST: "${(Config.get().cdn.endpoint || process.env.CDN || "http://localhost:3003").replace(/https?:/, "")}"`
)
.replace(
/GATEWAY_ENDPOINT: ".+"/,
`GATEWAY_ENDPOINT: "${Config.get().gateway.endpoint || process.env.GATEWAY || "ws://localhost:3002"}"`
)
);
});
return super.start();
}
}

View File

@ -0,0 +1,66 @@
import bodyParser, { OptionsJson } from "body-parser";
import express, { NextFunction, Request, Response, Application } from "express";
import { HTTPError } from "lambert-server";
import fs from "fs";
import path from "path";
import fetch, { Response as FetchResponse } from "node-fetch";
import { Config } from "@fosscord/server-util";
export default function TestClient(app: Application) {
const assetCache = new Map<string, { response: FetchResponse; buffer: Buffer }>();
const indexHTML = fs.readFileSync(path.join(__dirname, "..", "..", "client_test", "index.html"), { encoding: "utf8" });
app.use("/assets", express.static(path.join(__dirname, "..", "assets")));
app.get("/assets/:file", async (req: Request, res: Response) => {
delete req.headers.host;
var response: FetchResponse;
var buffer: Buffer;
const cache = assetCache.get(req.params.file);
if (!cache) {
response = await fetch(`https://discord.com/assets/${req.params.file}`, {
// @ts-ignore
headers: {
...req.headers
}
});
buffer = await response.buffer();
} else {
response = cache.response;
buffer = cache.buffer;
}
response.headers.forEach((value, name) => {
if (
[
"content-length",
"content-security-policy",
"strict-transport-security",
"set-cookie",
"transfer-encoding",
"expect-ct",
"access-control-allow-origin",
"content-encoding"
].includes(name.toLowerCase())
) {
return;
}
res.set(name, value);
});
assetCache.set(req.params.file, { buffer, response });
return res.send(buffer);
});
app.get("*", (req: Request, res: Response) => {
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
res.set("content-type", "text/html");
var html = indexHTML;
const CDN_ENDPOINT = Config.get().cdn.endpoint || process.env.CDN;
const GATEWAY_ENDPOINT = Config.get().gateway.endpoint || process.env.GATEWAY;
if (CDN_ENDPOINT) html = html.replace(/CDN_HOST: .+/, `CDN_HOST: "${CDN_ENDPOINT}",`);
if (GATEWAY_ENDPOINT) html = html.replace(/GATEWAY_ENDPOINT: .+/, `GATEWAY_ENDPOINT: "${GATEWAY_ENDPOINT}",`);
res.send(html);
});
}