Skip to content

Commit

Permalink
fix: db connection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed May 18, 2021
1 parent 9af97a8 commit 4954fcc
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 16 deletions.
108 changes: 108 additions & 0 deletions src/helpers/index.backup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var express = require('express');
var router = express.Router();
var path = require('path');
var utils = require("../helpers/utils.js");
var fs = require('fs');
const mime = require('mime-types')
const render = require('./render');

/**
* <write a short decription>
*
* 1. with hostname we query a master db in order to find the organization_id...
* with the org id we can now query for the uri in collection "files" and return value for fieldName "src"
* 2. in db we store path as /docs/dnd but need something more flexible so we can respond even if request /docs/dnd/
*
* */
const masterDB = '5ae0cfac6fb8c4e656fdaf92'
router.get('/*', async(req, res, next) => {



let hostname = req.hostname;
let organization = await utils.organizationsfindOne({ domains: hostname }, masterDB)
if (!organization)
return res.send('Organization cannot be found using the domain:' + hostname);


let url = req.url;
if (url.endsWith('/'))
url = url.substring(0, url.length - 1);

url = url.startsWith('/ws') ? url.substr(3) : url; // dev

// console.log('>>>>>>>>>>>>>', url, organization)
let organization_id = organization._id.toString();


let route_files = await utils.routesfindOne({ hostname: hostname, route_uri: url }, organization_id);


if (!route_files)
return res.status(404).send(`there is no ${url} in masterDb ${organization_id} `);

// if (!route_files['public'] || route_files['public'] === "false")
// return res.status(404).send(`access not allowed`);





let data;
if (route_files['src'])
data = route_files['src'];
else {
let route_export = await utils.getDocument({
collection: route_files['collection'],
document_id: route_files['document_id']
}, organization_id);
data = route_export[route_files['name']];

}

if (!data) {
res.send('Document provided by routes could not be found and has no src ');
}



let content_type = route_files['content_type'] ||
mime.lookup(url) ||
'text/html';


// console.log('(>>>>>>>>>>>>>>', content_type, route_files['content_type'], route_files['route'])

if (content_type.startsWith('image/') || content_type.startsWith('audio/') || content_type.startsWith('video/')) {
// let content_type = route['content_type'] || "image/png";
// todo: is there are better alternative or conevention not to process base64
let file = Buffer.from(data, 'base64');
res.set('Content-Type', content_type);
res.send(file);
}
else if (content_type === 'text/html') {


try {

let fullHtml = await render(data, organization_id);
res.type(content_type);
res.send(fullHtml);
}
catch (err) {
if (err.message.startsWith('infinite loop:'))
return res.send('there is a infinite loop');
else
return res.send('something is wrong')
}

}
else {
res.type(content_type);
res.send(data);
}


});

module.exports = router;
67 changes: 67 additions & 0 deletions src/helpers/render.backup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
let { parse } = require("node-html-parser");
let { getDocument } = require('../helpers/utils');

module.exports = async function renderHtml(html, organization_id) {

let dep = [];
let dbCache = new Map();

async function render(html, lastKey) {
const dom = parse(html);
for (let el of dom.querySelectorAll(
"[data-collection][name][data-document_id]"
)) {
let meta = el.attributes;
let id = meta["data-document_id"],
coll = meta['data-collection'],
name = meta['name'];
let key = id + coll + name;
if(!id || !name || !coll) continue;
if (dep.includes(key))
throw new Error(
`infinite loop: ${lastKey} ${id} ${coll} ${name} has been already rendered`
);
else
dep.push(key)

let cacheKey = id + coll;
let record;
if (dbCache.has(cacheKey))
record = dbCache.get(cacheKey)
else {

record = await
getDocument({
collection: coll,
document_id: id
}, organization_id);
dbCache.set(cacheKey, record)
}



if(!record || !record[name])
{
dep.pop();
continue;
}
let chunk = record[name];
if (!chunk) {

dep.pop();
continue;
}
let dom = await render(chunk);

el.innerHTML = "";
el.appendChild(dom);


dep.pop();
}

return dom;
}
let result = (await render(html, 'root')).toString();
return result;
}
2 changes: 1 addition & 1 deletion src/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const connection = require('./dbConnection.js');
var ObjectID = require('mongodb').ObjectID;
module.exports = {
getDocument: async (data,dbName) => {
getDocument: async (data, dbName) => {

try {
const db = await connection(dbName); // obtenemos la conexión
Expand Down
36 changes: 24 additions & 12 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
var express = require('express');
var router = express.Router();
var path = require('path');
var utils = require("../helpers/utils.js");
var utils = require("./utils.js");
var fs = require('fs');
const mime = require('mime-types')
const render = require('./render');

const { MongoClient } = require('mongodb');
var config = require('../../config.json');

let dbClient = null;

MongoClient.connect(config.db_url, { useNewUrlParser: true, poolSize: 10 })
.then((client, err) => {
dbClient = client;
});

/**
* <write a short decription>
*
Expand All @@ -17,10 +27,8 @@ const render = require('./render');
const masterDB = '5ae0cfac6fb8c4e656fdaf92'
router.get('/*', async(req, res, next) => {



let hostname = req.hostname;
let organization = await utils.organizationsfindOne({ domains: hostname }, masterDB)
let organization = await utils.organizationsfindOne(dbClient, { domains: hostname }, masterDB)
if (!organization)
return res.send('Organization cannot be found using the domain:' + hostname);

Expand All @@ -35,14 +43,14 @@ router.get('/*', async(req, res, next) => {
let organization_id = organization._id.toString();


let route_files = await utils.routesfindOne({ hostname: hostname, route_uri: url }, organization_id);
let route_files = await utils.routesfindOne(dbClient,{ hostname: hostname, route_uri: url }, organization_id);


if (!route_files)
return res.status(404).send(`there is no ${url} in masterDb ${organization_id} `);

if (!route_files['public'] || route_files['public'] === "false")
return res.status(404).send(`access not allowed`);
// if (!route_files['public'] || route_files['public'] === "false")
// return res.status(404).send(`access not allowed`);



Expand All @@ -52,10 +60,14 @@ router.get('/*', async(req, res, next) => {
if (route_files['src'])
data = route_files['src'];
else {
let route_export = await utils.getDocument({
collection: route_files['collection'],
document_id: route_files['document_id']
}, organization_id);
let route_export = await utils.getDocument(
dbClient,
{
collection: route_files['collection'],
document_id: route_files['document_id']
},
organization_id
);
data = route_export[route_files['name']];

}
Expand Down Expand Up @@ -85,7 +97,7 @@ router.get('/*', async(req, res, next) => {

try {

let fullHtml = await render(data, organization_id);
let fullHtml = await render(db_client, data, organization_id);
res.type(content_type);
res.send(fullHtml);
}
Expand Down
6 changes: 3 additions & 3 deletions src/routes/render.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let { parse } = require("node-html-parser");
let { getDocument } = require('../helpers/utils');
let { getDocument } = require('./utils');

module.exports = async function renderHtml(html, organization_id) {
module.exports = async function renderHtml(db_client, html, organization_id) {

let dep = [];
let dbCache = new Map();
Expand Down Expand Up @@ -31,7 +31,7 @@ module.exports = async function renderHtml(html, organization_id) {
else {

record = await
getDocument({
getDocument(db_client, {
collection: coll,
document_id: id
}, organization_id);
Expand Down
77 changes: 77 additions & 0 deletions src/routes/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var ObjectID = require('mongodb').ObjectID;

const select_db = (client, db_name) => {
if (client) {
return client.db(db_name)
}
}

module.exports = {
getDocument: async (client, data, dbName) => {

try {
const db = select_db(client, dbName); // obtenemos la conexión
//console.log(" Beginf Query")

const collection = db.collection(data["collection"]);

var query = {
"_id": new ObjectID(data["document_id"])
};

return await collection.findOne(query);
//console.log(" END Query")

}
catch(e){
console.log(" Catch Error finOne ---",e)
return null;
}
/*finally {
//client.close();
//console.log(" finlllay ")
}*/
},
getDocumentByQuery: async (client, data,dbName) => {

try {
const db = select_db(client, dbName); // obtenemos la conexión
const collection = db.collection(data["collection"]);
delete data["collection"];
var query = {
...data
};
return await collection.findOne(query);
}
catch(e){
console.log(" Catch Error finOne --- By Query",e)
return null;
}
},
organizationsfindOne : async (client, data, dbName) => {
try {
const db = select_db(client, dbName); // obtenemos la conexión
const collection = db.collection('organizations');
var query ={ "domains": { $in: [data["domains"]] } };
return await collection.findOne(query);
}catch(e){
console.log(" Catch Error OrganizationfinOne ---",e)
return null;
}
},
routesfindOne : async (client, data,dbName) => {
try {
const db = select_db(client, dbName); // obtenemos la conexión
const collection = db.collection('files');
return await collection.findOne({domains: { $in: [data["hostname"]] } , "path" : data["route_uri"]});
//return await collection.findOne({domains: { $in: [data["hostname"]] } , route : data["route_uri"]});
}catch(e){
console.log(" Catch Error ---",e)
return null;
}
/*finally {
console.log("Error FindOne")
return null;
}*/
}
}

0 comments on commit 4954fcc

Please sign in to comment.