-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (91 loc) · 2.57 KB
/
index.js
File metadata and controls
108 lines (91 loc) · 2.57 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
var path = require('path')
, restify = require('restify')
, geo = require('maxmind-db-reader')
, debug = require('diagnostics')('geoip2-api');
/**
* GeoIp2API constructor.
*
* @param {Server} server HTTP server instance.
* @param {Object} options Configuration.
* @Constructor
* @api public
*/
var GeoIp2API = module.exports = function GeoIp2API(server, options) {
if (!(this instanceof GeoIp2API)) return new GeoIp2API(options);
this.options = options || {};
//
// Open and read the provided database.
//
this.db = geo.openSync(
this.options.db || process.env.DB || path.join(__dirname, 'db.mmdb')
);
//
// Attach the server to the instance.
//
this.server = server;
};
/**
* Attach set of routes to the server
*
* @param {Array} routes Collection of functions.
* @api public
*/
GeoIp2API.prototype.routing = function routing(routes) {
var api = this;
routes.forEach(function addRoutes(router) {
router(api);
});
};
/**
* Get data by IP.
*
* @param {String} ip IP to query.
* @param {Function} done Completion callback.
* @returns {GeoIp2API}
* @api public
*/
GeoIp2API.prototype.get = function get(ip, done) {
this.db.getGeoData(ip, function fetched(error, data) {
if (error) return done(error);
debug('Found geographical data for ip %s', ip);
return done(null, data);
});
return this;
};
/**
* Create a new restify powered GeoIp2API server.
*
* @param {Number} port Port to listen on.
* @param {Object} options Configuration.
* @returns {GeoIp2API}
* @api public
*/
GeoIp2API.createServer = function createServer(port, options) {
var api;
options = 'object' === typeof port ? port : options || {};
if ('number' === typeof port) options.port = port;
options.name = options.name || 'GeoIP2';
options.version = options.version || require('./package').version;
options.port = options.port || process.env.PORT || 8082;
//
// Create a Restify server and add default Restify middleware.
//
api = new GeoIp2API(restify.createServer(options), options);
api.server.use(restify.acceptParser(api.server.acceptable));
api.server.use(restify.queryParser());
//
// Initialize the server with the provided routes.
//
api.routing(options.routes || [require('./server')]);
//
// Listen by defauly unless specifically forbidden through options.
//
if (options.listen !== false) {
api.server.listen(options.port, function listening(error) {
if (error) return console.error(error);
debug('%s listening at %s', api.server.name, api.server.url);
});
}
return api;
};