-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
138 lines (123 loc) · 3.68 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var express = require("express");
var app = express();
var cfenv = require("cfenv");
var bodyParser = require('body-parser');
var requestSrv = require('request');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
var annotatorService;
// user defined environment variable to test different ACD registered services
var _tmpSrvName = process.env.SERVICE_NAME;
var servicename = _tmpSrvName ? _tmpSrvName :'acd-default';
/**
* Endpoint to get a JSON array of all the visitors in the database
* REST API example:
* <code>
* GET http://localhost:3000/api/status
* </code>
*
* Response:
* {
* "version": "string",
* "upTime": "string",
* "serviceState": "OK",
* "stateDetails": "string",
* "hostName": "string",
* "requestCount": 0,
* "maxMemoryMb": 0,
* "commitedMemoryMb": 0,
* "inUseMemoryMb": 0,
* "availableProcessors": 0
* }
* @return A status object
*/
app.get("/api/status", function (request, response){
console.log("calling /api/status");
var status = {
"version": "",
"upTime": "",
"serviceState": "",
"stateDetails": "",
"hostName": "",
"requestCount": 0,
"maxMemoryMb": 0,
"commitedMemoryMb": 0,
"inUseMemoryMb": 0,
"availableProcessors": 0
};
if(!annotatorService){
console.log("No annotator service information.")
response.json(status);
return;
}
var url = annotatorService.credentials.url +'/status';
console.log("calling: " + url);
requestSrv(url, function(error, res, body){
if (!error && res.statusCode == 200){
console.log(body);
response.send(body);
}else{
console.log("Error while getting status");
console.log(res.statusCode);
console.log(body);
response.send(status);
}
}).auth(annotatorService.credentials.username, annotatorService.credentials.password);
});
/**
*
* <code>
* GET http://localhost:3000/api/analytics
* </code>
* {
* "annotatorNames": [
* "string"
* ]
* }
* @return a list of analytic ids that can be queried for capability metadata or sent data for processing
*/
app.get("/api/analytics", function(request, response){
console.log("calling /api/analytics");
var analytics = [];
if(!annotatorService){
console.log("No annotator service information.")
response.json(analytics);
return;
}
var url = annotatorService.credentials.url +'/analytics?version=2017-05-08';
console.log("calling: " + url);
requestSrv(url, function(error, res, body){
if (!error && res.statusCode == 200){
console.log(body);
response.send(body);
}else{
console.log("Error while getting status");
console.log(res.statusCode);
console.log(body);
response.send(analytics);
}
}).auth(annotatorService.credentials.username, annotatorService.credentials.password);
});
// load local VCAP configuration and service credentials
var vcapLocal;
try {
vcapLocal = require('./vcap-local.json');
console.log("Loaded local VCAP", vcapLocal);
} catch (e) { }
const appEnvOpts = vcapLocal ? { vcap: vcapLocal} : {}
const appEnv = cfenv.getAppEnv(appEnvOpts);
if (appEnv.services[servicename]){
var acd = appEnv.services[servicename][0];
annotatorService = acd;
}else{
console.log("No VCAP_SERVICES were found for: " + servicename + ". Check your user defined environment variable SERVICE_NAME.")
}
//serve static file (index.html, images, css)
app.use(express.static(__dirname + '/views'));
var port = process.env.PORT || 3000
app.listen(port, function() {
console.log("To view your app, open this link in your browser: http://localhost:" + port);
console.log("Using acd service named: " + servicename);
});