forked from GoogleCloudPlatform/LabelCat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.js
115 lines (100 loc) · 3.39 KB
/
container.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
// Copyright 2015, Google, Inc. 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.
'use strict';
let dependable = require('dependable');
let config = require('./config.js');
let path = require('path');
let gcloud = require('gcloud');
let GitHubApi = require('github');
let google = require('googleapis');
let key;
try {
if (process.env.NODE_ENV === 'test') {
key = {
private_key_id: '',
private_key: '',
client_email: '',
client_id: '',
type: 'service_account'
};
} else {
key = require(config.gcloud.keyFile);
}
} catch (err) {
console.error(`Could not read key file! Did you download one from https://console.developers.google.com/project/${config.gcloud.projectId}/apiui/credential ?`);
throw err;
}
let container = dependable.container();
container.register('Promise', function () {
return require('bluebird');
});
container.register('request', function (Promise) {
return Promise.promisify(require('request'));
});
container.register('config', config);
// Interface for communicating with the GCP DataStore.
container.register('dataset', function (Promise) {
return Promise.promisifyAll(gcloud.datastore.dataset({
projectId: config.gcloud.projectId,
keyFilename: config.gcloud.keyFile
}));
});
// Interface for communicating with the GCP PubSub.
container.register('pubsub', function (Promise) {
return Promise.promisifyAll(gcloud.pubsub({
projectId: config.gcloud.projectId,
keyFilename: config.gcloud.keyFile
}));
});
// Interface for communicating with the GCP Prediction API.
container.register('trainedModelsApi', function (Promise) {
let trainedmodels = google.prediction('v1.6').trainedmodels;
Promise.promisifyAll(trainedmodels);
return trainedmodels;
});
// Interface for communicating with the GitHub API.
container.register('githubApi', function () {
return new GitHubApi({
version: '3.0.0',
protocol: 'https',
headers: {
'User-Agent': 'LabelCat'
}
});
});
// Authorization tool for the Google APIs.
container.register('jwtClient', function (Promise) {
return Promise.promisifyAll(new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/prediction'],
null
));
});
// Interface for communicating with the Predictions API.
container.register('prediction', function () {
return google.prediction('v1.6');
})
// Register all of the files in these folders with the container, using each
// file's name as its registered name in the container.
container.load(path.join(`${__dirname}/app`, 'controllers'));
container.load(path.join(`${__dirname}/app`, 'lib'));
container.load(path.join(`${__dirname}/app`, 'middleware'));
container.load(path.join(`${__dirname}/app`, 'models'));
// Register the container with itself so it can be depended upon.
container.register('container', function () {
return container;
});
module.exports = container;