Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP – split out db / store / account config
- Loading branch information
Showing
10 changed files
with
172 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = accountConfig | ||
|
||
var defaultsDeep = require('lodash').defaultsDeep | ||
|
||
function accountConfig (state, callback) { | ||
var usersDb = state.getDatabase(state.config.db.authenticationDb) | ||
usersDb.constructor.plugin(require('pouchdb-users')) | ||
|
||
usersDb.installUsersBehavior() | ||
|
||
.then(function () { | ||
defaultsDeep(state.config.account, { | ||
admins: state.config.db.admins, | ||
secret: state.config.db.secret, | ||
usersDb: usersDb, | ||
notifications: state.config.account.notifications | ||
}) | ||
|
||
callback(null) | ||
}) | ||
|
||
.catch(callback, state.config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,34 @@ | ||
module.exports = getConfig | ||
var internals = module.exports.internals = { | ||
getDefaults: require('./defaults'), | ||
accountConfig: require('./account'), | ||
couchDbConfig: require('./db/couchdb'), | ||
pouchDbConfig: require('./db/pouchdb') | ||
getDatabaseFactory: require('./db/factory'), | ||
parseOptions: require('./parse-options'), | ||
pouchDbConfig: require('./db/pouchdb'), | ||
storeConfig: require('./store') | ||
} | ||
|
||
var parseUrl = require('url').parse | ||
var path = require('path') | ||
|
||
var defaultsDeep = require('lodash').defaultsDeep | ||
var log = require('npmlog') | ||
|
||
var getDatabaseFactory = require('./db/factory') | ||
var series = require('async').series | ||
|
||
function getConfig (options, callback) { | ||
var defaults = internals.getDefaults() | ||
var config = { | ||
paths: { | ||
data: options.data, | ||
public: options.public | ||
}, | ||
app: { | ||
hostname: options.bindAddress, | ||
port: options.port | ||
} | ||
if (options.dbUrl && !parseUrl(options.dbUrl).auth) { | ||
return callback('Authentication details missing from database URL: ' + options.dbUrl) | ||
} | ||
var getDbConfig | ||
|
||
defaultsDeep(config, defaults) | ||
|
||
if (options.dbUrl) { | ||
config.db.url = options.dbUrl | ||
if (!parseUrl(options.dbUrl).auth) { | ||
return callback('Authentication details missing from database URL: ' + options.dbUrl) | ||
} | ||
|
||
log.info('config', 'Connecting to CouchDB at ' + removeAuth(options.dbUrl)) | ||
|
||
getDbConfig = internals.couchDbConfig | ||
} else { | ||
if (options.inMemory) { | ||
log.info('config', 'Storing all data in memory') | ||
config.db.db = require('memdown') | ||
} else { | ||
config.db.prefix = path.join(config.paths.data, 'data/') | ||
log.info('config', 'No CouchDB URL provided, falling back to PouchDB') | ||
log.info('config', 'Writing PouchDB database files to ' + config.db.prefix) | ||
} | ||
|
||
getDbConfig = internals.pouchDbConfig | ||
var config = internals.parseOptions(options) | ||
var state = { | ||
config: config, | ||
getDatabase: internals.getDatabaseFactory(config) | ||
} | ||
|
||
getDbConfig(config, function (error, couchConfig) { | ||
if (error) { | ||
return callback(error) | ||
} | ||
|
||
config.db.secret = couchConfig.secret | ||
config.db.admins = couchConfig.admins | ||
config.db.authenticationDb = couchConfig.authentication_db | ||
|
||
var getDatabase = getDatabaseFactory(config) | ||
var usersDb = getDatabase(config.db.authenticationDb) | ||
usersDb.constructor.plugin(require('pouchdb-users')) | ||
|
||
usersDb.installUsersBehavior() | ||
|
||
.then(function () { | ||
// account config | ||
defaultsDeep(config.account, { | ||
admins: couchConfig.admins, | ||
secret: config.db.secret, | ||
usersDb: usersDb, | ||
notifications: config.account.notifications | ||
}) | ||
|
||
// store config | ||
if (config.db.url) { | ||
config.store.couchdb = removeAuth(config.db.url) | ||
} else { | ||
config.store.PouchDB = usersDb.constructor | ||
} | ||
|
||
callback(null, config) | ||
}) | ||
|
||
.catch(callback) | ||
var dbConfig = state.config.db.url ? internals.couchDbConfig : internals.pouchDbConfig | ||
state.getDatabase.PouchDB.plugin(require('pouchdb-users')) | ||
|
||
series([ | ||
dbConfig.bind(null, state), | ||
internals.accountConfig.bind(null, state), | ||
internals.storeConfig.bind(null, state) | ||
], function (error) { | ||
callback(error, state.config) | ||
}) | ||
} | ||
|
||
function removeAuth (url) { | ||
var parts = parseUrl(url) | ||
return url.replace(parts.auth + '@', '') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module.exports = parseOptions | ||
|
||
var path = require('path') | ||
|
||
var defaultsDeep = require('lodash').defaultsDeep | ||
var log = require('npmlog') | ||
|
||
var getDefaults = require('./defaults') | ||
var removeAuth = require('../utils/remove-auth-from-url') | ||
|
||
function parseOptions (options, callback) { | ||
var config = { | ||
db: {}, | ||
paths: { | ||
data: options.data, | ||
public: options.public | ||
}, | ||
app: { | ||
hostname: options.bindAddress, | ||
port: options.port | ||
} | ||
} | ||
|
||
defaultsDeep(config, getDefaults()) | ||
|
||
if (options.dbUrl) { | ||
config.db.url = options.dbUrl | ||
log.info('config', 'Connecting to CouchDB at ' + removeAuth(options.dbUrl)) | ||
} else { | ||
if (options.inMemory) { | ||
log.info('config', 'Storing all data in memory only') | ||
config.db.db = require('memdown') | ||
} else { | ||
config.db.prefix = path.join(config.paths.data, 'data') | ||
log.info('config', 'No CouchDB URL provided, falling back to PouchDB') | ||
log.info('config', 'Writing PouchDB database files to ' + config.db.prefix) | ||
} | ||
} | ||
|
||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = storeConfig | ||
|
||
var removeAuth = require('../../utils/remove-auth-from-url') | ||
|
||
function storeConfig (state, callback) { | ||
if (state.config.db.url) { | ||
state.config.store.couchdb = removeAuth(state.config.db.url) | ||
} else { | ||
state.config.store.PouchDB = state.getDatabase.PouchDB.constructor | ||
} | ||
|
||
callback(null, state.config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = removeAuth | ||
|
||
var parseUrl = require('url').parse | ||
|
||
function removeAuth (url) { | ||
var parts = parseUrl(url) | ||
return url.replace(parts.auth + '@', '') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.