Skip to content

Commit

Permalink
feat: added support to read config from json
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypa committed Sep 2, 2022
1 parent f5da960 commit 27d7860
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
Empty file added .app.json
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lint:fix": "eslint --quiet --fix src test",
"prepare": "husky install",
"test": "npm run test:unit && npm run test:integ",
"test:unit": "mocha test/unit/*.spec.js test/unit/**/*.spec.js test/unit/**/**/*.spec.js --timeout=3000",
"test:unit": "mocha test/unit/*.spec.js test/unit/**/*.spec.js test/unit/**/*.spec.js --timeout=3000",
"test:integ": "mocha test/integration/*.spec.js test/integration/**/*.spec.js --timeout=3000",
"printReportsLink": "echo Detailed unit test coverage report: file:///$(pwd)/coverage-unit/index.html && echo Detailed integration test coverage report: file:///$(pwd)/coverage-integration/index.html",
"cover": "npm run cover:unit",
Expand Down
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ server.post('/put', getPutSchema(), async function (request, reply) {
async function initMysql() {
try {

if (!LibMySql.init(configs.mySqlConfigs)) {
if (!LibMySql.init(configs.mysql)) {
throw new Error('Exception occurred while connecting to DB');
}
} catch (e) {
Expand Down
32 changes: 17 additions & 15 deletions src/utils/configs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as crypto from "crypto";
import {getMySqlConfigs} from "@aicore/libcommonutils";
import * as fs from "fs";

let APP_CONFIG = null;

/**
* It returns an object with the port, authKey, and mySqlConfigs
Expand All @@ -23,20 +25,20 @@ import {getMySqlConfigs} from "@aicore/libcommonutils";
* password : The value of the environment variable MY_SQL_PASSWORD or a random hex string
*/
export function getConfigs() {
const port = process.env.COCO_DB_PORT || '5000';
const authKey = process.env.COCO_DB_AUTH_KEY || crypto.randomBytes(4).toString('hex');
//const mySqlConfigs = getMySqlConfigs();
const mySqlConfigs = {
'host': 'localhost',
'port': '3306',
'database': 'testdb',
'user': 'root',
'password': '1234'
if (APP_CONFIG) {
return APP_CONFIG;
}
if (!process.env.APP_CONFIG) {
throw new Error('Please provide valid app config file');
}
APP_CONFIG = _getAppConfig(process.env.APP_CONFIG);
APP_CONFIG.port = APP_CONFIG.port || '5000';
return APP_CONFIG;
}

};
return {
port: port,
authKey: authKey,
mySqlConfigs: mySqlConfigs
};
function _getAppConfig(file) {
const appConfigFile = fs.readFileSync(file);
const appConfig = JSON.parse(appConfigFile.toString());
return appConfig;
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*global describe, it*/
import mockedFunctions from '../setup-mocks.js';
import mockedFunctions from '../utils/setup-mocks.js';
import LibMySql from "@aicore/libmysql";
import * as chai from 'chai';
import {createTable, getCreatTableSchema} from "../../../../src/api/createTable.js";
import {createTable, getCreatTableSchema} from "../../../src/api/createTable.js";

let expect = chai.expect;
describe('unit test for createTable tests', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*global describe, it*/
import mockedFunctions from '../setup-mocks.js';
import mockedFunctions from '../utils/setup-mocks.js';
import LibMySql from "@aicore/libmysql";
import * as chai from 'chai';
import {putDocument, getPutSchema} from "../../../../src/api/put.js";
import {hello} from "../../../../src/api/hello.js";
import {putDocument, getPutSchema} from "../../../src/api/put.js";
import {hello} from "../../../src/api/hello.js";

let expect = chai.expect;
describe('unit test for put api', function () {
Expand Down
Empty file.

0 comments on commit 27d7860

Please sign in to comment.