From bfdfc27fac6ba6a3be524eddf02e329f9183d21b Mon Sep 17 00:00:00 2001 From: Manjot Singh Date: Thu, 28 Jul 2022 14:42:27 -0700 Subject: [PATCH 1/3] Add ability to work with single node db and ssl. (#258) * Add ssl cert config for skysql * Fix linting error and import fs module * convert serverCert to string from an array * Added ability to use cluster pools or not (#256) * Added ability to use cluster pools or not (checks if DB_NODES array exists) Also added ability to optionally use SSL_CA cert for connection * Added ability to use cluster pools or not (checks if DB_NODES array exists) Also added ability to optionally use SSL_CA cert for connection Co-authored-by: Gauravjeet Singh --- api/config/index.js | 12 ++++++++++++ app.js | 12 ++++++++---- package-lock.json | 7 ++++++- package.json | 5 +++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/api/config/index.js b/api/config/index.js index b50fa44..8362f10 100644 --- a/api/config/index.js +++ b/api/config/index.js @@ -1,3 +1,5 @@ +const fs = require('fs'); + /** * Expects process-dev.json or process.json (or run local) * @@ -11,6 +13,11 @@ * "DB_USER": "", * "DB_PASSWORD": "", * "DB_POOL_SIZE": 5, + * "SSL_CA": "/Downloads/aws_skysql_chain.pem", + * // single endpoint, use: + * "DB_HOST": "dba.contoso.net", + * "DB_PORT": 5001 + * // if you are using a write anywhere cluster, use DB_NODES: * "DB_NODES": [ * { * "host": "localhost", @@ -40,6 +47,11 @@ const metadata = { acquireTimeout: 6000, connectionLimit: process.env.DB_POOL_SIZE, }; + +if (process.env.SSL_CA) { + metadata.ssl = { ca: fs.readFileSync(process.env.SSL_CA, 'utf8') }; +} + const standbyMetadata = { minimumIdle: 2, }; diff --git a/app.js b/app.js index 8a16872..e5db5cd 100644 --- a/app.js +++ b/app.js @@ -2,7 +2,7 @@ const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const cacheControl = require('express-cache-controller'); -const { createPoolCluster } = require('mariadb'); +const { createPool, createPoolCluster } = require('mariadb'); const swaggerUi = require('swagger-ui-express'); const config = require('./api/config'); @@ -18,9 +18,13 @@ const app = express(); const port = process.env.NODE_ENV === 'development' ? '3001' : '3000'; // database -const dbCluster = createPoolCluster(); -config.forEach(dbConfig => dbCluster.add(dbConfig.host, dbConfig)); -app.locals.pool = dbCluster.of(/.*?/, 'ORDER'); +if (config.length > 1) { + const dbCluster = createPoolCluster(); + config.forEach(dbConfig => dbCluster.add(dbConfig.host, dbConfig)); + app.locals.pool = dbCluster.of(/.*?/, 'ORDER'); +} else { + app.locals.pool = createPool(config[0]); +} // app app.use(cors()); diff --git a/package-lock.json b/package-lock.json index 3e69281..fce1e0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "banidb-api", - "version": "2.9.4", + "version": "2.9.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3493,6 +3493,11 @@ "is-obj": "^1.0.0" } }, + "dotenv": { + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz", + "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==" + }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", diff --git a/package.json b/package.json index 590a029..c4eaf1d 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "banidb-api", - "version": "2.9.5", + "version": "2.10.0", "description": "BaniDB API", "main": "index.js", "scripts": { - "dev": "pm2 start process-dev.json", + "dev": " NODE_ENV=development pm2 start process-dev.json", "docker:clean": "npm run docker:stop && ./bin/clean-up-docker-containers.sh", "docker:start": "DB_PORT=3002 ./bin/start-docker.sh", "docker:stop": "./bin/stop-docker.sh", @@ -35,6 +35,7 @@ "anvaad-js": "^1.4.3", "body-parser": "^1.18.2", "cors": "^2.8.4", + "dotenv": "^16.0.1", "express": "^4.16.2", "express-cache-controller": "^1.1.0", "express-graphql": "^0.9.0", From 58ae794dede6fe111f44049236ea428a6b133369 Mon Sep 17 00:00:00 2001 From: Amitoj Date: Thu, 15 Sep 2022 14:30:38 -0400 Subject: [PATCH 2/3] fix: change connection end to release (#260) --- api/controllers/amritkeertan.js | 8 ++++---- api/controllers/banis.js | 8 ++++---- api/controllers/healthcheck.js | 2 +- api/controllers/kosh.js | 6 +++--- api/controllers/metadata.js | 6 +++--- api/controllers/rehats.js | 8 ++++---- api/controllers/shabads.js | 12 ++++++------ 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/api/controllers/amritkeertan.js b/api/controllers/amritkeertan.js index bba8d86..080eeb4 100644 --- a/api/controllers/amritkeertan.js +++ b/api/controllers/amritkeertan.js @@ -77,7 +77,7 @@ exports.headers = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -112,7 +112,7 @@ exports.index = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -147,7 +147,7 @@ exports.shabad = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -161,6 +161,6 @@ const getHeaderInfo = async (headerID, conn, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; diff --git a/api/controllers/banis.js b/api/controllers/banis.js index 6407ab2..1ccda57 100644 --- a/api/controllers/banis.js +++ b/api/controllers/banis.js @@ -59,7 +59,7 @@ const getAll = async req => { throw new Error(err); } finally { if (conn) { - conn.end(); + conn.release(); } } }; @@ -75,7 +75,7 @@ exports.all = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -138,7 +138,7 @@ exports.getBani = async ({ id, length, sinceDate }, req) => { } catch (err) { throw new Error(err); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } return null; }; @@ -167,7 +167,7 @@ exports.bani = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; diff --git a/api/controllers/healthcheck.js b/api/controllers/healthcheck.js index 15f4647..5fccbd7 100644 --- a/api/controllers/healthcheck.js +++ b/api/controllers/healthcheck.js @@ -12,7 +12,7 @@ exports.db = async (req, res) => { lib.error(err, res, 500, false); } finally { if (conn) { - conn.end(); + conn.release(); } } }; diff --git a/api/controllers/kosh.js b/api/controllers/kosh.js index 67e5014..fcbd4bf 100644 --- a/api/controllers/kosh.js +++ b/api/controllers/kosh.js @@ -22,7 +22,7 @@ exports.letter = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -41,7 +41,7 @@ exports.word = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -65,6 +65,6 @@ exports.search = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; diff --git a/api/controllers/metadata.js b/api/controllers/metadata.js index af8916c..c58b07b 100644 --- a/api/controllers/metadata.js +++ b/api/controllers/metadata.js @@ -15,7 +15,7 @@ exports.writers = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -35,7 +35,7 @@ exports.raags = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -54,6 +54,6 @@ exports.sources = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; diff --git a/api/controllers/rehats.js b/api/controllers/rehats.js index 768b2e1..5019abf 100644 --- a/api/controllers/rehats.js +++ b/api/controllers/rehats.js @@ -14,7 +14,7 @@ exports.all = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -34,7 +34,7 @@ exports.chapterList = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -63,7 +63,7 @@ exports.chapters = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -82,6 +82,6 @@ exports.search = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; diff --git a/api/controllers/shabads.js b/api/controllers/shabads.js index 04c871e..07a3398 100644 --- a/api/controllers/shabads.js +++ b/api/controllers/shabads.js @@ -254,7 +254,7 @@ exports.search = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -357,7 +357,7 @@ exports.hukamnamas = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } } }; @@ -388,7 +388,7 @@ exports.random = async (req, res) => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -450,7 +450,7 @@ const getShabad = (req, res, ShabadIDQ, sinceDate = null, forceMulti = false) => } else { resolve({}); } - if (conn) conn.end(); + if (conn) conn.release(); }) .catch(err => reject(err)); }) @@ -525,7 +525,7 @@ const getAngs = async (req, res, { pageNo, sinceDate, sourceID }) => { } catch (e) { throw e; } finally { - if (conn) conn.end(); + if (conn) conn.release(); } }; @@ -618,7 +618,7 @@ const getNavigation = async (req, res, type, first, last, source = '') => { } catch (err) { lib.error(err, res, 500); } finally { - if (conn) conn.end(); + if (conn) conn.release(); } return {}; }; From 7ddbac004ac070c641acc21b43e80157eb9f8035 Mon Sep 17 00:00:00 2001 From: Gauravjeetsingh Date: Mon, 5 Jun 2023 22:26:52 +0530 Subject: [PATCH 3/3] feat: add endpoint to get verse details from query --- api/controllers/shabads.js | 59 ++++++++++++++++++++++++++++++++++++++ api/routes/index.js | 2 ++ 2 files changed, 61 insertions(+) diff --git a/api/controllers/shabads.js b/api/controllers/shabads.js index 07a3398..0227977 100644 --- a/api/controllers/shabads.js +++ b/api/controllers/shabads.js @@ -258,6 +258,65 @@ exports.search = async (req, res) => { } }; +exports.resultsInfo = async (req, res) => { + const { VerseID } = req.params; + const results = 20; + let page = parseInt(req.query.page, 10) || 1; + if (lib.isListOfNumbers(VerseID)) { + let conn; + try { + conn = await req.app.locals.pool.getConnection(); + + const q = `SELECT ${allColumns} ${allFrom} + WHERE v.ID IN (${VerseID})`; + + const row = await conn.query(`SELECT COUNT(*) FROM (${q}) AS count`, []); + + const totalResults = row[0]['COUNT(*)']; + const totalPages = Math.ceil(totalResults / results); + if (page > totalPages) { + page = totalPages; + } + const resultsInfo = { + totalResults, + pageResults: totalResults, + pages: { + page, + resultsPerPage: results, + totalPages, + }, + }; + + if (totalResults > 0) { + if (page < totalPages) { + req.query.page = page + 1; + resultsInfo.pages.nextPage = `${req.protocol}://${req.get('host')}${req.baseUrl}${ + req.path + }?${Object.keys(req.query) + .map(key => `${key}=${encodeURIComponent(req.query[key])}`) + .join('&')}`; + } + const rows = await conn.query(`${q} LIMIT ?, ?`, [(page - 1) * results, results]); + const verses = rows.map(verse => lib.prepVerse(verse, true, false)); + resultsInfo.pageResults = verses.length; + res.json({ + resultsInfo, + verses, + }); + } else { + res.json({ + resultsInfo, + verses: [], + }); + } + } catch (err) { + lib.error(err, res, 500); + } finally { + if (conn) conn.release(); + } + } +}; + exports.shabads = async (req, res) => { let { ShabadID } = req.params; const sinceDate = req.query.updatedsince ? lib.isValidDatetime(req.query.updatedsince) : null; diff --git a/api/routes/index.js b/api/routes/index.js index 7662a65..0728eb3 100644 --- a/api/routes/index.js +++ b/api/routes/index.js @@ -31,6 +31,8 @@ route.get('/health', limiter.rate250, healthcheck.db); // Shabad Routes route.get('/search/:query', limiter.rate250, shabads.search); +route.get('/search-results/:VerseID', limiter.rate250, shabads.resultsInfo); + route.get('/shabads/:ShabadID', limiter.rate100, shabads.shabads); route.get('/angs/:PageNo/:SourceID?', limiter.rate100, shabads.angs);