Skip to content

Commit

Permalink
Merge pull request #40 from flyvictor/expose-metadata-synchronously
Browse files Browse the repository at this point in the history
Init /resource synchronously and respond with 503 until fortune is funct...
  • Loading branch information
bbraithwaite committed Jul 8, 2014
2 parents 14852bb + 2472c7f commit e9f8450
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/fortune.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ Fortune.prototype._init = function (options) {
this.adapter = new Adapter(options, inflect);
};

Fortune.prototype._resourceInitialized = function(){
this._resourcesStarted ++;
if (Object.keys(this._resources).length === this._resourcesStarted){
this._ready = true;
}
};

Fortune.prototype.resources = function(){
var _this = this;
return _.map(_this._resources, function(md, name){
Expand Down Expand Up @@ -148,9 +155,14 @@ Fortune.prototype._exposeResourceDefinitions = function() {
"/resources";

this.router.get(resourceRoute, function(req,res){
res.write(JSON.stringify({resources: _this.resources()}));
res.end();
});
if (_this._ready){
res.write(JSON.stringify({resources: _this.resources()}));
res.end();
}else{
res.writeHead(503);
res.end();
}
});
};

/**
Expand Down Expand Up @@ -192,6 +204,7 @@ Fortune.prototype.resource = function(name, schema, options, schemaCallback) {
this._resources[name] = { schema: schema, modelOptions: modelOptions, hooksOptions: hooksOptions};
hooks.initGlobalHooks(_this._resources[name], _this.options);
//Register updates in queryParser. Should be called here to make sure that all resources are registered
_this._exposeResourceDefinitions();
querytree.use(this);

this.adapter.awaitConnection().then(function () {
Expand All @@ -201,8 +214,8 @@ Fortune.prototype.resource = function(name, schema, options, schemaCallback) {
_this._schema[name] = _.clone(schema);
try {
schema = _this.adapter.schema(name, schema, options, schemaCallback);
_this._exposeResourceDefinitions();
_this._route(name, _this.adapter.model(name, schema,modelOptions), _this._resources, inflect);
_this._resourceInitialized();
} catch(error) {
console.trace('There was an error loading the "' + name + '" resource. ' + error);
}
Expand Down Expand Up @@ -464,6 +477,14 @@ Fortune.prototype._route = route;
*/
Fortune.prototype._resource = '';

/**
* Ready state helpers
* @type {boolean}
* @api private
*/
Fortune.prototype._ready = false;
Fortune.prototype._resourcesStarted = 0;


// Default Cross-Origin Resource Sharing setup.
function allowCrossDomain (cors) {
Expand Down

0 comments on commit e9f8450

Please sign in to comment.