Using pino as a log writer + Append the Generics to make Routing more scalable
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import { Client, Message, TextChannel } from 'discord.js';
|
||||
import dedent from 'ts-dedent';
|
||||
import { ImplementableApi } from './implementableApi';
|
||||
|
||||
import { once as eventsOnce } from 'events';
|
||||
@ -29,7 +28,12 @@ type ChannelsType = {
|
||||
ChannelPeerTube: TextChannel;
|
||||
};
|
||||
|
||||
class DiscordParser extends ImplementableApi {
|
||||
type TBaseDiscordMessageType = ('addListener' | 'newEntriesNotify') &
|
||||
ImplementableApi.TBaseMessageType;
|
||||
|
||||
class DiscordParser<
|
||||
T extends TBaseDiscordMessageType
|
||||
> extends ImplementableApi<T> {
|
||||
readonly botPrefix: string;
|
||||
readonly channels: {
|
||||
[key: string]: TextChannel;
|
||||
@ -46,9 +50,9 @@ class DiscordParser extends ImplementableApi {
|
||||
this.settingEvents();
|
||||
}
|
||||
|
||||
static async instanciate(
|
||||
static async instanciate<T extends TBaseDiscordMessageType>(
|
||||
config: DiscordParser.Config
|
||||
): Promise<DiscordParser.ConstructorPattern> {
|
||||
): Promise<DiscordParser<T>> {
|
||||
const client = new Client();
|
||||
client.login(config.token);
|
||||
await eventsOnce(client, 'ready');
|
||||
@ -63,12 +67,12 @@ class DiscordParser extends ImplementableApi {
|
||||
),
|
||||
};
|
||||
|
||||
return {
|
||||
return new DiscordParser<T>({
|
||||
name: config.name,
|
||||
channels: channels,
|
||||
client: client,
|
||||
botPrefix: config.keyWord,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private static getTextChannel(client: Client, id: string): TextChannel {
|
||||
@ -78,7 +82,7 @@ class DiscordParser extends ImplementableApi {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public receivedMessage(message: ImplementableApi.Message) {
|
||||
public receivedMessage(message: ImplementableApi.Message<T>) {
|
||||
switch (message.type) {
|
||||
case 'newEntriesNotify':
|
||||
this.sendMsgYtb(
|
||||
@ -114,14 +118,15 @@ class DiscordParser extends ImplementableApi {
|
||||
if (msg_splitted[0] === this.botPrefix) {
|
||||
switch (msg_splitted[1]) {
|
||||
case 'add':
|
||||
const send_message: ImplementableApi.Message = {
|
||||
rawContent: {
|
||||
address: msg_splitted[2],
|
||||
user: message.author.toString(),
|
||||
date: message.createdAt.toUTCString(),
|
||||
},
|
||||
type: 'newListener',
|
||||
};
|
||||
const send_message: ImplementableApi.Message<TBaseDiscordMessageType> =
|
||||
{
|
||||
rawContent: {
|
||||
address: msg_splitted[2],
|
||||
user: message.author.toString(),
|
||||
date: message.createdAt.toUTCString(),
|
||||
},
|
||||
type: 'addListener',
|
||||
};
|
||||
message.channel.send('Ceci est un feedback');
|
||||
this.emit('addListener', send_message);
|
||||
}
|
||||
|
@ -10,14 +10,17 @@ namespace ImplementableApi {
|
||||
* the type field permit to know which type of message it is
|
||||
* rawContent field is the string content of the message
|
||||
*/
|
||||
export type Message = {
|
||||
export type Message<T extends TBaseMessageType> = {
|
||||
idListener?: number;
|
||||
type: 'newEntriesNotify' | 'newListener';
|
||||
type: T | 'logging';
|
||||
rawContent: any;
|
||||
};
|
||||
export type TBaseMessageType = string;
|
||||
}
|
||||
|
||||
abstract class ImplementableApi extends EventEmitter {
|
||||
abstract class ImplementableApi<
|
||||
T extends ImplementableApi.TBaseMessageType
|
||||
> extends EventEmitter {
|
||||
readonly name: string;
|
||||
|
||||
constructor(readonly config: ImplementableApi.Config) {
|
||||
@ -25,7 +28,7 @@ abstract class ImplementableApi extends EventEmitter {
|
||||
this.name = config.name;
|
||||
}
|
||||
|
||||
public abstract receivedMessage(message: ImplementableApi.Message): void;
|
||||
public abstract receivedMessage(message: ImplementableApi.Message<T>): void;
|
||||
}
|
||||
|
||||
export { ImplementableApi };
|
||||
|
@ -1,11 +1,6 @@
|
||||
import { ImplementableApi } from './implementableApi';
|
||||
import {
|
||||
appendFileSync,
|
||||
closeSync,
|
||||
openSync,
|
||||
writeFile,
|
||||
writeFileSync,
|
||||
} from 'fs';
|
||||
|
||||
import pino, { Logger } from 'pino';
|
||||
|
||||
namespace LogWriter {
|
||||
export type Config = ImplementableApi.Config & {
|
||||
@ -17,33 +12,30 @@ namespace LogWriter {
|
||||
*/
|
||||
class LogWriter extends ImplementableApi {
|
||||
readonly path: string;
|
||||
readonly logger: Logger;
|
||||
|
||||
constructor(readonly config: LogWriter.Config) {
|
||||
super(config);
|
||||
this.path = config.path;
|
||||
|
||||
this.firstWrite();
|
||||
}
|
||||
|
||||
private firstWrite() {
|
||||
this.writeMsg('LogWriter is running');
|
||||
this.logger = pino(pino.destination({ dest: config.path }));
|
||||
}
|
||||
|
||||
private writeMsg(message: string): void;
|
||||
private writeMsg(message: ImplementableApi.Message): void;
|
||||
private writeMsg(message: ImplementableApi.Message | string) {
|
||||
if (typeof message !== 'string')
|
||||
message = `[${message.type} :: ${new Date().toISOString()}] ${
|
||||
message.rawContent
|
||||
message = `[${message.type}] ${
|
||||
typeof message.rawContent === 'string'
|
||||
? message.rawContent
|
||||
: JSON.stringify(message.rawContent)
|
||||
} ${
|
||||
message.idListener ??
|
||||
`( listener_id : ${message.idListener} )\n`
|
||||
}`;
|
||||
|
||||
const fd = openSync(this.path, 'a');
|
||||
const str = `[${new Date().toISOString()}] ${message}\n`;
|
||||
appendFileSync(fd, str);
|
||||
closeSync(fd);
|
||||
// const str = `[${new Date().toISOString()}] ${message}\n`;
|
||||
this.logger.info(message);
|
||||
}
|
||||
|
||||
public receivedMessage(message: ImplementableApi.Message) {
|
||||
|
@ -1,8 +1,5 @@
|
||||
import { ImplementableApi } from './implementableApi';
|
||||
|
||||
import { DiscordParser } from './discordParser';
|
||||
import { LogWriter } from './logWriter';
|
||||
import { PeerTubeRequester } from './peertubeRequester';
|
||||
import EventEmitter from 'events';
|
||||
|
||||
namespace Router {
|
||||
@ -12,44 +9,57 @@ namespace Router {
|
||||
};
|
||||
export type InternalConfig = {
|
||||
events: EventsType[];
|
||||
apis: {
|
||||
apiName: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
// export type GlobalConfig = {
|
||||
// router: InternalConfig;
|
||||
// };
|
||||
}
|
||||
|
||||
class Router extends EventEmitter {
|
||||
api_array: { [key: string]: ImplementableApi } = {};
|
||||
routes: Router.InternalConfig;
|
||||
class Router<
|
||||
MessageType extends ImplementableApi.TBaseMessageType
|
||||
> extends EventEmitter {
|
||||
api_array: { [key: string]: ImplementableApi<MessageType> } = {};
|
||||
events: Router.EventsType[];
|
||||
|
||||
constructor(readonly config: Router.EventsType[]) {
|
||||
super();
|
||||
this.routes = { events: config, apis: [] };
|
||||
this.events = config;
|
||||
}
|
||||
|
||||
public injectDependency(api: ImplementableApi): void {
|
||||
public injectDependency(api: ImplementableApi<MessageType>): void {
|
||||
if (api.name in this.api_array)
|
||||
throw `The api name '${api.name}' is already take`;
|
||||
this.routes.apis.push({ apiName: api.name });
|
||||
this.api_array[api.name] = api;
|
||||
|
||||
this.setEvents(api);
|
||||
|
||||
this.receivedMessage({
|
||||
rawContent: `The dependency \`${api.name}\` was well injected into the router`,
|
||||
type: 'logging',
|
||||
});
|
||||
}
|
||||
|
||||
private setEvents(api: ImplementableApi) {
|
||||
for (const eventName in this.routes.events.map(
|
||||
private setEvents(api: ImplementableApi<MessageType>) {
|
||||
for (const event of this.events.filter(
|
||||
(ev) => ev.type === 'received'
|
||||
))
|
||||
api.on(eventName, () => this.emit(eventName));
|
||||
)) {
|
||||
api.on(event.name, (obj: ImplementableApi.Message<MessageType>) => {
|
||||
this.receivedMessage({
|
||||
type: 'logging',
|
||||
rawContent: `A message of type \`${obj.type}\` was emited by \`${api.name}\` with the event \`${event.name}\``,
|
||||
});
|
||||
this.emit(event.name, obj);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public receivedMessage(message: ImplementableApi.Message) {
|
||||
for (const apiName in this.routes.apis)
|
||||
this.api_array[apiName].receivedMessage(message);
|
||||
public receivedMessage(message: ImplementableApi.Message<MessageType>) {
|
||||
this.redirectMessage({
|
||||
rawContent: `A message of type \`${message.type}\` was received`,
|
||||
type: 'logging',
|
||||
});
|
||||
this.redirectMessage(message);
|
||||
}
|
||||
private redirectMessage(message: ImplementableApi.Message<MessageType>) {
|
||||
for (const api in this.api_array)
|
||||
this.api_array[api].receivedMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user