Skip to content
This repository has been archived by the owner on Jan 13, 2020. It is now read-only.

Commit

Permalink
Feature pubsub google (#75)
Browse files Browse the repository at this point in the history
erxes/erxes-widgets-api#449
  • Loading branch information
munkhorgil authored and Battulga BatAmar committed Jun 7, 2019
1 parent 1c4eb0f commit bbcaaf8
Show file tree
Hide file tree
Showing 5 changed files with 745 additions and 27 deletions.
8 changes: 7 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ TEST_MONGO_URL=mongodb://localhost/erxesApiTest
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_PASSWORD=

# Pubsub REDIS | GOOGLE
PUBSUB_TYPE='REDIS'

# Google Pubsub config file
GOOGLE_APPLICATION_CREDENTIALS=''
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
google_cred.json
settings.js
server/settings.js
dist
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@
]
},
"dependencies": {
"@axelspringer/graphql-google-pubsub": "^1.2.1",
"@google-cloud/pubsub": "^0.29.1",
"apollo-server-express": "^2.3.1",
"body-parser": "^1.17.1",
"cors": "^2.8.1",
"dotenv": "^4.0.0",
"express": "^4.15.2",
"graphql": "^14.0.2",
"graphql-tools": "^4.0.3",
"ioredis": "^4.9.0",
"ioredis": "^4.9.5",
"meteor-random": "0.0.3",
"mongoose": "^5.2.16",
"nodemailer": "^4.0.1",
Expand Down
73 changes: 64 additions & 9 deletions src/pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import { GooglePubSub } from '@axelspringer/graphql-google-pubsub';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as Redis from 'ioredis';
import * as path from 'path';
import { IConversationDocument, ICustomerDocument, IMessageDocument } from './db/models';
import { ICompanyDocument } from './db/models/definitions/companies';

// load environment variables
dotenv.config();

interface IPubSub {
publish(trigger: string, payload: any, options?: any): any;
}

interface IGoogleOptions {
projectId: string;
credentials: {
client_email: string;
private_key: string;
};
}

interface IPubsubData {
type?: string;
trigger?: string;
payload: IMessageDocument | IConversationDocument | ICustomerDocument | ICompanyDocument;
}

const {
PUBSUB_TYPE,
REDIS_HOST = 'localhost',
REDIS_PORT = 6379,
REDIS_PASSWORD = '',
}: {
PUBSUB_TYPE?: string;
REDIS_HOST?: string;
REDIS_PORT?: number;
REDIS_PASSWORD?: string;
Expand All @@ -29,20 +54,50 @@ const redisOptions = {
},
};

const broker = new Redis(redisOptions);
const configGooglePubsub = (): IGoogleOptions => {
const checkHasConfigFile = fs.existsSync(path.join(__dirname, '..', '/google_cred.json'));

if (!checkHasConfigFile) {
throw new Error('Google credentials file not found!');
}

const serviceAccount = require('../google_cred.json');

return {
projectId: serviceAccount.project_id,
credentials: {
client_email: serviceAccount.client_email,
private_key: serviceAccount.private_key,
},
};
};

const createBrokerInstance = (): IPubSub => {
let pubsub;

if (PUBSUB_TYPE === 'GOOGLE') {
const googleOptions = configGooglePubsub();

const googlePubsub = new GooglePubSub(googleOptions);

pubsub = googlePubsub;
} else {
const redisPubsub = new Redis(redisOptions);

pubsub = redisPubsub;
}

return pubsub;
};

const broker = createBrokerInstance();

export const publish = (action: string, data) => {
export const publish = (action: string, data: IPubsubData) => {
const { NODE_ENV } = process.env;

if (NODE_ENV !== 'production') {
return;
}

return broker.publish(
'widgetNotification',
JSON.stringify({
action,
data,
}),
);
return broker.publish('widgetNotification', JSON.stringify({ action, data }));
};
Loading

0 comments on commit bbcaaf8

Please sign in to comment.