This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
107 lines (95 loc) · 2.66 KB
/
server.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
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
'use strict';
const fs = require('fs');
const Hapi = require('hapi');
const squel = require('squel');
const pluralize = require('pluralize');
let resources = {};
const normalizedPath = require('path').join(__dirname, 'resources');
fs.readdirSync(normalizedPath).forEach(function(file) {
const resourceName = file.split('.')[0];
resources[resourceName] = require('./resources/' + file);
});
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
let db = [];
for (const dsn of config.dsn) {
db[dsn.name] = require('odbc')();
db[dsn.name].open(dsn.conn, function(err) {
if (err) return console.log(err);
});
}
const getHandler = function(resource, request, reply) {
const dsn = request.params.dsn;
const table = resource.tableName;
let sql = squel.select().from(table);
// let bindingarameters = [];
if (request.params.id) {
// we were sent an ID to get one record
if (resource.id) {
sql = sql.where(resource.id + ' = ?', request.params.id);
} else {
return reply(JSON.stringify({error: 'No ID field defined'}))
.type('application/json')
.code(500);
}
} else if (request.query === {}) {
// no query parameters
} else {
for (let key in request.query) {
sql = sql.where(key + ' = ?', request.query[key]);
}
}
const sqlString = sql.toString();
console.log('Sending SQL:', sqlString);
db[dsn].query(sqlString, function(err, data) {
console.log('Rows:', data.length);
let statusCode = 200;
if (err) {
statusCode = 500;
console.error(err);
return reply(JSON.stringify(err))
.type('application/json')
.code(statusCode);
} else {
if (data.length === 0) {
statusCode = 404;
}
const encoded = resource.encode(pluralize, data);
return reply(JSON.stringify(encoded))
.type('application/json')
.code(statusCode);
}
});
};
const getSingleHandler = function(request, reply) {
const resourceName = request.params.resource;
const resource = resources[resourceName];
return getHandler(resource, request, reply);
};
const getMultiHandler = function(request, reply) {
const resourceName = pluralize(request.params.resource, 1);
const resource = resources[resourceName];
return getHandler(resource, request, reply);
};
const server = new Hapi.Server();
server.connection({
port: 6322,
routes: {
cors: true
}
});
server.route({
method: 'GET',
path: '/{dsn}/{resource}/{id}',
handler: getSingleHandler
});
server.route({
method: 'GET',
path: '/{dsn}/{resource}',
handler: getMultiHandler
});
server.start(function(err) {
if (err) {
throw err;
}
console.log('Server running');
});