-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (47 loc) · 1.62 KB
/
index.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
import restify from 'restify';
import dotenv from 'dotenv';
import fs from 'fs';
import Postgres from './lib/mappers/postgres';
import Country from './lib/models/Country';
import City from './lib/models/City';
import GeoModel from './lib/models/Geo';
import CountryCity from './lib/models/Country-City';
import Countries from './lib/controllers/Countries';
import Cities from './lib/controllers/Cities';
import GeoController from './lib/controllers/Geo';
//load environment vars if .env file is there
try {
let envFile = fs.statSync('.env');
if (envFile) {
dotenv.load();
}
} catch (e) {
if (!process.env.DB_CONNECT) {
throw(new Error('No database connection defined'));
}
console.log('No .env file found, but environment vars defined.');
}
let server = restify.createServer();
server.use(restify.bodyParser());
server.use(restify.queryParser());
//allow trailing slashes on requests
server.pre(restify.pre.sanitizePath());
let Pg = new Postgres(process.env.DB_CONNECT);
let controllers = {
countries: new Countries(new Country(Pg)),
cities: new Cities(new City(Pg), new CountryCity(Pg)),
geo: new GeoController(new GeoModel(Pg))
};
server.get('/', (req, res) => {
res.send({
message: 'Welcome to the home of the EU Migrants Refuge API'
});
});
server.get('/countries/', controllers.countries.list);
server.get('/countries/:country', controllers.countries.get);
server.get('/countries/:country/cities', controllers.cities.list);
server.get('/cities/bounds', controllers.cities.bounds);
server.get('/geo/bounds', controllers.geo.bounds);
server.listen(3000, () => {
console.log('now listening at %s', server.url);
});