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

🚧 fix server bundle

This commit is contained in:
Flam3rboy 2021-08-13 22:57:46 +02:00
parent f70a24ab6a
commit 194ef5478e
10 changed files with 96 additions and 28 deletions

View File

@ -0,0 +1,10 @@
import { Router, Response, Request } from "express";
const router = Router();
router.post("/", (req: Request, res: Response) => {
// TODO:
res.sendStatus(204);
});
export default router;

18
bundle/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"sourceMaps": true,
"type": "node",
"request": "launch",
"name": "Launch server bundle",
"program": "${workspaceFolder}/dist/start.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"envFile": "${workspaceFolder}/.env"
}
]
}

View File

@ -7,6 +7,7 @@
"": { "": {
"name": "@fosscord/server", "name": "@fosscord/server",
"version": "1.0.0", "version": "1.0.0",
"hasInstallScript": true,
"license": "AGPLV3", "license": "AGPLV3",
"dependencies": { "dependencies": {
"@fosscord/api": "file:../api", "@fosscord/api": "file:../api",
@ -28,7 +29,6 @@
"../api": { "../api": {
"name": "@fosscord/api", "name": "@fosscord/api",
"version": "1.0.0", "version": "1.0.0",
"hasInstallScript": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@fosscord/util": "file:../util", "@fosscord/util": "file:../util",
@ -85,7 +85,6 @@
"../cdn": { "../cdn": {
"name": "@fosscord/cdn", "name": "@fosscord/cdn",
"version": "1.0.0", "version": "1.0.0",
"hasInstallScript": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@fosscord/util": "file:../util", "@fosscord/util": "file:../util",
@ -121,7 +120,6 @@
"../gateway": { "../gateway": {
"name": "@fosscord/gateway", "name": "@fosscord/gateway",
"version": "1.0.0", "version": "1.0.0",
"hasInstallScript": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@fosscord/util": "file:../util", "@fosscord/util": "file:../util",
@ -152,7 +150,13 @@
"version": "1.3.55", "version": "1.3.55",
"hasInstallScript": true, "hasInstallScript": true,
"license": "GPLV3", "license": "GPLV3",
"dependencies": { "devDependencies": {
"@types/amqplib": "^0.8.1",
"@types/jsonwebtoken": "^8.5.0",
"@types/mongodb": "^3.6.9",
"@types/mongoose-autopopulate": "^0.10.1",
"@types/mongoose-lean-virtuals": "^0.5.1",
"@types/node": "^14.17.9",
"ajv": "^8.5.0", "ajv": "^8.5.0",
"amqplib": "^0.8.0", "amqplib": "^0.8.0",
"dot-prop": "^6.0.1", "dot-prop": "^6.0.1",
@ -161,15 +165,7 @@
"missing-native-js-functions": "^1.2.2", "missing-native-js-functions": "^1.2.2",
"mongodb": "^3.6.9", "mongodb": "^3.6.9",
"mongoose": "^5.13.7", "mongoose": "^5.13.7",
"mongoose-autopopulate": "^0.12.3" "mongoose-autopopulate": "^0.12.3",
},
"devDependencies": {
"@types/amqplib": "^0.8.1",
"@types/jsonwebtoken": "^8.5.0",
"@types/mongodb": "^3.6.9",
"@types/mongoose-autopopulate": "^0.10.1",
"@types/mongoose-lean-virtuals": "^0.5.1",
"@types/node": "^14.17.9",
"typescript": "^4.1.3" "typescript": "^4.1.3"
} }
}, },

View File

@ -4,7 +4,7 @@
"description": "", "description": "",
"main": "src/start.js", "main": "src/start.js",
"scripts": { "scripts": {
"postinstall": "cd ../api/ && npm i && cd ../cdn/ && npm i && cd ../gateway/ && npm i && ", "postinstall": "cd ../api/ && npm i && cd ../cdn/ && npm i && cd ../gateway/ && npm i",
"build": "npm run build:api && npm run build:cdn && npm run build:gateway && npm run build:bundle", "build": "npm run build:api && npm run build:cdn && npm run build:gateway && npm run build:bundle",
"build:bundle": "tsc -b .", "build:bundle": "tsc -b .",
"build:api": "cd ../api/ && npm run build", "build:api": "cd ../api/ && npm run build",

View File

@ -9,9 +9,10 @@ import express from "express";
import { Config } from "../../util/dist"; import { Config } from "../../util/dist";
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer();
const port = Number(process.env.PORT) || 8080; const port = Number(process.env.PORT) || 8080;
const production = true; const production = true;
server.on("request", app);
// @ts-ignore // @ts-ignore
const api = new APIServer({ server, port, production, app }); const api = new APIServer({ server, port, production, app });
@ -22,8 +23,14 @@ const gateway = new GatewayServer({ server, port, production });
async function main() { async function main() {
await Config.set({ await Config.set({
cdn: { endpointClientKeepDefault: true, endpoint: `http://localhost:${port}` }, cdn: {
gateway: { endpointClientKeepDefault: true, endpoint: `ws://localhost:${port}` }, endpointClient: "${location.host}",
endpoint: `http://localhost:${port}`,
},
gateway: {
endpointClient: '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}',
endpoint: `ws://localhost:${port}`,
},
}); });
await api.start(); await api.start();

View File

@ -0,0 +1,29 @@
{
"folders": [
{
"path": "api"
},
{
"path": "dashboard"
},
{
"path": "bundle"
},
{
"path": "cdn"
},
{
"path": "gateway"
},
{
"path": "rtc"
},
{
"path": "util"
},
{
"path": "webrtc"
}
],
"settings": {}
}

View File

@ -23,6 +23,8 @@ export class Server {
}); });
this.server.on("upgrade", (request, socket, head) => { this.server.on("upgrade", (request, socket, head) => {
console.log("socket requests upgrade", request.url);
// @ts-ignore
this.ws.handleUpgrade(request, socket, head, (socket) => { this.ws.handleUpgrade(request, socket, head, (socket) => {
this.ws.emit("connection", socket, request); this.ws.emit("connection", socket, request);
}); });

View File

@ -58,13 +58,14 @@ export async function setupListener(this: WebSocket) {
this.permissions[guild] = x; this.permissions[guild] = x;
this.listeners; this.listeners;
this.events[guild] = await listenEvent(guild, consumer, opts); this.events[guild] = await listenEvent(guild, consumer, opts);
for (const channel of guild_channels) {
for (const channel of guild_channels.filter((c) => c.guild_id === guild)) {
if (x.overwriteChannel(channel.permission_overwrites).has("VIEW_CHANNEL")) { if (x.overwriteChannel(channel.permission_overwrites).has("VIEW_CHANNEL")) {
this.events[channel.id] = await listenEvent(channel.id, consumer, opts); this.events[channel.id] = await listenEvent(channel.id, consumer, opts);
} }
} }
}) })
.catch((e) => {}); .catch((e) => console.log("couldn't get permission for guild " + guild, e));
} }
this.once("close", () => { this.once("close", () => {
@ -74,14 +75,14 @@ export async function setupListener(this: WebSocket) {
} }
// TODO: only subscribe for events that are in the connection intents // TODO: only subscribe for events that are in the connection intents
function consume(this: WebSocket, opts: EventOpts) { async function consume(this: WebSocket, opts: EventOpts) {
const { data, event } = opts; const { data, event } = opts;
const id = data.id as string; const id = data.id as string;
const permission = this.permissions[id] || new Permissions("ADMINISTRATOR"); // default permission for dm const permission = this.permissions[id] || new Permissions("ADMINISTRATOR"); // default permission for dm
const consumer = consume.bind(this); const consumer = consume.bind(this);
const listenOpts = opts as ListenEventOpts; const listenOpts = opts as ListenEventOpts;
console.log("event", event); // console.log("event", event);
// subscription managment // subscription managment
switch (event) { switch (event) {
@ -94,14 +95,14 @@ function consume(this: WebSocket, opts: EventOpts) {
if (!permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) return; if (!permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) return;
// TODO: check if user has permission to channel // TODO: check if user has permission to channel
case "GUILD_CREATE": case "GUILD_CREATE":
listenEvent(id, consumer, listenOpts); this.events[id] = await listenEvent(id, consumer, listenOpts);
break; break;
case "CHANNEL_UPDATE": case "CHANNEL_UPDATE":
const exists = this.events[id]; const exists = this.events[id];
// @ts-ignore // @ts-ignore
if (permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) { if (permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) {
if (exists) break; if (exists) break;
listenEvent(id, consumer, listenOpts); this.events[id] = await listenEvent(id, consumer, listenOpts);
} else { } else {
if (!exists) return; // return -> do not send channel update events for hidden channels if (!exists) return; // return -> do not send channel update events for hidden channels
opts.cancel(id); opts.cancel(id);

View File

@ -43,11 +43,11 @@ export interface KafkaBroker {
export interface DefaultOptions { export interface DefaultOptions {
gateway: { gateway: {
endpointClientKeepDefault?: boolean; endpointClient: string | null;
endpoint: string | null; endpoint: string | null;
}; };
cdn: { cdn: {
endpointClientKeepDefault?: boolean; endpointClient: string | null;
endpoint: string | null; endpoint: string | null;
}; };
general: { general: {
@ -150,11 +150,11 @@ export interface DefaultOptions {
export const DefaultOptions: DefaultOptions = { export const DefaultOptions: DefaultOptions = {
gateway: { gateway: {
endpointClientKeepDefault: false, endpointClient: null,
endpoint: null, endpoint: null,
}, },
cdn: { cdn: {
endpointClientKeepDefault: false, endpointClient: null,
endpoint: null, endpoint: null,
}, },
general: { general: {

View File

@ -16,6 +16,7 @@ export async function emitEvent(payload: Omit<Event, "created_at">) {
const successful = RabbitMQ.channel?.publish(id, "", Buffer.from(`${data}`), { type: payload.event }); const successful = RabbitMQ.channel?.publish(id, "", Buffer.from(`${data}`), { type: payload.event });
if (!successful) throw new Error("failed to send event"); if (!successful) throw new Error("failed to send event");
} else { } else {
console.log("emit event", id);
events.emit(id, payload); events.emit(id, payload);
} }
} }
@ -44,7 +45,11 @@ export async function listenEvent(event: string, callback: (event: EventOpts) =>
// @ts-ignore // @ts-ignore
return rabbitListen(opts?.channel || RabbitMQ.channel, event, callback, { acknowledge: opts?.acknowledge }); return rabbitListen(opts?.channel || RabbitMQ.channel, event, callback, { acknowledge: opts?.acknowledge });
} else { } else {
const cancel = () => events.removeListener(event, callback); const cancel = () => {
console.log("cancel event", event);
events.removeListener(event, callback);
};
console.log("listen event", event);
events.addListener(event, (opts) => callback({ ...opts, cancel })); events.addListener(event, (opts) => callback({ ...opts, cancel }));
return cancel; return cancel;