RabbitMQ

This commit is contained in:
Flam3rboy 2021-08-12 16:47:51 +02:00
parent 2c9a661dd8
commit 675ef382ae
5 changed files with 7 additions and 7 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -33,7 +33,7 @@
},
"homepage": "https://github.com/fosscord/fosscord-api#readme",
"dependencies": {
"@fosscord/server-util": "^1.3.49",
"@fosscord/server-util": "^1.3.51",
"@types/jest": "^26.0.22",
"@types/json-schema": "^7.0.7",
"ajv": "^8.4.0",

View File

@ -3,7 +3,7 @@ import fs from "fs";
import { Connection } from "mongoose";
import { Server, ServerOptions } from "lambert-server";
import { Authentication, CORS } from "./middlewares/";
import { Config, db } from "@fosscord/server-util";
import { Config, db, RabbitMQ } from "@fosscord/server-util";
import i18next from "i18next";
import i18nextMiddleware, { I18next } from "i18next-http-middleware";
import i18nextBackend from "i18next-node-fs-backend";
@ -58,6 +58,7 @@ export class FosscordServer extends Server {
await this.setupSchema();
console.log("[Database] connected");
await Config.init();
await RabbitMQ.init();
this.app.use(CORS);
this.app.use(Authentication);

View File

@ -113,8 +113,6 @@ router.post("/", messageUpload.single("file"), async (req: Request, res: Respons
var body = req.body as MessageCreateSchema;
const attachments: Attachment[] = [];
console.log(body);
if (req.file) {
try {
const file = await uploadFile(`/attachments/${channel_id}`, req.file);

View File

@ -1,13 +1,14 @@
import { Config, Event, EventModel, rabbitCon, rabbitCh } from "@fosscord/server-util";
import { Config, Event, EventModel, RabbitMQ } from "@fosscord/server-util";
export async function emitEvent(payload: Omit<Event, "created_at">) {
if (rabbitCon) {
if (RabbitMQ.connection) {
const id = (payload.channel_id || payload.user_id || payload.guild_id) as string;
if (!id) console.error("event doesn't contain any id", payload);
const data = typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.data; // use rabbitmq for event transmission
await RabbitMQ.channel?.assertQueue(id);
// assertQueue isn't needed, because a queue will automatically created if it doesn't exist
const successful = rabbitCh.sendToQueue(id, Buffer.from(`${data}`), { type: payload.event });
const successful = RabbitMQ.channel?.sendToQueue(id, Buffer.from(`${data}`), { type: payload.event });
if (!successful) throw new Error("failed to send event");
} else {
// use mongodb for event transmission