Skip to content

Commit

Permalink
Failover db (#186)
Browse files Browse the repository at this point in the history
* Adds failover to database connection #179
  • Loading branch information
ManjotS authored Jun 23, 2020
1 parent e579112 commit e844702
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
36 changes: 30 additions & 6 deletions api/config/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
const metadata = {
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'root',
database: process.env.NODE_ENV === 'development' ? 'khajana_dev_khajana' : 'khajana_khajana',
dateStrings: true,
acquireTimeout: 5000,
connectionLimit: process.env.DB_POOL_SIZE,
};
const standbyMetadata = {
port: 3306,
minimumIdle: 0,
};

module.exports = {
mysql: {
mysql0: {
...metadata,
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'root',
database: process.env.NODE_ENV === 'development' ? 'khajana_dev_khajana' : 'khajana_khajana',
dateStrings: true,
connectionLimit: process.env.DB_POOL_SIZE,
},
mysql1: {
...metadata,
...standbyMetadata,
host: 'db1.khalis.net',
},
mysql2: {
...metadata,
...standbyMetadata,
host: 'db2.khalis.net',
},
mysql3: {
...metadata,
...standbyMetadata,
host: 'db3.khalis.net',
},
};
10 changes: 8 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const cacheControl = require('express-cache-controller');
const { createPool } = require('mariadb');
const { createPoolCluster } = require('mariadb');
const config = require('./api/config');
const routes = require('./api/routes');

const app = express();
const port = process.env.NODE_ENV === 'development' ? '3001' : '3000';

app.locals.pool = createPool(config.mysql);
// database
app.locals.pool = createPoolCluster();
app.locals.pool.add('local', config.mysql0);
app.locals.pool.add('db1', config.mysql1);
app.locals.pool.add('db2', config.mysql2);
app.locals.pool.add('db3', config.mysql3);

// app
app.use(cors());
app.use(cacheControl({ maxAge: 21600 }));
app.use(bodyParser.urlencoded({ extended: true }));
Expand Down

0 comments on commit e844702

Please sign in to comment.