Skip to content

Commit

Permalink
Config and database connector
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen5209 committed Jul 29, 2021
1 parent cac16c1 commit 51e2759
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"telegraf": "^4.4.1"
},
"devDependencies": {
"@types/node": "^16.4.6",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"eslint": "^7.31.0",
Expand Down
54 changes: 54 additions & 0 deletions src/Core/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { Category } from 'logging-ts';
import { Core } from '..';

export class Config {
public telegram: { botToken: string, admins: number[], baseApiUrl: string | undefined };
public mongodb: { host: string, name: string };
public debug: boolean;
private logger: Category;

constructor(core: Core) {
this.logger = new Category('Config', core.mainLogger);
this.logger.info('Loading Config...');

const telegramDefault = { botToken: '', admins: [], baseApiUrl: undefined };
const mongodbDefault = { host: '', name: '' };

if (existsSync('./config.json')) {
const config = JSON.parse(readFileSync('config.json', { encoding: 'utf-8' }));

// take and merge config
this.telegram = {
botToken: (config.telegram.botToken) ? config.telegram.botToken : telegramDefault.botToken,
admins: (config.telegram.admins) ? config.telegram.admins : telegramDefault.admins,
baseApiUrl: (config.telegram.baseApiUrl) ? config.telegram.baseApiUrl : telegramDefault.baseApiUrl
};
this.mongodb = {
host: (config.mongodb.host) ? config.mongodb.host : mongodbDefault.host,
name: (config.mongodb.name) ? config.mongodb.name : mongodbDefault.name
};
this.debug = (config.Debug) ? config.Debug : false;

this.write();
} else {
this.logger.error('Can\'t load config.json: File not found.', null);
this.logger.info('Generating empty config...');
this.telegram = telegramDefault;
this.mongodb = mongodbDefault;
this.debug = false;
this.write();
this.logger.info('Fill your config and try again.');
process.exit(-1);
}
}

private write() {
const json = JSON.stringify({
telegram: this.telegram,
mongodb: this.mongodb,
Debug: this.debug
}, null, 4);
writeFileSync('./config.json', json, 'utf8');
}
}
32 changes: 32 additions & 0 deletions src/Core/MongoDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { EventEmitter } from 'events';
import { Category } from 'logging-ts';
import { Db, MongoClient } from 'mongodb';
import { Core } from '..';

export const ERR_DB_NOT_INIT = Error('Database is not initialized');

export declare interface MongoDB {
on(event: 'connect', listen: (database: Db) => void): this;
}

export class MongoDB extends EventEmitter {
public client?: Db;
private logger: Category;

constructor(core: Core) {
super();

this.logger = new Category('MongoDB', core.mainLogger);
this.logger.info('Loading MongoDB...');

const config = core.config.mongodb;

MongoClient.connect(config.host).then(client => {
this.logger.info('Successfully connected to mongodb');

this.client = client.db(config.name);

this.emit('connect', this.client);
});
}
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { catService } from 'logging-ts';
import { Config } from './Core/Config';
import { MongoDB } from './Core/MongoDB';
export class Core {
public readonly mainLogger = catService;
public readonly config = new Config(this);
public readonly mongodb = new MongoDB(this);

constructor() {
// eslint-disable-next-line no-unused-expressions,@typescript-eslint/no-unused-expressions
// new Telegram();
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.8.tgz#edf1bf1dbf4e04413ca8e5b17b3b7d7d54b59818"
integrity sha512-YSBPTLTVm2e2OoQIDYx8HaeWJ5tTToLH67kXR7zYNGupXMEHa2++G8k+DczX2cFVgalypqtyZIcU19AFcmOpmg==

"@types/node@^16.4.6":
version "16.4.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.6.tgz#1734d119dfa8fede5606d35ae10f9fc9c84d889b"
integrity sha512-FKyawK3o5KL16AwbeFajen8G4K3mmqUrQsehn5wNKs8IzlKHE8TfnSmILXVMVziAEcnB23u1RCFU1NT6hSyr7Q==

"@typescript-eslint/eslint-plugin@^4.28.5":
version "4.28.5"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.28.5.tgz#8197f1473e7da8218c6a37ff308d695707835684"
Expand Down

0 comments on commit 51e2759

Please sign in to comment.