Skip to content

Commit

Permalink
Moved config to json
Browse files Browse the repository at this point in the history
  • Loading branch information
jzongker committed Nov 10, 2021
1 parent 85856ec commit a0b8fae
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 125 deletions.
50 changes: 26 additions & 24 deletions LambdaEntry.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
const { createServer, proxy } = require('aws-serverless-express');
const { init } = require('./dist/App');
const { Environment } = require('./dist/helpers/Environment');
const { Pool } = require('./dist/apiBase/pool');
const AWS = require('aws-sdk');
const { Logger } = require('./dist/helpers/Logger');
const { Repositories } = require('./dist/repositories/Repositories');
const { SocketHelper } = require('./dist/helpers/SocketHelper');
const { ApiGatewayManagementApi } = require('aws-sdk');

const gwManagement = new ApiGatewayManagementApi({ apiVersion: '2020-04-16', endpoint: process.env.SOCKET_URL });

Environment.init(process.env.APP_ENV);
Pool.initPool();

const gwManagement = new ApiGatewayManagementApi({ apiVersion: '2020-04-16', endpoint: Environment.socketUrl });


async function logMessage(message) {
const wl = new Logger();
wl.error(message);
await wl.flush();
const wl = new Logger();
wl.error(message);
await wl.flush();
}

module.exports.handleWeb = function handleWeb(event, context) {
AWS.config.update({ region: 'us-east-2' });
init().then(app => {
const server = createServer(app);
return proxy(server, event, context);
});
AWS.config.update({ region: 'us-east-2' });
init().then(app => {
const server = createServer(app);
return proxy(server, event, context);
});

}

module.exports.handleSocket = async function handleSocket(event) {
const rc = event.requestContext;
const eventType = rc.eventType;
const connectionId = rc.connectionId;
//console.log(eventType);
if (eventType == "DISCONNECT") await SocketHelper.handleDisconnect(connectionId) //; Repositories.getCurrent().connection.deleteForSocket(connectionId);
else if (eventType == "MESSAGE") {
const payload = { churchId: "", conversationId: "", action: "socketId", data: rc.connectionId }
//try {
await gwManagement.postToConnection({ ConnectionId: rc.connectionId, Data: JSON.stringify(payload) }).promise();
//} catch (e) { logMessage(e.toString()); }


}
return { statusCode: 200, body: 'success' }
const rc = event.requestContext;
const eventType = rc.eventType;
const connectionId = rc.connectionId;
//console.log(eventType);
if (eventType == "DISCONNECT") await SocketHelper.handleDisconnect(connectionId) //; Repositories.getCurrent().connection.deleteForSocket(connectionId);
else if (eventType == "MESSAGE") {
const payload = { churchId: "", conversationId: "", action: "socketId", data: rc.connectionId }
//try {
await gwManagement.postToConnection({ ConnectionId: rc.connectionId, Data: JSON.stringify(payload) }).promise();
//} catch (e) { logMessage(e.toString()); }


}
return { statusCode: 200, body: 'success' }
}
11 changes: 11 additions & 0 deletions config/dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"appEnv": "dev",
"appName": "MessagingApi",
"contentRoot": "http://localhost:3402",
"fileStore": "disk",
"mailSystem": "SMTP",
"s3Bucket": "",
"deliveryProvider": "local",
"socketPort": 8087,
"socketUrl": ""
}
11 changes: 11 additions & 0 deletions config/prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"appEnv": "prod",
"appName": "MessagingApi",
"contentRoot": "https://content.churchapps.org",
"fileStore": "S3",
"mailSystem": "SES",
"s3Bucket": "churchapps-content",
"deliveryProvider": "aws",
"socketPort": 0,
"socketUrl": "wss://socket.churchapps.org/"
}
11 changes: 11 additions & 0 deletions config/staging.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"appEnv": "staging",
"appName": "MessagingApi",
"contentRoot": "https://content.staging.churchapps.org",
"fileStore": "S3",
"mailSystem": "SES",
"s3Bucket": "staging-churchapps-content",
"deliveryProvider": "aws",
"socketPort": 0,
"socketUrl": "wss://socket.staging.churchapps.org/"
}
25 changes: 13 additions & 12 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ import express from "express";
import { CustomAuthProvider } from "./apiBase/auth";
import cors from "cors"
import { SocketHelper } from "./helpers/SocketHelper";
import { Environment } from "./helpers";


export const init = async () => {
dotenv.config();
const container = new Container();
await container.loadAsync(bindings);
const app = new InversifyExpressServer(container, null, null, null, CustomAuthProvider);
dotenv.config();
const container = new Container();
await container.loadAsync(bindings);
const app = new InversifyExpressServer(container, null, null, null, CustomAuthProvider);

const configFunction = (expApp: express.Application) => {
expApp.use(bodyParser.urlencoded({ extended: true }));
expApp.use(bodyParser.json());
expApp.use(cors())
};
const configFunction = (expApp: express.Application) => {
expApp.use(bodyParser.urlencoded({ extended: true }));
expApp.use(bodyParser.json());
expApp.use(cors())
};

const server = app.setConfig(configFunction).build();
const server = app.setConfig(configFunction).build();

if (process.env.DELIVERY_PROVIDER === "local") SocketHelper.init();
if (Environment.deliveryProvider === "local") SocketHelper.init();

return server;
return server;
}
2 changes: 1 addition & 1 deletion src/apiBase
31 changes: 31 additions & 0 deletions src/helpers/Environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from "fs";
import path from "path";

import { EnvironmentBase } from "../apiBase";

export class Environment extends EnvironmentBase {

static deliveryProvider: string;
static socketPort: number;
static socketUrl: string;

static init(environment: string) {
let file = "dev.json";
if (environment === "staging") file = "staging.json";
if (environment === "prod") file = "prod.json";


const relativePath = "../../config/" + file;
const physicalPath = path.resolve(__dirname, relativePath);

const json = fs.readFileSync(physicalPath, "utf8");
const data = JSON.parse(json);
this.populateBase(data);

this.deliveryProvider = data.deliveryProvider;
this.socketPort = data.socketPort;
this.socketUrl = data.socketUrl;

}

}
88 changes: 44 additions & 44 deletions src/helpers/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@ import "reflect-metadata";
import winston from "winston";
import WinstonCloudWatch from "winston-cloudwatch";
import AWS from 'aws-sdk';

import { Environment } from "./Environment";

export class Logger {
private _logger: winston.Logger = null;
private wc: WinstonCloudWatch;
private pendingMessages = false;

public error(msg: string | object) {
if (this._logger === null) this.init();
this.pendingMessages = true;
this._logger.error(msg);
private _logger: winston.Logger = null;
private wc: WinstonCloudWatch;
private pendingMessages = false;

public error(msg: string | object) {
if (this._logger === null) this.init();
this.pendingMessages = true;
this._logger.error(msg);
}

public info(msg: string | object) {
if (this._logger === null) this.init();
this.pendingMessages = true;
this._logger.info(msg);
}


private init() {
this.pendingMessages = false;
AWS.config.update({ region: 'us-east-2' });
if (Environment.appEnv === "dev") {
this._logger = winston.createLogger({ transports: [new winston.transports.Console()], format: winston.format.json() });
// this.wc = new WinstonCloudWatch({ logGroupName: 'StreamingLiveDev', logStreamName: 'ChatApi' });
// this._logger = winston.createLogger({ transports: [this.wc], format: winston.format.json() });
}

public info(msg: string | object) {
if (this._logger === null) this.init();
this.pendingMessages = true;
this._logger.info(msg);
else if (Environment.appEnv === "staging") {
this.wc = new WinstonCloudWatch({ logGroupName: 'CoreApis', logStreamName: 'MessagingApi' });
this._logger = winston.createLogger({ transports: [this.wc], format: winston.format.json() });
}


private init() {
this.pendingMessages = false;
AWS.config.update({ region: 'us-east-2' });
if (process.env.API_ENV === "dev") {
this._logger = winston.createLogger({ transports: [new winston.transports.Console()], format: winston.format.json() });
// this.wc = new WinstonCloudWatch({ logGroupName: 'StreamingLiveDev', logStreamName: 'ChatApi' });
// this._logger = winston.createLogger({ transports: [this.wc], format: winston.format.json() });
}
else if (process.env.API_ENV === "staging") {
this.wc = new WinstonCloudWatch({ logGroupName: 'CoreApis', logStreamName: 'MessagingApi' });
this._logger = winston.createLogger({ transports: [this.wc], format: winston.format.json() });
}
else if (process.env.API_ENV === "prod") {
this.wc = new WinstonCloudWatch({ logGroupName: 'CoreApis', logStreamName: 'MessagingApi' });
this._logger = winston.createLogger({ transports: [this.wc], format: winston.format.json() });
}
this._logger.info("Logger initialized");
else if (Environment.appEnv === "prod") {
this.wc = new WinstonCloudWatch({ logGroupName: 'CoreApis', logStreamName: 'MessagingApi' });
this._logger = winston.createLogger({ transports: [this.wc], format: winston.format.json() });
}

public flush() {
const promise = new Promise<void>((resolve) => {
if (this.pendingMessages) {
this.wc.kthxbye(() => {
this._logger = null;
resolve();
});
} else resolve();
this._logger.info("Logger initialized");
}

public flush() {
const promise = new Promise<void>((resolve) => {
if (this.pendingMessages) {
this.wc.kthxbye(() => {
this._logger = null;
resolve();
});
return promise;
}
} else resolve();
});
return promise;
}


}
85 changes: 43 additions & 42 deletions src/helpers/SocketHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,54 @@ import { PayloadInterface, SocketConnectionInterface } from "./Interfaces";
import { Repositories } from "../repositories"
import { Connection } from "../models"
import { DeliveryHelper } from "./DeliveryHelper";
import { Environment } from ".";

export class SocketHelper {

private static wss: WebSocket.Server = null;
private static connections: SocketConnectionInterface[] = [];

static init = () => {
const port = parseInt(process.env.SOCKET_PORT, 0);
if (port > 0) {
SocketHelper.wss = new WebSocket.Server({ port });
console.log("Listening on websocket port " + port);

SocketHelper.wss.on('connection', (socket) => {
const sc: SocketConnectionInterface = { id: UniqueIdHelper.shortId(), socket }
SocketHelper.connections.push(sc);
const payload: PayloadInterface = { churchId: "", conversationId: "", action: "socketId", data: sc.id }
sc.socket.send(JSON.stringify(payload));
sc.socket.on('close', async () => {
// console.log("DELETING " + sc.id);
await SocketHelper.handleDisconnect(sc.id);
});
});
}
}

static handleDisconnect = async (socketId: string) => {
// console.log("handleDisconnect");
// console.log(socketId);
const connections = await Repositories.getCurrent().connection.loadBySocketId(socketId);
await Repositories.getCurrent().connection.deleteForSocket(socketId);
connections.forEach((c: Connection) => {
DeliveryHelper.sendAttendance(c.churchId, c.conversationId);
private static wss: WebSocket.Server = null;
private static connections: SocketConnectionInterface[] = [];

static init = () => {
const port = Environment.socketPort;
if (port > 0) {
SocketHelper.wss = new WebSocket.Server({ port });
console.log("Listening on websocket port " + port);

SocketHelper.wss.on('connection', (socket) => {
const sc: SocketConnectionInterface = { id: UniqueIdHelper.shortId(), socket }
SocketHelper.connections.push(sc);
const payload: PayloadInterface = { churchId: "", conversationId: "", action: "socketId", data: sc.id }
sc.socket.send(JSON.stringify(payload));
sc.socket.on('close', async () => {
// console.log("DELETING " + sc.id);
await SocketHelper.handleDisconnect(sc.id);
});

});
}

static getConnection = (id: string) => {
let result: SocketConnectionInterface = null;
SocketHelper.connections.forEach(sc => { if (sc.id === id) result = sc; });
return result;
}

static deleteConnection = (id: string) => {
for (let i = SocketHelper.connections.length - 1; i >= 0; i--) {
const sc = SocketHelper.connections[i];
if (sc.id === id) SocketHelper.connections.splice(i, 1);
}
}

static handleDisconnect = async (socketId: string) => {
// console.log("handleDisconnect");
// console.log(socketId);
const connections = await Repositories.getCurrent().connection.loadBySocketId(socketId);
await Repositories.getCurrent().connection.deleteForSocket(socketId);
connections.forEach((c: Connection) => {
DeliveryHelper.sendAttendance(c.churchId, c.conversationId);
});

}

static getConnection = (id: string) => {
let result: SocketConnectionInterface = null;
SocketHelper.connections.forEach(sc => { if (sc.id === id) result = sc; });
return result;
}

static deleteConnection = (id: string) => {
for (let i = SocketHelper.connections.length - 1; i >= 0; i--) {
const sc = SocketHelper.connections[i];
if (sc.id === id) SocketHelper.connections.splice(i, 1);
}
}

}
3 changes: 2 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Permissions'
export * from '../apiBase/helpers/Interfaces'

export { UniqueIdHelper } from "../apiBase/helpers";
export { UniqueIdHelper } from "../apiBase/helpers";
export { Environment } from "./Environment";
Loading

0 comments on commit a0b8fae

Please sign in to comment.