87 lines
No EOL
3 KiB
TypeScript
87 lines
No EOL
3 KiB
TypeScript
import {Environment} from "../Environment";
|
|
import {Container} from "./Container";
|
|
import {DatabaseConnection} from "../Database/DatabaseConnection";
|
|
import {configure, getLogger, Logger} from "log4js";
|
|
import path from "node:path";
|
|
import {GroupRepository} from "../Repositories/GroupRepository";
|
|
import {PlaydateRepository} from "../Repositories/PlaydateRepository";
|
|
import {GroupConfigurationRepository} from "../Repositories/GroupConfigurationRepository";
|
|
import {DiscordClient} from "../Discord/DiscordClient";
|
|
import {IconCache} from "../Icons/IconCache";
|
|
import {EventHandler} from "../Events/EventHandler";
|
|
import {InteractionRouter} from "../Discord/InteractionRouter";
|
|
import Commands from "../Discord/Commands/Commands";
|
|
import {CommandDeployer} from "../Discord/CommandDeployer";
|
|
import {REST} from "discord.js";
|
|
import {log} from "node:util";
|
|
|
|
export enum ServiceHint {
|
|
App,
|
|
Deploy
|
|
}
|
|
|
|
export class Services {
|
|
public static setup(container: Container, hint: ServiceHint) {
|
|
const env = new Environment();
|
|
env.setup();
|
|
container.set<Environment>(env);
|
|
|
|
const logger = this.setupLogger(hint);
|
|
container.set<Logger>(logger, 'logger');
|
|
|
|
const database = new DatabaseConnection(env.database);
|
|
container.set<DatabaseConnection>(database);
|
|
|
|
const restClient = new REST();
|
|
const commands = new Commands();
|
|
const discordClient = new DiscordClient(
|
|
env.discord.clientId,
|
|
new InteractionRouter(commands, logger),
|
|
new CommandDeployer(env.discord.clientId, restClient, commands, logger),
|
|
logger,
|
|
restClient
|
|
);
|
|
container.set<DiscordClient>(discordClient);
|
|
|
|
const iconCache = new IconCache(discordClient);
|
|
container.set<IconCache>(iconCache);
|
|
|
|
container.set<EventHandler>(new EventHandler());
|
|
this.setupRepositories(container);
|
|
}
|
|
|
|
private static setupRepositories(container: Container) {
|
|
const db = container.get<DatabaseConnection>(DatabaseConnection.name);
|
|
container.set<GroupRepository>(new GroupRepository(db));
|
|
container.set<PlaydateRepository>(new PlaydateRepository(db, container.get<GroupRepository>(GroupRepository.name)))
|
|
container.set<GroupConfigurationRepository>(new GroupConfigurationRepository(db, container.get<GroupRepository>(GroupRepository.name)))
|
|
}
|
|
|
|
private static setupLogger(hint: ServiceHint): Logger {
|
|
|
|
configure({
|
|
appenders: {
|
|
out: {type: "stdout"},
|
|
appLogFile: {type: "file", filename: path.resolve("logs/run.log")},
|
|
deployLogFile: {type: "file", filename: path.resolve("logs/deploy.log")},
|
|
},
|
|
categories: {
|
|
default: {appenders: ['out'], level: 'debug'},
|
|
app: {appenders: ["out", "appLogFile"], level: "debug"},
|
|
deploy: {appenders: ["out", "deployLogFile"], level: "debug"},
|
|
}
|
|
})
|
|
|
|
let loggername = '';
|
|
switch (hint) {
|
|
case ServiceHint.App:
|
|
loggername = "app";
|
|
break;
|
|
case ServiceHint.Deploy:
|
|
loggername = "deploy";
|
|
break;
|
|
|
|
}
|
|
return getLogger(loggername);
|
|
}
|
|
} |