2013-11-26 02:26:00 +08:00
|
|
|
/*
|
2014-06-05 04:17:17 +08:00
|
|
|
* Cylon API
|
2013-11-26 02:26:00 +08:00
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 02:58:50 +08:00
|
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
2013-11-26 02:26:00 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
"use strict";
|
2013-11-26 02:26:00 +08:00
|
|
|
|
2014-06-05 02:37:15 +08:00
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path');
|
2014-04-04 05:32:56 +08:00
|
|
|
|
2014-06-05 02:06:02 +08:00
|
|
|
var express = require('express'),
|
|
|
|
bodyParser = require('body-parser');
|
2014-04-04 03:28:27 +08:00
|
|
|
|
2014-06-05 04:17:17 +08:00
|
|
|
var Logger = require('./logger');
|
2014-04-24 06:56:42 +08:00
|
|
|
|
2014-05-08 06:19:23 +08:00
|
|
|
var API = module.exports = function API(opts) {
|
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-04-04 03:28:27 +08:00
|
|
|
|
2014-06-05 02:37:15 +08:00
|
|
|
for (var d in this.defaults) {
|
|
|
|
this[d] = opts.hasOwnProperty(d) ? opts[d] : this.defaults[d];
|
|
|
|
}
|
|
|
|
|
2014-06-05 04:21:15 +08:00
|
|
|
this.createServer();
|
2014-05-08 06:19:23 +08:00
|
|
|
|
2014-06-16 23:44:52 +08:00
|
|
|
this.express.set('title', 'Cylon API Server');
|
2014-06-05 04:17:17 +08:00
|
|
|
|
2014-07-04 00:35:24 +08:00
|
|
|
this.express.use(this.setupAuth());
|
2014-06-16 23:44:52 +08:00
|
|
|
this.express.use(bodyParser());
|
|
|
|
this.express.use(express["static"](__dirname + "/../node_modules/robeaux/"));
|
2014-06-05 04:17:17 +08:00
|
|
|
|
2014-06-05 04:39:30 +08:00
|
|
|
// set CORS headers for API requests
|
2014-06-16 23:44:52 +08:00
|
|
|
this.express.use(function(req, res, next) {
|
2014-07-04 00:35:24 +08:00
|
|
|
res.set("Access-Control-Allow-Origin", this.CORS || "*");
|
2014-06-05 04:39:30 +08:00
|
|
|
res.set("Access-Control-Allow-Headers", "Content-Type");
|
|
|
|
res.set('Content-Type', 'application/json');
|
|
|
|
return next();
|
2014-07-04 00:35:24 +08:00
|
|
|
}.bind(this));
|
2014-06-05 04:39:30 +08:00
|
|
|
|
2014-06-05 06:54:23 +08:00
|
|
|
// extracts command params from request
|
2014-06-16 23:44:52 +08:00
|
|
|
this.express.use(function(req, res, next) {
|
2014-06-05 06:54:23 +08:00
|
|
|
var method = req.method.toLowerCase(),
|
|
|
|
container = {};
|
|
|
|
|
|
|
|
req.commandParams = [];
|
|
|
|
|
|
|
|
if (method === 'get' || Object.keys(req.query).length > 0) {
|
|
|
|
container = req.query;
|
|
|
|
} else if (typeof(req.body) === 'object') {
|
|
|
|
container = req.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var p in container) {
|
|
|
|
req.commandParams.push(container[p]);
|
2014-06-17 04:09:13 +08:00
|
|
|
}
|
2014-06-05 06:54:23 +08:00
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2014-06-05 04:39:30 +08:00
|
|
|
// load route definitions
|
2014-06-17 04:09:13 +08:00
|
|
|
this.express.use('/', require('./api/routes'));
|
2014-05-08 06:19:23 +08:00
|
|
|
};
|
|
|
|
|
2014-06-05 02:37:15 +08:00
|
|
|
API.prototype.defaults = {
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: '3000',
|
|
|
|
auth: false,
|
|
|
|
CORS: '',
|
|
|
|
ssl: {
|
2014-06-05 02:58:54 +08:00
|
|
|
key: path.normalize(__dirname + "/api/ssl/server.key"),
|
|
|
|
cert: path.normalize(__dirname + "/api/ssl/server.crt")
|
2014-06-05 02:37:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-05 04:21:15 +08:00
|
|
|
API.prototype.createServer = function createServer() {
|
2014-06-16 23:44:52 +08:00
|
|
|
this.express = express();
|
2014-06-05 04:21:15 +08:00
|
|
|
|
|
|
|
//configure ssl if requested
|
|
|
|
if (this.ssl && typeof(this.ssl) === 'object') {
|
|
|
|
var https = require('https');
|
|
|
|
|
2014-06-16 23:44:52 +08:00
|
|
|
this.server = https.createServer({
|
2014-06-05 04:21:15 +08:00
|
|
|
key: fs.readFileSync(this.ssl.key),
|
|
|
|
cert: fs.readFileSync(this.ssl.cert)
|
2014-06-16 23:44:52 +08:00
|
|
|
}, this.express);
|
2014-06-05 04:21:15 +08:00
|
|
|
} else {
|
2014-06-17 04:09:13 +08:00
|
|
|
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.");
|
2014-06-16 23:44:52 +08:00
|
|
|
this.server = this.express;
|
2014-06-05 04:21:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-05 04:17:17 +08:00
|
|
|
API.prototype.setupAuth = function setupAuth() {
|
|
|
|
var authfn = function auth(req, res, next) { next(); };
|
|
|
|
|
2014-06-17 02:42:33 +08:00
|
|
|
if (!!this.auth && typeof(this.auth) === 'object' && this.auth.type) {
|
2014-06-05 04:17:17 +08:00
|
|
|
var type = this.auth.type,
|
|
|
|
module = "./api/auth/" + type,
|
|
|
|
filename = path.normalize(__dirname + "/" + module + ".js"),
|
|
|
|
exists = fs.existsSync(filename);
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
authfn = require(filename)(this.auth);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return authfn;
|
|
|
|
};
|
|
|
|
|
2014-05-08 06:19:23 +08:00
|
|
|
API.prototype.listen = function() {
|
2014-06-16 23:44:52 +08:00
|
|
|
this.server.listen(this.port, this.host, null, function() {
|
2014-07-04 00:35:24 +08:00
|
|
|
var title = this.express.get('title');
|
|
|
|
var protocol = this.ssl ? "https" : "http";
|
2014-05-08 06:19:23 +08:00
|
|
|
|
|
|
|
Logger.info(title + " is now online.");
|
2014-07-04 00:35:24 +08:00
|
|
|
Logger.info("Listening at " + protocol + "://" + this.host + ":" + this.port);
|
|
|
|
}.bind(this));
|
2014-05-08 06:19:23 +08:00
|
|
|
};
|