-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
102 lines (84 loc) · 2.75 KB
/
app.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
//This is app.js
//This is the main server controller for the niche api
//uses express to route http requests
//uses swagger to validate request/response syntax
//v2.0
'use strict';
//load required modules
var SwaggerExpress = require('swagger-express-mw');
var app = require('express')();
var pg = require("pg")
var fs = require("fs")
var bodyParser = require('body-parser');
var compression = require('compression')
var cors = require('cors')
var promise = require('bluebird'); //promise library for pgp to run on
var pgp = require('pg-promise')( //postgres promise library makes it easier to execute user queries
{promiseLib: promise}
);
var shortid = require("shortid");
app.use(cors()) //allowa allow cross-server responses
app.use(bodyParser({limit: '500mb'})); //there are going to be some big requests
app.use(compression()) //gzip all responses
//set up logging
function logCall (req, res, next) { //we could go to a db table
console.log("-------" + new Date().toJSON() + "------")
console.log(req.method)
console.log(req.path)
var db = global.createConnection()
var sql = "INSERT INTO calllog VALUES(DEFAULT, DEFAULT, ${method}, ${path}, ${params}, ${ip})"
var vals = {method: req.method, path: req.path, params: req.params, ip: req.ip}
db.none(sql, vals)
.then(function(data){
next()
})
.catch(function(err){
console.log(err)
next()
})
}
app.use(logCall) //log every call
//read the configuration file with the connection details
global.conf = JSON.parse(fs.readFileSync('conf.js', 'utf8'))
global.createConnection =function(){
//connect to the ddatabase specified in the config file
//has to use VPN to SHC network
var cn = {
host: global.conf.host,
port: global.conf.port,
database: global.conf.db,
user: global.conf.username,
password: global.conf.password
};
var db = pgp(cn); //do the connection using pg-promise library
console.log("Created connection to database.")
return db
}
//get a list of the bands so we don't have to do it on every call
var db = global.createConnection()
var query2 = "SELECT * from bandindex ORDER BY bandvalue ASC;"
db.any(query2)
.then(function(bandindex){
global.years = []
global.bands = []
for (var band in bandindex){
global.years.push(bandindex[band]['bandvalue'])
global.bands.push(bandindex[band]['bandnumber'])
}
console.log("Got band info.")
})
.catch(function(err){
//errors getting band info
console.log(err)
})
module.exports = app; // for testing
var config = {
appRoot: __dirname // required config
};
SwaggerExpress.create(config, function(err, swaggerExpress) {
if (err) { throw err; }
// install middleware
swaggerExpress.register(app);
var port = process.env.PORT || 8080; //run
app.listen(port);
});