-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
executable file
·70 lines (58 loc) · 1.91 KB
/
api.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
#!/usr/bin/env node
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var http = require('http')
var server = http.createServer(app)
var port = 3001
var pg = require('pg')
var pgConfig = require('./config.json')
app.disable('x-powered-by');
app.use(bodyParser())
console.log('HTTP: Setting up Router..')
// Main page, probably serve this from a HTML file in the future
app.get('/', function(req, res) {
res.type('text/html')
res.send('Welcome to UKHASnet-APIv3')
})
// Website-specific API
require('./api-mapNodes')(app,pg,pgConfig)
require('./api-nodeGraphData')(app,pg,pgConfig)
// Node Info/Data API
require('./api-nodeInfo')(app,pg,pgConfig)
require('./api-nodePackets')(app,pg,pgConfig)
require('./api-nodeData')(app,pg,pgConfig)
require('./api-lastData')(app,pg,pgConfig)
require('./api-nodeLocations')(app,pg,pgConfig)
require('./api-nodeTypes')(app,pg,pgConfig)
// Node Configuration API
require('./api-addNode')(app,pg,pgConfig)
require('./api-editNode')(app,pg,pgConfig)
require('./api-nodeNameAvailable')(app,pg,pgConfig)
// 404 handler, probably serve this from a HTML file in the future
app.use(function(req, res){
res.type('text/html')
res.send(404, '<h2>API URL not found</h2>API Documentation at <a href="http://www.ukhas.net/wiki/api">http://www.ukhas.net/wiki/api</a>');
});
console.log('HTTP: Router Initialised')
console.log('DB: Testing Connection..')
pg.connect(pgConfig, function(err, client, done) {
if(err) {
console.log('DB: Connection Error: ', err)
return
}
client.query('SELECT 1;', function(err, result) {
done()
if(err) {
console.log('DB: Query Error: ', err)
return
} else {
console.log('DB: Connection OK')
start_api()
}
})
})
function start_api() {
server.listen(port)
console.log('ukhas.net API v0.3 now running on port '+port)
}