1
0
mirror of https://github.com/dap-ps/discover.git synced 2025-01-31 03:26:13 +00:00

add config/index.js for central config management

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-08-05 12:36:32 -04:00
parent ff01763803
commit f7c0be8eb6
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
11 changed files with 103 additions and 29 deletions

View File

@ -1,4 +1,5 @@
const web3 = require('./web3');
const config = require('./../config')
let DiscoverABI = [
{
@ -479,4 +480,4 @@ let DiscoverABI = [
}
];
module.exports = web3.eth.Contract(DiscoverABI, process.env.DISCOVER_CONTRACT);
module.exports = new web3.eth.Contract(DiscoverABI, config.DISCOVER_CONTRACT);

View File

@ -1,2 +1,6 @@
const Web3 = require('web3');
module.exports = new Web3(new Web3.providers.WebsocketProvider(process.env.BLOCKCHAIN_CONNECTION_POINT));
const config = require('../config')
module.exports = new Web3(
new Web3.providers.WebsocketProvider(config.BLOCKCHAIN_CONNECTION_POINT)
);

View File

@ -1,10 +1,14 @@
let mongoose = require("mongoose");
let mongoose = require('mongoose');
const config = require('./');
class DBConfig {
static config() {
if (config.DB_CONNECTION == undefined) {
throw Error('Unable to find MongoDB URI in DB_CONNECTION env variable!')
}
mongoose.Promise = global.Promise;
mongoose.connect(process.env.DB_CONNECTION, { useNewUrlParser: true });
mongoose.connect(config.DB_CONNECTION, { useNewUrlParser: true });
}
}

39
back-end/config/index.js Normal file
View File

@ -0,0 +1,39 @@
/* This file is a central place for managing backend settings */
/* shorthand */
const env = process.env
/* some defaults cannot be known in advance */
const config = {
/* Hosting */
PORT : env.PORT || 4000,
RATE_LIMIT_TIME : env.RATE_LIMIT_TIME || 15,
RATE_LIMIT_MAX_REQ : env.RATE_LIMIT_MAX_REQ || 1,
/* Misc */
ENVIRONMENT : env.ENVIRONMENT || "DEV",
/* Database */
DB_CONNECTION : env.DB_CONNECTION || null,
/* Access */
ADMIN_USER : env.ADMIN_USER || "admin",
ADMIN_PASSWORD : env.ADMIN_PASSWORD || "discoverbancor",
/* Blockchain */
IPFS_HOST : env.IPFS_HOST || "ipfs.infura.io",
IPFS_PORT : env.IPFS_PORT || "5001",
IPFS_PROTOCOL : env.IPFS_PROTOCOL || "https",
DISCOVER_CONTRACT : env.DISCOVER_CONTRACT || "0x25B1bD06fBfC2CbDbFc174e10f1B78b1c91cc77B",
BLOCKCHAIN_CONNECTION_POINT : env.BLOCKCHAIN_CONNECTION_POINT || "wss://ropsten.infura.io/ws/v3/8675214b97b44e96b70d05326c61fd6a",
/* EMail */
EMAIL_HOST : env.EMAIL_HOST || null,
EMAIL_PORT : env.EMAIL_PORT || null,
EMAIL_TLS : env.EMAIL_TLS || null,
EMAIL_USER : env.EMAIL_USER || null,
EMAIL_PASSWORD : env.EMAIL_PASSWORD || null,
APPROVE_NOTIFIER_MAIL : env.APPROVE_NOTIFIER_MAIL || "dapps-approvals@status.im",
APPROVER_MAIL : env.APPROVER_MAIL || "dapps-approvals@status.im",
/* Logging */
CLOUDWATCH_ACCESS_KEY_ID : env.CLOUDWATCH_ACCESS_KEY_ID || null,
CLOUDWATCH_SECRET_ACCESS_KEY : env.CLOUDWATCH_SECRET_ACCESS_KEY || null,
CLOUDWATCH_REGION : env.CLOUDWATCH_REGION || null,
}
module.exports = config;

View File

@ -1,10 +1,16 @@
const Email = require('./base-email');
const config = require('../config')
class ApprovalEmail extends Email {
constructor(dapp) {
const emailBody = `A DApp metadata ${JSON.stringify(dapp.details)} has been uploaded. You can connect with the Dapp owner at email: ${dapp.email}`;
super(process.env.APPROVE_NOTIFIER_MAIL, process.env.APPROVER_MAIL, `Uploaded DApp Metadata. Hash - ${dapp.hash}`, emailBody);
super(
config.APPROVE_NOTIFIER_MAIL,
config.APPROVER_MAIL,
`Uploaded DApp Metadata. Hash - ${dapp.hash}`,
emailBody
);
}
}
module.exports = ApprovalEmail;
module.exports = ApprovalEmail;

View File

@ -1,9 +1,11 @@
const config = require('../config')
module.exports = {
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
secure: process.env.EMAIL_TLS,
host: config.EMAIL_HOST,
port: config.EMAIL_PORT,
secure: config.EMAIL_TLS,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD
user: config.EMAIL_USER,
pass: config.EMAIL_PASSWORD
}
};
};

View File

@ -1,14 +1,15 @@
const winston = require('winston');
const CloudWatchTransport = require('winston-aws-cloudwatch');
const config = require('../config')
let baseConfig = {
logGroupName: `${process.env.ENVIRONMENT}-logs`,
logGroupName: `${config.ENVIRONMENT}-logs`,
createLogGroup: true,
createLogStream: true,
awsConfig: {
accessKeyId: process.env.CLOUDWATCH_ACCESS_KEY_ID,
secretAccessKey: process.env.CLOUDWATCH_SECRET_ACCESS_KEY,
region: process.env.CLOUDWATCH_REGION
accessKeyId: config.CLOUDWATCH_ACCESS_KEY_ID,
secretAccessKey: config.CLOUDWATCH_SECRET_ACCESS_KEY,
region: config.CLOUDWATCH_REGION
}
}
@ -39,7 +40,7 @@ module.exports = {
transports: transports
});
if (process.env.ENVIRONMENT == 'DEV') {
if (config.ENVIRONMENT == 'DEV') {
logger.add(new winston.transports.Console());
} else {
// Set the Formatting per Logger, because we need to context
@ -51,4 +52,4 @@ module.exports = {
return logger;
},
};
};

View File

@ -1,12 +1,19 @@
const logger = require('./../../logger/logger').getLoggerFor('Admin-authorization');
const parseBasicAuthorization = require('./../../utils/authorization-utils').parseBasicAuthorization;
const logger = require('../../logger/logger').getLoggerFor('Admin-authorization');
const parseBasicAuthorization = require('../../utils/authorization-utils').parseBasicAuthorization;
const config = require('../../config')
class AdminAuthorizationMiddleware {
static verifyUserAuth(auth) {
return (
authorization.username == config.ADMIN_USER &&
authorization.password == config.ADMIN_PASSWORD
)
}
static authorize(req, res, next) {
try {
let authorization = parseBasicAuthorization(req.headers.authorization);
if (authorization.username == process.env.ADMIN_USER && authorization.password == process.env.ADMIN_PASSWORD) {
if (this.verifyUserAuth(authorization)) {
return void next();
}

View File

@ -1,17 +1,20 @@
let rateLimit = require('express-rate-limit');
const rateLimit = require('express-rate-limit');
const logger = require('./../../logger/logger').getLoggerFor("Rate-Limit");
const config = require('./../../config')
class RateLimitMiddleware {
static setup() {
const windowMs = config.RATE_LIMIT_TIME;
const maxReq = config.RATE_LIMIT_MAX_REQ;
let limiter = rateLimit({
windowMs: process.env.RATE_LIMIT_TIME,
max: process.env.MAX_REQUESTS_FOR_RATE_LIMIT_TIME,
windowMs: windowMs,
max: maxReq,
handler: function (req, res) {
logger.warn(this.message);
res.status(this.statusCode).send({ error: this.message });
},
message: `Rate limit was reached, you are able to do ${process.env.MAX_REQUESTS_FOR_RATE_LIMIT_TIME} requests per ${process.env.RATE_LIMIT_TIME} milliseconds`
message: `Rate limit was reached, you are able to do ${maxReq} requests per ${windowMs} milliseconds`
});
return limiter;

View File

@ -1,3 +1,5 @@
const config = require('./config')
function setupSystem() {
let dotenv = require("dotenv");
dotenv.config();
@ -18,8 +20,8 @@ async function setupAPI() {
setupPostRoutedAppMiddlewares(app);
app.use(express.static('frontend'));
app.listen(process.env.PORT);
console.log(`Server started on port: ${process.env.PORT}...`);
app.listen(config.PORT);
console.log(`Server started on port: ${config.PORT}...`);
return app;
}

View File

@ -1,11 +1,16 @@
const ipfsClient = require('ipfs-http-client');
const logger = require('./../logger/logger').getLoggerFor('IPFS-Service');
const logger = require('../logger/logger').getLoggerFor('IPFS-Service');
const config = require('../config')
class IPFSService {
constructor() {
if (!IPFSService.instance) {
this.storage = ipfsClient(process.env.IPFS_HOST, process.env.IPFS_PORT, { protocol: process.env.IPFS_PROTOCOL })
this.storage = ipfsClient(
config.IPFS_HOST,
config.IPFS_PORT,
{ protocol: config.IPFS_PROTOCOL }
)
IPFSService.instance = this;
}