summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Server.ts64
-rw-r--r--src/index.ts4
-rw-r--r--src/models/Guild.ts.disabled59
-rw-r--r--src/models/Snowflake.ts1
-rw-r--r--src/models/database.ts0
-rw-r--r--src/routes/api/v8/channel/#CHANNELID/index.ts0
-rw-r--r--src/routes/api/v8/guilds/index.ts0
-rw-r--r--src/test.ts10
-rw-r--r--src/test/db_benchmark.ts55
9 files changed, 132 insertions, 61 deletions
diff --git a/src/Server.ts b/src/Server.ts

index 92c73a35..39b1fa56 100644 --- a/src/Server.ts +++ b/src/Server.ts
@@ -2,27 +2,26 @@ import express, { Application, Router } from "express"; import { traverseDirectory } from "./Utils"; import { Server as HTTPServer } from "http"; import fs from "fs/promises"; +import { Server, ServerOptions } from "lambert-server"; -export type ServerOptions = { - port: number; -}; +export interface DiscordServerOptions extends ServerOptions {} -export class Server { - private app: Application; - private http: HTTPServer; - private options: ServerOptions; - private routes: Router[]; - private initalized: Promise<any>; - - constructor(opts: ServerOptions = { port: 8080 }) { - this.options = opts; +declare global { + namespace Express { + interface Request { + server: DiscordServer; + } + } +} - this.app = express(); +export class DiscordServer extends Server { + public options: DiscordServerOptions; - this.initalized = this.init(); + constructor(opts?: DiscordServerOptions) { + super(opts); } - async init() { + async start() { // recursively loads files in routes/ this.routes = await this.registerRoutes(__dirname + "/routes/"); // const indexHTML = await (await fetch("https://discord.com/app")).buffer(); @@ -33,39 +32,6 @@ export class Server { res.set("content-type", "text/html"); res.send(indexHTML); }); - } - - async start() { - await this.initalized; - await new Promise<void>((res) => this.app.listen(this.options.port, () => res())); - console.log(`[Server] started on ${this.options.port}`); - } - - async registerRoutes(root: string) { - return await traverseDirectory({ dirname: root, recursive: true }, this.registerRoute.bind(this, root)); - } - - registerRoute(root: string, file: string): any { - if (root.endsWith("/") || root.endsWith("\\")) root = root.slice(0, -1); // removes slash at the end of the root dir - let path = file.replace(root, ""); // remove root from path and - path = path.split(".").slice(0, -1).join("."); // trancate .js/.ts file extension of path - if (path.endsWith("/index")) path = path.slice(0, -6); // delete index from path - - try { - var router = require(file); - if (router.router) router = router.router; - if (router.default) router = router.default; - if (!router || router?.prototype?.constructor?.name !== "router") - throw `File doesn't export any default router`; - this.app.use(path, <Router>router); - console.log(`[Server] Route ${path} registerd`); - return router; - } catch (error) { - console.error(new Error(`[Server] Failed to register route ${path}: ${error}`)); - } - } - - async stop() { - return new Promise<void>((res) => this.http.close(() => res())); + return super.start(); } } diff --git a/src/index.ts b/src/index.ts
index 8765d6dc..2f8aa185 100644 --- a/src/index.ts +++ b/src/index.ts
@@ -1,4 +1,4 @@ -import { Server } from "./Server"; +import { DiscordServer } from "./Server"; -const server = new Server(); +const server = new DiscordServer(); server.start().catch(console.error); diff --git a/src/models/Guild.ts.disabled b/src/models/Guild.ts.disabled new file mode 100644
index 00000000..5cffa9b8 --- /dev/null +++ b/src/models/Guild.ts.disabled
@@ -0,0 +1,59 @@ +import { Snowflake } from "./Snowflake"; + +export interface Guild { + afkChannel?: Snowflake; + afkTimeout: number; + onlineCount: number; + available: boolean; + banner: string | null; + channels: GuildChannelManager; + readonly createdTimestamp: number; + defaultMessageNotifications: DefaultMessageNotifications | number; + deleted: boolean; + description: string | null; + discoverySplash: string | null; + embedChannel: GuildChannel | null; + embedChannelID: Snowflake | null; + embedEnabled: boolean; + emojis: GuildEmojiManager; + explicitContentFilter: ExplicitContentFilterLevel; + features: GuildFeatures[]; + icon: string | null; + id: Snowflake; + joinedTimestamp: number; + large: boolean; + maximumMembers: number | null; + maximumPresences: number | null; + memberCount: number; + members: GuildMemberManager; + mfaLevel: number; + name: string; + readonly nameAcronym: string; + readonly owner: Snowflake | null; + ownerID: Snowflake; + readonly partnered: boolean; + preferredLocale: string; + premiumSubscriptionCount: number | null; + premiumTier: PremiumTier; + presences: PresenceManager; + readonly publicUpdatesChannel: TextChannel | null; + publicUpdatesChannelID: Snowflake | null; + region: string; + roles: RoleManager; + readonly rulesChannel: TextChannel | null; + rulesChannelID: Snowflake | null; + readonly shard: WebSocketShard; + shardID: number; + splash: string | null; + readonly systemChannel: TextChannel | null; + systemChannelFlags: Readonly<SystemChannelFlags>; + systemChannelID: Snowflake | null; + vanityURLCode: string | null; + vanityURLUses: number | null; + verificationLevel: VerificationLevel; + readonly verified: boolean; + readonly voiceStates: VoiceStateManager; + readonly widgetChannel: TextChannel | null; + widgetChannelID: Snowflake | null; + widgetEnabled: boolean | null; +} diff --git a/src/models/Snowflake.ts b/src/models/Snowflake.ts new file mode 100644
index 00000000..02e6df3a --- /dev/null +++ b/src/models/Snowflake.ts
@@ -0,0 +1 @@ +export type Snowflake = string; diff --git a/src/models/database.ts b/src/models/database.ts new file mode 100644
index 00000000..e69de29b --- /dev/null +++ b/src/models/database.ts
diff --git a/src/routes/api/v8/channel/#CHANNELID/index.ts b/src/routes/api/v8/channel/#CHANNELID/index.ts new file mode 100644
index 00000000..e69de29b --- /dev/null +++ b/src/routes/api/v8/channel/#CHANNELID/index.ts
diff --git a/src/routes/api/v8/guilds/index.ts b/src/routes/api/v8/guilds/index.ts new file mode 100644
index 00000000..e69de29b --- /dev/null +++ b/src/routes/api/v8/guilds/index.ts
diff --git a/src/test.ts b/src/test.ts deleted file mode 100644
index 8dc237d9..00000000 --- a/src/test.ts +++ /dev/null
@@ -1,10 +0,0 @@ -import { r } from "rethinkdb-ts"; - -async function main() { - await r.connectPool(); - - const result = await r.db("test").tableCreate("authors").run(); - console.log(result); -} - -main(); diff --git a/src/test/db_benchmark.ts b/src/test/db_benchmark.ts new file mode 100644
index 00000000..74623e06 --- /dev/null +++ b/src/test/db_benchmark.ts
@@ -0,0 +1,55 @@ +// @ts-nocheck +import { r } from "rethinkdb-ts"; +import faker from "faker"; +import cluster from "cluster"; +import { performance } from "perf_hooks"; + +console.log("starting"); + +if (cluster.isMaster) { + for (var i = 0; i < 1; i++) { + cluster.fork(); + } + console.log("all nodes started"); +} + +if (cluster.isWorker) { + const inserts = []; + + for (let i = 0; i < 100; i++) { + inserts.push({ + color: faker.commerce.color(), + department: faker.commerce.department(), + price: faker.commerce.price(), + product: faker.commerce.product(), + productAdjective: faker.commerce.productAdjective(), + productName: faker.commerce.productName(), + productMaterial: faker.commerce.productMaterial(), + productDescription: faker.commerce.productDescription(), + }); + } + + async function main(connection) { + const start = performance.now(); + await r + .db("test") + .table("test") + .nth(Math.floor(Math.random() * 300000)) + .run(connection); + const end = performance.now(); + // console.log(end - start); + + // await main(connection); + setTimeout(main.bind(null, connection)); + } + + (async () => { + const threads = 30; + + for (var i = 0; i < threads; i++) { + setTimeout(async () => { + main(await r.connect({ port: 28015, host: "192.168.178.122" })); + }); + } + })(); +}