Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to get config from file that is given in parameter #197

Merged
merged 12 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ sslcert
.idea/
*.log
env.json
settings.json
config.json
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ COPY . .
# ENV NODE_ENV ${NODE}

EXPOSE 8000
CMD [ "npm", "start", "--", "-v" ]
CMD [ "npm", "start", "--", "-v", "--listen", "0.0.0.0", "--port", "8000"]
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,34 @@ Available [here](doc/APIs)

## Configuration

By default it start server as development mode. You can configure environment using [env.json](`config/env/env.example.json`)
By default opentmi is started as development mode. You can configure environment using [`--config <file>`](`config.example.json`) -option.
```
{
"OPENTMI_BIND": "0.0.0.0",
"OPENTMI_PORT": 80,
"WEBTOKEN": "token",
"MONGODB": "mongodb://localhost/opentmi",
"FILE_DB": "./data",
"OPENTMI_ADMIN_USERNAME": "admin",
"OPENTMI_ADMIN_PASSWORD": "admin",
"GITHUB_CLIENTID": "ID",
"GITHUB_SECRET": "SECRET",
"GITHUB_ORG": "ORGANIZATION",
"GITHUB_CBURL": "GITHUB_CBURL",
"GITHUB_ADMINTEAM": "ADMIN-TEAM"
"name": "opentmi",
"host": "0.0.0.0",
"port": 3000,
"webtoken": "OpenTMI-toP-SeCRet-tOKEn",
"db": "mongodb://localhost/opentmi",
"filedb": "./data",
"admin": {
"user": "admin",
"pwd": "admin"
},
"mongo": {
"sslValidate": true
},
"github": {
"clientID": "<client-id>",
"clientSecret": "<client-secret>",
"callbackURL": "http://localhost:3000/auth/github/callback",
"organization": "<github-org>",
"adminTeam": "<admin-team>"
}
}
```

note: `"mongo"` options overwrites defaults and is pypassed to [MongoClient](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html).

# Architecture

* **Backend** (this repository)
Expand Down
4 changes: 2 additions & 2 deletions app/addons/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const childProcess = require('child_process');

// 3rd party modules
const express = require('express');
const logger = require('../tools/logger');
const Promise = require('bluebird');
const mongoose = require('mongoose');

const nconf = require('../../config');
const logger = require('../tools/logger');
const nconf = require('../tools/config');

const exec = Promise.promisify(childProcess.exec, {multiArgs: true});

Expand Down
10 changes: 6 additions & 4 deletions app/controllers/authentication.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require('colors');

const qs = require('querystring');
const nconf = require('../../config');

const mongoose = require('mongoose');
const request = require('request');
const auth = require('./../../config/middlewares/authorization');
const jwt = require('jwt-simple');
const async = require('async');
const _ = require('lodash');

const logger = require('../tools/logger');
const nconf = require('../tools/config');
const auth = require('../routes/middlewares/authorization');


const User = mongoose.model('User');
const Group = mongoose.model('Group');

const logger = require('../tools/logger');
// const googleSecret = nconf.get('google_secret');
const tokenSecret = nconf.get('webtoken');
const githubAdminTeam = nconf.get('github').adminTeam;
Expand Down
19 changes: 12 additions & 7 deletions app/db.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
const logger = require('./tools/logger');
const mongoose = require('mongoose');
const Promise = require('bluebird');
const _ = require('lodash');

const nconf = require('../config');
const logger = require('./tools/logger');
const config = require('./tools/config');

const dbUrl = nconf.get('db');
const dbUrl = config.get('db');
const dbOptions = config.get('mongo') || {};
mongoose.Promise = Promise;

let tearingDown = false;

const connect = function () {
const options = {
const must = {
logger: logger.info.bind(logger),
promiseLibrary: Promise,
useNewUrlParser: true
};
const overwritable = {
appname: 'opentmi',
validateOptions: true,
promiseLibrary: Promise,
useNewUrlParser: true,
loggerLevel: 'info' // @todo fetch from config file
loggerLevel: 'info'
};
const options = _.merge(overwritable, dbOptions, must);

logger.info(`Connecting to MongoDB: ${dbUrl}`);
return mongoose
Expand Down
6 changes: 3 additions & 3 deletions app/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const logger = require('./tools/logger');
const expressWinston = require('express-winston');

/* Project libraries */
const nconf = require('../config');
const config = require('./tools/config');
const pkg = require('../package.json');

const env = process.env.NODE_ENV || 'development';
Expand All @@ -37,7 +37,7 @@ module.exports = (app) => {
threshold: 512
}));

app.use(express.static(`${nconf.get('root')}/public`));
app.use(express.static(`${config.get('root')}/public`));

// Logging middleware
app.use(expressWinston.logger({
Expand Down Expand Up @@ -97,7 +97,7 @@ module.exports = (app) => {
saveUninitialized: true,
secret: pkg.name,
store: new MongoStore({
url: nconf.get('db'),
url: config.get('db'),
collection: 'sessions'
})
}));
Expand Down
38 changes: 19 additions & 19 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ const Promise = require('bluebird');
// application modules
const express = require('./express');
const Server = require('./server');
const nconf = require('../config');
const eventBus = require('./tools/eventBus');
const models = require('./models');
const routes = require('./routes');
const AddonManager = require('./addons');
const DB = require('./db');
const logger = require('./tools/logger');
const config = require('./tools/config');
const eventBus = require('./tools/eventBus');
const DB = require('./db');


if (nconf.get('help') || nconf.get('h')) {
nconf.stores.argv.showHelp();
if (config.get('help') || config.get('h')) {
config.stores.argv.showHelp();
process.exit(0);
}

// Defines
const https = nconf.get('https');
const listen = cluster.isMaster ? nconf.get('listen') : 'localhost';
const port = cluster.isMaster ? nconf.get('port') : 0;
const environment = nconf.get('env');
const dbUrl = nconf.get('db');
const https = config.get('https');
const listen = cluster.isMaster ? config.get('listen') : 'localhost';
const port = cluster.isMaster ? config.get('port') : 0;
const dbUrl = config.get('db');


// Create express instance
Expand All @@ -46,7 +46,7 @@ io.adapter(ioAdapter);
// Initialize database connection
DB.connect()
.catch((error) => {
logger.error('mongoDB connection failed: ', error.stack);
console.error('mongoDB connection failed: ', error.stack); // eslint-disable-line no-console
process.exit(-1);
})
.then(() => models.registerModels())
Expand All @@ -60,19 +60,19 @@ DB.connect()
.then(() => {
function onError(error) {
if (error.code === 'EACCES' && port < 1024) {
logger.error("You haven't access to open port below 1024");
logger.error("Please use admin rights if you wan't to use port %d!", port);
console.error("You haven't access to open port below 1024"); // eslint-disable-line no-console
console.error("Please use admin rights if you wan't to use port %d!", port); // eslint-disable-line no-console
} else if (error.code === 'EADDRINUSE') {
logger.error(`Port ${port} is already in use`);
console.error(`Port ${port} is already in use`); // eslint-disable-line no-console
} else {
logger.error(error);
console.error(error); // eslint-disable-line no-console
}
process.exit(-1);
}

function onListening() {
const listenurl = `${(https ? 'https' : 'http:')}://${listen}:${port}`;
logger.info(`OpenTMI started on ${listenurl} in ${environment} mode`);
logger.info(`OpenTMI started on ${listenurl}`);
eventBus.emit('start_listening', {url: listenurl});
}
server.on('error', onError);
Expand All @@ -96,10 +96,10 @@ DB.connect()
.then(() => logger.debug('Closing DB connection'))
.then(() => DB.disconnect().timeout(2000))
.catch((error) => {
logger.error(`shutdown Error: ${error}`);
console.error(`shutdown Error: ${error}`); // eslint-disable-line no-console
})
.finally(() => {
logger.info('Exit OpenTMI');
console.info('Exit OpenTMI'); // eslint-disable-line no-console
process.exit(0);
});
};
Expand All @@ -108,7 +108,7 @@ DB.connect()
process.on('SIGTERM', () => termination('SIGTERM'));
})
.catch((error) => {
logger.error('Exception during initialization: ', error);
console.error('Exception during initialization: ', error); // eslint-disable-line no-console
process.exit(-1);
});

Expand Down
2 changes: 1 addition & 1 deletion app/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const farmhash = require('farmhash');
// Local modules
const logger = require('./tools/logger');
const eventBus = require('./tools/eventBus');
const config = require('../config');
const config = require('./tools/config');


// Module variables
Expand Down
2 changes: 1 addition & 1 deletion app/models/extends/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mongoose = require('mongoose');

// local module
const logger = require('../../tools/logger');
const nconf = require('../../../config');
const nconf = require('../../tools/config');
const checksum = require('../../tools/checksum.js');

// Model variables
Expand Down
4 changes: 2 additions & 2 deletions app/routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const express = require('express');
const jwt = require('express-jwt');

// Application modules
const nconf = require('../../config');
const auth = require('./../../config/middlewares/authorization');
const nconf = require('../tools/config');
const auth = require('./middlewares/authorization');
const AdminController = require('./../controllers/admin');
const {notClustered} = require('./../controllers/common');

Expand Down
4 changes: 2 additions & 2 deletions app/routes/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const express = require('express');
const jwt = require('express-jwt');

// Local modules
const nconf = require('../../config');
const auth = require('./../../config/middlewares/authorization');
const nconf = require('../tools/config');
const auth = require('./middlewares/authorization');
const ClusterController = require('./../controllers/clusters');
const {notClustered} = require('./../controllers/common');

Expand Down
4 changes: 2 additions & 2 deletions app/routes/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const express = require('express');
const jwt = require('express-jwt');

// application modules
const nconf = require('../../config');
const {ensureAdmin, ensureAuthenticated} = require('./../../config/middlewares/authorization');
const nconf = require('../tools/config');
const {ensureAdmin, ensureAuthenticated} = require('./middlewares/authorization');
const EventsController = require('./../controllers/events');

// Route variables
Expand Down
4 changes: 2 additions & 2 deletions app/routes/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const express = require('express');
const jwt = require('express-jwt');

// Local modules
const nconf = require('../../config');
const auth = require('./../../config/middlewares/authorization');
const nconf = require('../tools/config');
const auth = require('./middlewares/authorization');
const ItemController = require('./../controllers/items');

const TOKEN_SECRET = nconf.get('webtoken');
Expand Down
4 changes: 2 additions & 2 deletions app/routes/loans.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const express = require('express');
const jwt = require('express-jwt');

// Application modules
const nconf = require('../../config');
const auth = require('./../../config/middlewares/authorization');
const nconf = require('../tools/config');
const auth = require('./middlewares/authorization');
const LoanController = require('./../controllers/loans');

// Route variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
const jwt = require('jwt-simple');
const moment = require('moment');
const mongoose = require('mongoose');
const logger = require('../../app/tools/logger');
const async = require('async');
const _ = require('lodash');

// Local modules
const nconf = require('../../config');
require('../../app/models/group');
const logger = require('../../tools/logger');
const nconf = require('../../tools/config');
require('../../models/group');

// Middleware variables
const TOKEN_SECRET = nconf.get('webtoken');
Expand Down
4 changes: 2 additions & 2 deletions app/routes/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const jwt = require('express-jwt');

// Application modules
const SchemaController = require('./../controllers/schemas');
const auth = require('./../../config/middlewares/authorization');
const nconf = require('../../config');
const auth = require('./middlewares/authorization');
const nconf = require('../tools/config');

// Route variables
const TOKEN_SECRET = nconf.get('webtoken');
Expand Down
2 changes: 1 addition & 1 deletion app/routes/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express');
const nconf = require('../../config');
const nconf = require('../tools/config');

const admin = nconf.get('admin');

Expand Down
2 changes: 1 addition & 1 deletion app/routes/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const socketioJwt = require('socketio-jwt');
const _ = require('lodash');

// Application modules
const nconf = require('../../config');
const nconf = require('../tools/config');
const Controller = require('../controllers/socketio');
const logger = require('../tools/logger');
const eventBus = require('../tools/eventBus');
Expand Down
4 changes: 2 additions & 2 deletions app/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Third party modules
const express = require('express');
const mongoose = require('mongoose');
const nconf = require('../../config');
const nconf = require('../tools/config');
const jwt = require('express-jwt');
const logger = require('../tools/logger');

// Local modules
const auth = require('./../../config/middlewares/authorization');
const auth = require('./middlewares/authorization');
const apiKeys = require('./../controllers/apikeys');
const UserController = require('./../controllers/users');
const AuthController = require('./../controllers/authentication');
Expand Down
Loading