Skip to content

Commit

Permalink
feat: Added services for data/logs managing
Browse files Browse the repository at this point in the history
Refactored docker-compose to initialize kibana and pgweb for data managing
  • Loading branch information
atassis authored and sergeysova committed May 22, 2018
1 parent da42658 commit cea7163
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
23 changes: 18 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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


21 changes: 14 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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')
const { InvalidChatlistError, validateChatList, normalizeChatList } = require('./lib/chatlist-validate')
const { elasticPing } = require('./lib/elastic')
const features = require('./features')


let CHAT_LIST
/* eslint-disable unicorn/no-process-exit, no-console */

if (!config.bot.token) {
if (!environment.BOT_TOKEN) {
throw new Error('No telegram bot token provided')
}

Expand All @@ -38,21 +38,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 = []

Expand Down
7 changes: 4 additions & 3 deletions src/lib/runtime.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit cea7163

Please sign in to comment.