loopback/lib/application.js

585 lines
16 KiB
JavaScript
Raw Normal View History

2016-05-04 00:10:46 +00:00
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2014-01-06 23:52:08 +00:00
/*!
2013-05-01 19:11:43 +00:00
* Module dependencies.
*/
var DataSource = require('loopback-datasource-juggler').DataSource;
var Registry = require('./registry');
var assert = require('assert');
var fs = require('fs');
var extend = require('util')._extend;
var RemoteObjects = require('strong-remoting');
var classify = require('underscore.string/classify');
var camelize = require('underscore.string/camelize');
var path = require('path');
var util = require('util');
2013-05-01 19:11:43 +00:00
/**
2014-01-06 23:52:08 +00:00
* The `App` object represents a Loopback application.
2014-10-16 22:54:40 +00:00
*
2014-01-06 23:52:08 +00:00
* The App object extends [Express](http://expressjs.com/api.html#express) and
* supports Express middleware. See
* [Express documentation](http://expressjs.com/) for details.
2014-10-16 22:54:40 +00:00
*
2014-01-06 23:52:08 +00:00
* ```js
* var loopback = require('loopback');
* var app = loopback();
2014-10-16 22:54:40 +00:00
*
2014-01-06 23:52:08 +00:00
* app.get('/', function(req, res){
* res.send('hello world');
* });
2014-10-16 22:54:40 +00:00
*
2014-01-06 23:52:08 +00:00
* app.listen(3000);
* ```
2014-10-16 22:54:40 +00:00
*
2014-01-06 23:52:08 +00:00
* @class LoopBackApplication
* @header var app = loopback()
2014-01-06 23:52:08 +00:00
*/
function App() {
// this is a dummy placeholder for jsdox
2014-01-06 23:52:08 +00:00
}
/*!
2013-05-01 19:11:43 +00:00
* Export the app prototype.
*/
var app = module.exports = {};
2013-05-01 19:11:43 +00:00
/**
2014-01-06 23:52:08 +00:00
* Lazily load a set of [remote objects](http://apidocs.strongloop.com/strong-remoting/#remoteobjectsoptions).
*
2014-04-02 22:15:21 +00:00
* **NOTE:** Calling `app.remotes()` more than once returns only a single set of remote objects.
2014-01-06 23:52:08 +00:00
* @returns {RemoteObjects}
2013-05-23 16:53:42 +00:00
*/
app.remotes = function() {
if (this._remotes) {
2013-05-23 16:53:42 +00:00
return this._remotes;
} else {
2014-04-14 19:25:41 +00:00
var options = {};
if (this.get) {
2014-04-14 19:25:41 +00:00
options = this.get('remoting');
}
2014-10-16 22:54:40 +00:00
return (this._remotes = RemoteObjects.create(options));
2013-05-23 16:53:42 +00:00
}
2014-10-16 22:54:40 +00:00
};
2013-05-23 16:53:42 +00:00
2014-01-06 23:52:08 +00:00
/*!
2013-05-23 16:53:42 +00:00
* Remove a route by reference.
2013-05-01 19:11:43 +00:00
*/
app.disuse = function(route) {
if (this.stack) {
2013-05-01 19:11:43 +00:00
for (var i = 0; i < this.stack.length; i++) {
if (this.stack[i].route === route) {
2013-05-01 19:11:43 +00:00
this.stack.splice(i, 1);
}
}
}
2014-10-16 22:54:40 +00:00
};
2013-05-01 19:11:43 +00:00
2013-05-24 14:59:23 +00:00
/**
* Attach a model to the app. The `Model` will be available on the
2014-01-06 23:52:08 +00:00
* `app.models` object.
2013-05-01 19:11:43 +00:00
*
* Example - Attach an existing model:
```js
* var User = loopback.User;
* app.model(User);
*```
* Example - Attach an existing model, alter some aspects of the model:
* ```js
* var User = loopback.User;
* app.model(User, { dataSource: 'db' });
*```
2014-10-16 22:54:40 +00:00
*
* @param {Object|String} Model The model to attach.
2014-04-02 22:15:21 +00:00
* @options {Object} config The model's configuration.
* @property {String|DataSource} dataSource The `DataSource` to which to attach the model.
* @property {Boolean} [public] Whether the model should be exposed via REST API.
* @property {Object} [relations] Relations to add/update.
2014-01-06 23:52:08 +00:00
* @end
* @returns {ModelConstructor} the model class
2013-05-01 19:11:43 +00:00
*/
2013-05-24 14:59:23 +00:00
app.model = function(Model, config) {
var isPublic = true;
var registry = this.registry;
if (arguments.length > 1) {
config = config || {};
if (typeof Model === 'string') {
// create & attach the model - backwards compatibility
// create config for loopback.modelFromConfig
var modelConfig = extend({}, config);
modelConfig.options = extend({}, config.options);
modelConfig.name = Model;
// modeller does not understand `dataSource` option
delete modelConfig.dataSource;
Model = registry.createModel(modelConfig);
// delete config options already applied
['relations', 'base', 'acls', 'hidden', 'methods'].forEach(function(prop) {
delete config[prop];
if (config.options) delete config.options[prop];
});
delete config.properties;
}
configureModel(Model, config, this);
isPublic = config.public !== false;
} else {
assert(Model.prototype instanceof Model.registry.getModel('Model'),
2014-07-16 18:04:23 +00:00
Model.modelName + ' must be a descendant of loopback.Model');
}
var modelName = Model.modelName;
2013-10-29 21:12:23 +00:00
this.models[modelName] =
this.models[classify(modelName)] =
this.models[camelize(modelName)] = Model;
2013-10-29 21:12:23 +00:00
this.models().push(Model);
if (isPublic && Model.sharedClass) {
this.remotes().addClass(Model.sharedClass);
if (Model.settings.trackChanges && Model.Change) {
this.remotes().addClass(Model.Change.sharedClass);
}
clearHandlerCache(this);
this.emit('modelRemoted', Model.sharedClass);
2013-11-18 20:52:00 +00:00
}
var self = this;
Model.on('remoteMethodDisabled', function(model, methodName) {
self.emit('remoteMethodDisabled', model, methodName);
});
Model.shared = isPublic;
Model.app = this;
Model.emit('attached', this);
2013-10-29 21:12:23 +00:00
return Model;
};
2013-05-24 14:59:23 +00:00
/**
2014-04-02 22:15:21 +00:00
* Get the models exported by the app. Returns only models defined using `app.model()`
*
2014-04-02 22:15:21 +00:00
* There are two ways to access models:
*
2014-04-02 22:15:21 +00:00
* 1. Call `app.models()` to get a list of all models.
*
* ```js
* var models = app.models();
*
* models.forEach(function(Model) {
* console.log(Model.modelName); // color
* });
* ```
*
* 2. Use `app.model` to access a model by name.
2015-10-21 20:47:36 +00:00
* `app.models` has properties for all defined models.
*
2014-04-02 22:15:21 +00:00
* The following example illustrates accessing the `Product` and `CustomerReceipt` models
* using the `models` object.
*
* ```js
* var loopback = require('loopback');
* var app = loopback();
* app.boot({
* dataSources: {
* db: {connector: 'memory'}
* }
* });
*
* app.model('product', {dataSource: 'db'});
* app.model('customer-receipt', {dataSource: 'db'});
*
* // available based on the given name
* var Product = app.models.Product;
*
* // also available as camelCase
* var product = app.models.product;
*
* // multi-word models are avaiable as pascal cased
* var CustomerReceipt = app.models.CustomerReceipt;
*
* // also available as camelCase
* var customerReceipt = app.models.customerReceipt;
* ```
2014-01-06 23:52:08 +00:00
*
2014-04-02 22:15:21 +00:00
* @returns {Array} Array of model classes.
2013-05-24 14:59:23 +00:00
*/
app.models = function() {
2013-11-15 04:19:46 +00:00
return this._models || (this._models = []);
2014-10-16 22:54:40 +00:00
};
2013-05-01 19:11:43 +00:00
2013-10-31 17:06:43 +00:00
/**
* Define a DataSource.
2014-01-06 23:52:08 +00:00
*
* @param {String} name The data source name
* @param {Object} config The data source config
2013-10-31 17:06:43 +00:00
*/
app.dataSource = function(name, config) {
try {
var ds = dataSourcesFromConfig(name, config, this.connectors, this.registry);
this.dataSources[name] =
this.dataSources[classify(name)] =
this.dataSources[camelize(name)] = ds;
return ds;
} catch (err) {
if (err.message) {
err.message = 'Cannot create data source ' + JSON.stringify(name) +
': ' + err.message;
}
throw err;
}
2014-10-16 22:54:40 +00:00
};
2013-10-31 17:06:43 +00:00
/**
* Register a connector.
*
* When a new data-source is being added via `app.dataSource`, the connector
* name is looked up in the registered connectors first.
*
* Connectors are required to be explicitly registered only for applications
* using browserify, because browserify does not support dynamic require,
* which is used by LoopBack to automatically load the connector module.
*
* @param {String} name Name of the connector, e.g. 'mysql'.
* @param {Object} connector Connector object as returned
* by `require('loopback-connector-{name}')`.
*/
app.connector = function(name, connector) {
this.connectors[name] =
this.connectors[classify(name)] =
this.connectors[camelize(name)] = connector;
};
2013-05-24 22:08:23 +00:00
/**
2013-07-17 21:30:38 +00:00
* Get all remote objects.
2014-04-02 22:15:21 +00:00
* @returns {Object} [Remote objects](http://apidocs.strongloop.com/strong-remoting/#remoteobjectsoptions).
2013-05-24 22:08:23 +00:00
*/
app.remoteObjects = function() {
2013-07-17 21:30:38 +00:00
var result = {};
this.remotes().classes().forEach(function(sharedClass) {
result[sharedClass.name] = sharedClass.ctor;
2013-07-17 21:30:38 +00:00
});
2014-10-16 22:54:40 +00:00
2013-07-17 21:30:38 +00:00
return result;
2014-10-16 22:54:40 +00:00
};
2013-07-25 23:24:00 +00:00
/*!
* Get a handler of the specified type from the handler cache.
2014-10-16 22:54:40 +00:00
* @triggers `mounted` events on shared class constructors (models)
2013-07-25 23:24:00 +00:00
*/
2014-10-16 22:54:40 +00:00
app.handler = function(type, options) {
2013-07-25 23:24:00 +00:00
var handlers = this._handlers || (this._handlers = {});
if (handlers[type]) {
2013-07-25 23:24:00 +00:00
return handlers[type];
}
2014-10-16 22:54:40 +00:00
2013-07-25 23:24:00 +00:00
var remotes = this.remotes();
var handler = this._handlers[type] = remotes.handler(type, options);
2014-10-16 22:54:40 +00:00
remotes.classes().forEach(function(sharedClass) {
sharedClass.ctor.emit('mounted', app, sharedClass, remotes);
});
2014-10-16 22:54:40 +00:00
2013-07-25 23:24:00 +00:00
return handler;
2014-10-16 22:54:40 +00:00
};
2013-07-25 23:24:00 +00:00
2013-10-29 21:12:23 +00:00
/**
* An object to store dataSource instances.
*/
app.dataSources = app.datasources = {};
2013-11-15 04:19:46 +00:00
/**
* Enable app wide authentication.
*/
app.enableAuth = function(options) {
var AUTH_MODELS = ['User', 'AccessToken', 'ACL', 'Role', 'RoleMapping'];
2013-11-15 04:19:46 +00:00
var remotes = this.remotes();
var app = this;
2013-11-15 04:19:46 +00:00
if (options && options.dataSource) {
var appModels = app.registry.modelBuilder.models;
AUTH_MODELS.forEach(function(m) {
var Model = app.registry.findModel(m);
if (!Model) {
throw new Error(
'Authentication requires model ' + m + ' to be defined.');
}
if (m.dataSource || m.app) return;
for (var name in appModels) {
var candidate = appModels[name];
var isSubclass = candidate.prototype instanceof Model;
var isAttached = !!candidate.dataSource || !!candidate.app;
if (isSubclass && isAttached) return;
}
app.model(Model, {
dataSource: options.dataSource,
public: m === 'User'
});
});
}
remotes.authorization = function(ctx, next) {
var method = ctx.method;
2013-11-15 04:19:46 +00:00
var req = ctx.req;
var Model = method.ctor;
var modelInstance = ctx.instance;
var modelId = modelInstance && modelInstance.id ||
// replacement for deprecated req.param()
(req.params && req.params.id !== undefined ? req.params.id :
req.body && req.body.id !== undefined ? req.body.id :
req.query && req.query.id !== undefined ? req.query.id :
undefined);
var modelName = Model.modelName;
2013-11-15 04:19:46 +00:00
var modelSettings = Model.settings || {};
var errStatusCode = modelSettings.aclErrorStatus || app.get('aclErrorStatus') || 401;
if (!req.accessToken) {
errStatusCode = 401;
}
if (Model.checkAccess) {
Model.checkAccess(
req.accessToken,
modelId,
method,
ctx,
function(err, allowed) {
if (err) {
console.log(err);
next(err);
} else if (allowed) {
next();
} else {
var messages = {
403: {
message: 'Access Denied',
code: 'ACCESS_DENIED'
},
404: {
message: ('could not find ' + modelName + ' with id ' + modelId),
code: 'MODEL_NOT_FOUND'
},
401: {
message: 'Authorization Required',
code: 'AUTHORIZATION_REQUIRED'
}
};
var e = new Error(messages[errStatusCode].message || messages[403].message);
e.statusCode = errStatusCode;
e.code = messages[errStatusCode].code || messages[403].code;
next(e);
}
2013-11-15 04:19:46 +00:00
}
);
} else {
next();
}
};
this.isAuthEnabled = true;
};
2013-11-15 04:19:46 +00:00
2013-10-29 21:12:23 +00:00
app.boot = function(options) {
throw new Error(
'`app.boot` was removed, use the new module loopback-boot instead');
2014-10-16 22:54:40 +00:00
};
2013-10-29 21:12:23 +00:00
function dataSourcesFromConfig(name, config, connectorRegistry, registry) {
var connectorPath;
2014-10-16 22:54:40 +00:00
assert(typeof config === 'object',
'can not create data source without config object');
if (typeof config.connector === 'string') {
name = config.connector;
if (connectorRegistry[name]) {
config.connector = connectorRegistry[name];
} else {
connectorPath = path.join(__dirname, 'connectors', name + '.js');
if (fs.existsSync(connectorPath)) {
config.connector = require(connectorPath);
}
}
if (!config.connector.name)
config.connector.name = name;
}
return registry.createDataSource(config);
2013-10-29 21:12:23 +00:00
}
function configureModel(ModelCtor, config, app) {
assert(ModelCtor.prototype instanceof ModelCtor.registry.getModel('Model'),
2014-07-16 18:04:23 +00:00
ModelCtor.modelName + ' must be a descendant of loopback.Model');
var dataSource = config.dataSource;
if (dataSource) {
if (typeof dataSource === 'string') {
dataSource = app.dataSources[dataSource];
}
2013-10-29 21:12:23 +00:00
assert(
dataSource instanceof DataSource,
ModelCtor.modelName + ' is referencing a dataSource that does not exist: "' +
config.dataSource + '"'
);
}
config = extend({}, config);
config.dataSource = dataSource;
setSharedMethodSharedProperties(ModelCtor, app, config);
app.registry.configureModel(ModelCtor, config);
}
function setSharedMethodSharedProperties(model, app, modelConfigs) {
var settings = {};
// apply config.json settings
var config = app.get('remoting');
var configHasSharedMethodsSettings = config &&
config.sharedMethods &&
typeof config.sharedMethods === 'object';
if (configHasSharedMethodsSettings)
util._extend(settings, config.sharedMethods);
// apply model-config.json settings
var modelConfig = modelConfigs.options;
var modelConfigHasSharedMethodsSettings = modelConfig &&
modelConfig.remoting &&
modelConfig.remoting.sharedMethods &&
typeof modelConfig.remoting.sharedMethods === 'object';
if (modelConfigHasSharedMethodsSettings)
util._extend(settings, modelConfig.remoting.sharedMethods);
// validate setting values
Object.keys(settings).forEach(function(setting) {
var settingValue = settings[setting];
var settingValueType = typeof settingValue;
if (settingValueType !== 'boolean')
throw new TypeError('Expected boolean, got ' + settingValueType);
});
// set sharedMethod.shared using the merged settings
var sharedMethods = model.sharedClass.methods({includeDisabled: true});
sharedMethods.forEach(function(sharedMethod) {
// use the specific setting if it exists
var hasSpecificSetting = settings.hasOwnProperty(sharedMethod.name);
if (hasSpecificSetting) {
sharedMethod.shared = settings[sharedMethod.name];
} else { // otherwise, use the default setting if it exists
var hasDefaultSetting = settings.hasOwnProperty('*');
if (hasDefaultSetting)
sharedMethod.shared = settings['*'];
}
});
}
function clearHandlerCache(app) {
app._handlers = undefined;
}
/**
* Listen for connections and update the configured port.
*
* When there are no parameters or there is only one callback parameter,
* the server will listen on `app.get('host')` and `app.get('port')`.
*
* For example, to listen on host/port configured in app config:
* ```js
* app.listen();
* ```
*
* Otherwise all arguments are forwarded to `http.Server.listen`.
*
* For example, to listen on the specified port and all hosts, and ignore app config.
* ```js
* app.listen(80);
* ```
*
* The function also installs a `listening` callback that calls
* `app.set('port')` with the value returned by `server.address().port`.
* This way the port param contains always the real port number, even when
* listen was called with port number 0.
*
* @param {Function} [cb] If specified, the callback is added as a listener
* for the server's "listening" event.
* @returns {http.Server} A node `http.Server` with this application configured
* as the request handler.
*/
app.listen = function(cb) {
var self = this;
var server = require('http').createServer(this);
server.on('listening', function() {
self.set('port', this.address().port);
var listeningOnAll = false;
var host = self.get('host');
if (!host) {
listeningOnAll = true;
host = this.address().address;
self.set('host', host);
} else if (host === '0.0.0.0' || host === '::') {
listeningOnAll = true;
}
if (!self.get('url')) {
if (process.platform === 'win32' && listeningOnAll) {
// Windows browsers don't support `0.0.0.0` host in the URL
// We are replacing it with localhost to build a URL
// that can be copied and pasted into the browser.
host = 'localhost';
}
var url = 'http://' + host + ':' + self.get('port') + '/';
self.set('url', url);
}
});
var useAppConfig =
2014-10-16 23:40:04 +00:00
arguments.length === 0 ||
(arguments.length == 1 && typeof arguments[0] == 'function');
if (useAppConfig) {
var port = this.get('port');
// NOTE(bajtos) port:undefined no longer works on node@6,
// we must pass port:0 explicitly
if (port === undefined) port = 0;
server.listen(port, this.get('host'), cb);
} else {
server.listen.apply(server, arguments);
}
return server;
2014-10-16 22:54:40 +00:00
};