From 3f06558e11a34620efe757c28c7e2c2580cbac31 Mon Sep 17 00:00:00 2001 From: atassis Date: Tue, 22 May 2018 19:47:40 +0300 Subject: [PATCH] feat: Added services for data/logs managing Refactored docker-compose to initialize kibana and pgweb for data managing --- .env.example | 23 ++++++++++++++++++----- docker-compose.yml | 21 ++++++++++++++------- src/config/index.js | 9 +++++++-- src/index.js | 16 ++++++---------- src/lib/runtime.js | 7 ++++--- 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/.env.example b/.env.example index b8034cb..ce36519 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,20 @@ -BOT_TOKEN=123456789:asd9uahsdfuybs8dfynsudygfs78ydfguds -PRIVATE_CHANNEL_ID=-1001001112233 -NODE_ENV=development -ELASTIC_LOG=false -DB_URL=postgres://rubot:rubot@db/rubot +## +# Connection variables +DATABASE_URL=postgres://rubot:rubot@db/rubot +ELASTICSEARCH_URL=http://elasticsearch:9200 + +## +# Authentication variables POSTGRES_USER=rubot POSTGRES_PASSWORD=rubot + +## +# Application base variables +NODE_ENV=development + +## +# Application bot variables +BOT_TOKEN=123456789:asd9uahsdfuybs8dfynsudygfs78ydfguds +PRIVATE_CHANNEL_ID=-1001001112233 + + diff --git a/docker-compose.yml b/docker-compose.yml index 6d2b782..aed7bed 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,22 +2,29 @@ version: '3' services: app: build: . - restart: always + restart: on-failure env_file: - ./.env - db: + postgres: image: postgres:9-alpine restart: always volumes: - ./data/pgsql:/var/lib/postgresql - env_file: - - ./.env ports: - 5432:5432 - es: + admin: + image: sosedoff/pgweb + restart: always + ports: + - 10000:8081 + elasticsearch: image: elastic/elasticsearch:6.2.4 restart: always volumes: - ./data/elastic - env_file: - - ./.env + kibana: + image: docker.elastic.co/kibana/kibana:6.2.4 + ports: + - 10001:5601 + links: + - elasticsearch diff --git a/src/config/index.js b/src/config/index.js index f30bc30..0eaa355 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -13,8 +13,8 @@ const { let db -if (environment.DB_URL) { - const { auth, pathname, protocol, host } = url.parse(environment.DB_URL) +if (environment.DATABASE_URL) { + const { auth, pathname, protocol, host } = url.parse(environment.DATABASE_URL) const [username, password] = auth.split(':') const database = pathname.slice(1) const dialect = protocol.slice(0, -1) @@ -30,6 +30,11 @@ if (environment.DB_URL) { else { db = dbConfig[NODE_ENV] } + +if (!BOT_TOKEN) { + throw new Error('No telegram bot token provided') +} + module.exports = { db, environment, diff --git a/src/index.js b/src/index.js index 26f04a0..6e4f94d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ +console.log(process.env) const debug = require('debug')('rubot:index') -const config = require('./config') +const { bot: botConfig, environment } = require('./config') const { sequelize } = require('./models') const { createBot } = require('./lib/runtime') const { Channel } = require('./lib/channel') @@ -8,14 +9,9 @@ const { InvalidChatlistError, validateChatList, normalizeChatList } = require('. const { elasticPing } = require('./lib/elastic') const features = require('./features') - let CHAT_LIST /* eslint-disable unicorn/no-process-exit, no-console */ -if (!config.bot.token) { - throw new Error('No telegram bot token provided') -} - try { const chatlistConfig = require('./.chatlist.json') // eslint-disable-line global-require @@ -38,21 +34,21 @@ catch (error) { } const bot = createBot( - config.bot.token, + botConfig.token, features, - { username: config.bot.username } + { username: botConfig.username } ) async function main() { debug('main()') await sequelize.authenticate() - if (process.env.ELASTIC_LOG) { + if (environment.ELASTICSEARCH_URL) { await elasticPing() } bot.context.botInfo = await bot.telegram.getMe() - bot.context.privateChannel = new Channel(config.bot.privateChannelId, bot) + bot.context.privateChannel = new Channel(botConfig.privateChannelId, bot) // TODO: hardcoded chatlist bot.context.ownedChats = [] diff --git a/src/lib/runtime.js b/src/lib/runtime.js index 18f2993..ad38a53 100644 --- a/src/lib/runtime.js +++ b/src/lib/runtime.js @@ -1,6 +1,7 @@ const debug = require('debug')('rubot:lib:runtime') const Telegraf = require('telegraf') +const { environment } = require('../config') const extendedContext = require('./extended-context') const { push } = require('./elastic') @@ -28,15 +29,15 @@ function createBot(token, features, telegrafConfig = {}) { debug('createBot()', telegrafConfig) const instance = new Telegraf(token, telegrafConfig) - if (process.env.NODE_ENV === 'development') { + if (environment.NODE_ENV === 'development') { instance.use(Telegraf.log()) } - if (process.env.ELASTIC_LOG) { + if (environment.ELASTICSEARCH_URL) { instance.use((ctx, next) => { if (ctx.update.message) { push({ - index: `rubot-${process.env.NODE_ENV || 'undefined'}`, + index: `rubot-${environment.NODE_ENV || 'undefined'}`, type: 'message', id: `M${ctx.update.message.message_id}C${ctx.update.message.chat.id}F${ctx.update.message.from.id}`, body: Object.assign({