This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathapp.js
executable file
·103 lines (89 loc) · 3.33 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
103
/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License'); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
// Deployment tracking
const queryBuilder = require('./query-builder');
const WatsonDiscoverySetup = require('./lib/watson-discovery-setup');
const DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
const DEFAULT_COLLECTION_NAME = 'data-breaches';
var environment_id;
var collection_id;
var discovery;
const version_date = '2018-03-05';
const qs = { aggregation: `[${queryBuilder.aggregations.join(',')}]` };
// Credentials will be pulled in from VCAP_SERVICES or .env
discovery = new DiscoveryV1({
version: version_date,
qs: qs,
});
// pull in all json files to add to discovery collection
var discoveryDocs = [];
const fs = require('fs');
const path = require('path');
var arrayOfFiles = fs.readdirSync('./data/breaches/');
arrayOfFiles.forEach(function(file) {
discoveryDocs.push(path.join('./data/breaches/', file));
});
// setup Discovery by finding/creating environment and finding/creating collection
var discoverySetupParams = { default_name: DEFAULT_COLLECTION_NAME };
const discoverySetup = new WatsonDiscoverySetup(discovery);
discoverySetup.setupDiscovery(discoverySetupParams, (err, data) => {
if (err) {
discoverySetup.handleSetupError(err);
} else {
console.log('Discovery is ready!');
// now load data into discovery service collection
var collectionParams = data;
// set collection creds - at this point the collectionParams
// will point to the actual credentials, whether the user
// entered them in .env for an existing collection, or if
// we had to create them from scratch.
environment_id = collectionParams.environment_id;
collection_id = collectionParams.collection_id;
collectionParams.documents = discoveryDocs;
console.log('Begin loading ' + discoveryDocs.length + ' json files into discovery. Please be patient as this can take several minutes.');
discoverySetup.loadCollectionFiles(collectionParams);
}
});
// gather news collection info
const NewsDemoApp = new Promise((resolve) => {
// Bootstrap application settings
const express = require('express');
const app = express();
require('./config/express')(app);
app.get('/', (req, res) => {
res.render('index', {
BLUEMIX_ANALYTICS: process.env.BLUEMIX_ANALYTICS,
});
});
// setup query endpoint for news
app.post('/api/query', (req, res, next) => {
const params = Object.assign({}, queryBuilder.build(req.body, true), {
environment_id: environment_id,
collection_id: collection_id
});
discovery.query(params, (error, response) => {
if (error) {
next(error);
} else {
res.json(response);
}
});
});
// error-handler settings for all other routes
require('./config/error-handler')(app);
resolve(app);
});
module.exports = NewsDemoApp;