Allow api origins to have paths.

Add basic logging wrapper for winston to start using.
Connect express.logger to logger module.
Use example.org instead of origin.org juuuuuust to be safe.
This commit is contained in:
Mike Larsson 2013-05-16 15:46:44 -04:00
Родитель 23b58c785a
Коммит 2615f39d01
5 изменённых файлов: 37 добавлений и 10 удалений

14
api.js
Просмотреть файл

@ -1,7 +1,7 @@
var request = require('request');
var errors = require('./lib/errors');
var _ = require('underscore');
var logger = require('winston');
var logger = require('./logger');
var url = require('url');
@ -76,6 +76,14 @@ function apiMethod (method) {
}
}
function getFullUrl(origin, path) {
path = path || '';
path = path.replace(/^\/?/, '');
return url.format(_.extend(
origin,
{ pathname: origin.path + path }));
}
// Load data from remote endpoint
function remote (method, path, callback) {
@ -84,7 +92,7 @@ function remote (method, path, callback) {
// TODO - need to add ability to pass data through
// TODO - might want to cache this at some point
var endpointUrl = url.format(_.extend(this.origin, { pathname: path }));
var endpointUrl = getFullUrl(this.origin, path);
request[method](endpointUrl, function(err, response, body) {
logger.log('info', 'API request: "%s %s" %s',
@ -157,8 +165,6 @@ module.exports = function Api(origin, config) {
config = config || {};
origin = url.parse(origin);
if (origin.pathname !== '/')
throw new Error('Api origin url must not contain a path');
this.origin = origin;
_.each(['get', 'post', 'put', 'patch', 'head', 'del'], function(method) {

7
app.js
Просмотреть файл

@ -4,6 +4,7 @@ const express = require('express');
const nunjucks = require('nunjucks');
const middleware = require('./middleware');
const helpers = require('./helpers');
const logger = require('./logger');
const app = express();
const env = new nunjucks.Environment(new nunjucks.FileSystemLoader(path.join(__dirname, 'views')), {autoescape: true});
@ -11,7 +12,11 @@ env.express(app);
app.use(express.cookieParser());
app.use(middleware.session());
app.use(express.logger());
app.use(express.logger({stream:{
write: function(msg, encoding) {
logger.info(msg.trim());
}
}}));
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.csrf());

4
logger.js Normal file
Просмотреть файл

@ -0,0 +1,4 @@
var winston = require('winston');
/* This space reserved for future config/transports */
/* e.g. winston.add(winston.transports.File, { filename: 'somelog.log' }); */
module.exports = winston;

Просмотреть файл

@ -3,6 +3,8 @@ const errors = require('./lib/errors');
const _ = require('underscore');
const ENDPOINT = process.env['CSOL_OPENBADGER_URL'];
if (!ENDPOINT)
throw new Error('Must specify CSOL_OPENBADGER_URL in the environment');
// Make sure badges returned from remote API
// contain all the information we need

Просмотреть файл

@ -41,15 +41,12 @@ function fakeRequest(func, config, callback) {
callback(req, res, next);
}
const ORIGIN = 'http://origin.org';
const ORIGIN = 'http://example.org';
test('Api()', function(t) {
t.test('requires pathless origin', function(t) {
t.test('requires origin', function(t) {
t.throws(function(){ new Api(); }, 'throws without');
t.throws(function(){ new Api('http://foo.org/bar'); },
{ name: 'Error', message: 'Api origin url must not contain a path' },
'throws with path');
t.end();
});
@ -336,6 +333,19 @@ test('api.get', function(t) {
t.end();
});
t.test('leading slashes don\'t indicate absolute path', function(t) {
const WITH_PATH = 'http://example.org/base/';
var api = new Api(WITH_PATH);
var requestMock = sinon.mock(request);
var get = requestMock.expects('get').twice();
api.get('/foo/bar', function(){});
api.get('foo/bar', function(){});
t.ok(get.alwaysCalledWith(sinon.match(WITH_PATH + 'foo/bar')), 'with origin/endpoint');
requestMock.restore();
t.end();
});
t.test('calls against different origins don\'t collide', function(t) {
const ANOTHER = 'http://another.org';
var api2 = new Api(ANOTHER);