/
pinpoint.js
68 lines (57 loc) · 1.64 KB
/
pinpoint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* pinpoint.js: Top-level include for the Pinpoint module.
*
* (C) 2011 Charlie Robbins
* MIT LICENSE
*
*/
var http = require('http'),
winston = require('winston'),
database = require('./database'),
bookmark = require('./bookmark'),
service = require('./service');
/**
* Creates the server for the pinpoint web service
* @param {int} port: Port for the server to run on
* @param {connection} database: Connection to CouchDB
*/
exports.createServer = function (port, database) {
var resource = new bookmark.Bookmark(database),
router = service.createRouter(resource);
var server = http.createServer(function (request, response) {
var body = '';
winston.info('Incoming Request', { url: request.url });
request.on('data', function (chunk) {
body += chunk;
});
request.on('end', function () {
//
// Dispatch the request to the router
//
var emitter = router.handle(request, body, function (route) {
response.writeHead(route.status, route.headers);
response.end(route.body);
});
emitter.on('log', function (info) {
winston.info('Request completed', info);
});
})
});
if (port) {
server.listen(port);
}
return server;
};
/**
* Creates the server for the pinpoint web service
* @param {int} port: Port for the server to run on
* @param {connection} database: Connection to CouchDB
*/
exports.start = function (options, callback) {
database.setup(options, function (err, db) {
if (err) {
return callback(err);
}
callback(null, exports.createServer(options.port, db));
});
};