forked from Open-EO/openeo-earthengine-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
203 lines (174 loc) · 5.96 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const CapabilitiesAPI = require('./openeo/capabilities');
const CollectionsAPI = require('./openeo/collections');
const FilesAPI = require('./openeo/files');
const JobsAPI = require('./openeo/jobs');
const ProcessesAPI = require('./openeo/processes');
const ProcessGraphsAPI = require('./openeo/processGraphs');
const ServicesAPI = require('./openeo/services');
const SubscriptionsAPI = require('./openeo/subscriptions');
const UsersAPI = require('./openeo/users');
const Config = require('./openeo/config');
const ProcessRegistry = require('./openeo/processRegistry');
const Utils = require('./openeo/utils');
const fse = require('fs-extra');
const restify = require('restify');
global.ee = require('@google/earthengine');
class Server {
constructor() {
console.log('Initializing openEO Google Earth Engine driver...');
this.http_server = null;
this.https_server = null;
this.config = new Config();
this.processRegistry = new ProcessRegistry();
for(var i in this.config.processes) {
this.processRegistry.add(this.config.processes[i]);
}
this.serverOptions = {
handleUpgrades: true,
ignoreTrailingSlash: true
};
this.corsExposeHeaders = 'OpenEO-Identifier, OpenEO-Costs';
this.api = {};
this.api.capabilities = new CapabilitiesAPI(this.config);
this.api.collections = new CollectionsAPI();
this.api.processes = new ProcessesAPI();
this.api.files = new FilesAPI();
this.api.jobs = new JobsAPI();
this.api.services = new ServicesAPI() ;
this.api.subscriptions = new SubscriptionsAPI();
this.api.users = new UsersAPI();
this.api.processGraphs = new ProcessGraphsAPI();
const privateKey = fse.readJsonSync(this.config.serviceAccountCredentialsFile);
ee.data.authenticateViaPrivateKey(privateKey,
() => {
console.log("GEE Authentication succeeded.");
ee.initialize();
this.startServer();
},
(error) => {
console.log("ERROR: GEE Authentication failed: " + error);
process.exit(1);
}
);
}
addEndpoint(method, path, callback) {
if (!Array.isArray(path)) {
path = [path, path.replace(/\{([\w]+)\}/g, ":$1")];
}
this.api.capabilities.addEndpoint(method, path[0]);
if (method === 'delete') {
method = 'del';
}
this.http_server[method](this.config.apiPath + path[1], callback);
if (this.isHttpsEnabled()) {
this.https_server[method](this.config.apiPath + path[1], callback);
}
}
isHttpsEnabled() {
return (this.config.ssl && this.config.ssl.port > 0 && typeof this.config.ssl.key === 'string' && typeof this.config.ssl.certificate === 'string') ? true : false;
}
initHttpServer() {
this.http_server = restify.createServer(this.serverOptions);
this.initServer(this.http_server);
}
initHttpsServer() {
if (this.isHttpsEnabled()) {
var https_options = Object.assign({}, this.serverOptions, {
key: fse.readFileSync(this.config.ssl.key),
certificate: fse.readFileSync(this.config.ssl.certificate)
});
this.https_server = restify.createServer(https_options);
this.initServer(this.https_server);
}
}
initServer(server) {
server.pre(this.preflight.bind(this));
server.use(this.populateGlobals.bind(this));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.use(restify.plugins.authorizationParser());
server.use(this.injectCorsHeader.bind(this));
server.use(this.api.users.checkRequestAuthToken.bind(this.api.users));
}
createSubscriptions(topics) {
for(let i in topics) {
this.api.subscriptions.registerTopic(topics[i]);
}
}
populateGlobals(req, res, next) {
req.config = this.config;
req.processRegistry = this.processRegistry;
req.user = this.api.users.emptyUser();
req.api = this.api;
req.downloadRegion = null;
return next();
}
startServer() {
this.initHttpServer();
this.initHttpsServer();
let p = [];
for(var i in this.api) {
p.push(this.api[i].beforeServerStart(this));
}
Promise.all(p)
.then(() => this.startServerHttp())
.then(() => this.startServerHttps())
.catch(e => {
console.log('Server not started due to the following error: ');
console.log(e);
process.exit(1);
});
}
startServerHttp() {
return new Promise((resolve, reject) => {
const port = process.env.PORT || this.config.port;
this.http_server.listen(port, () => {
var exposePortStr = this.config.exposePort != 80 ? ":" + this.config.exposePort : "";
Utils.serverUrl = "http://" + this.config.hostname + exposePortStr + this.config.apiPath;
console.log('HTTP-Server listening at %s', Utils.getServerUrl());
resolve();
});
});
}
startServerHttps() {
return new Promise((resolve, reject) => {
if (this.isHttpsEnabled()) {
var sslport = process.env.SSL_PORT || this.config.ssl.port;
this.https_server.listen(sslport, () => {
var exposePortStr = this.config.ssl.exposePort != 443 ? ":" + this.config.ssl.exposePort : "";
Utils.serverUrl = "https://" + this.config.hostname + exposePortStr + this.config.apiPath;
console.log('HTTPS-Server listening at %s', Utils.getServerUrl());
resolve();
});
}
else {
console.log('HTTPS not enabled.');
resolve();
}
});
}
injectCorsHeader(req, res, next) {
if (!req.headers['origin']) {
return next();
}
res.setHeader('access-control-allow-origin', req.headers['origin']);
res.setHeader('access-control-allow-credentials', 'true');
res.setHeader('access-control-expose-headers', this.corsExposeHeaders);
return next();
}
preflight(req, res, next) {
if (req.method !== 'OPTIONS') {
return next();
}
res.once('header', () => {
res.header('access-control-allow-origin', req.headers['origin'])
res.header('access-control-allow-credentials', 'true')
res.header('access-control-expose-headers', this.corsExposeHeaders);
res.header('access-control-allow-methods', 'OPTIONS, GET, POST, PATCH, PUT, DELETE');
res.header('access-control-allow-headers', 'Authorization, Content-Type');
});
res.send(204);
// Don't call next, this ends execution, nothing more to send.
}
};
global.server = new Server();