-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cac16c1
commit 51e2759
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters