-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
executable file
·73 lines (64 loc) · 2.63 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
#!/usr/bin/env node
/*
* Copyright (c) 2023 MarkLogic Corporation
*
* 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.
*/
// clean shutdown on `ctrl + c`
process.on('SIGINT', () => process.exit(0));
process.on('SIGTERM', () => process.exit(0));
// Following the example at https://koopjs.github.io/docs/usage/koop-core#koop-as-middleware of running Koop
// in an existing Express server. A user can then further customize the Express server as desired.
const express = require('express');
const Koop = require('@koopjs/koop-core');
const config = require('config');
const log = require('./src/koop/logger');
const dbClientManager = require('./src/koop/dbClientManager');
const fs = require('fs');
const koop = new Koop({});
// Determine the authorization strategy to use; see https://koopjs.github.io/docs/usage/authorization for more info.
if (config.auth && config.auth.plugin && config.auth.enabled !== false) {
log.info(`Enabling auth plugin: ${config.auth.plugin}`);
let auth = null;
if (config.auth.plugin === 'auth-marklogic-digest-basic') {
auth = require("./src/koop/authMarkLogic")(config.auth.options);
} else if (config.auth.plugin === 'auth-marklogic-basic-header') {
auth = require("./src/koop/authBasicHeader")();
} else if (config.auth.plugin) {
auth = require(config.auth.plugin)(config.auth.options);
}
koop.register(auth);
} else {
log.info(`No auth plugin specified, relying on configured MarkLogic credentials`);
dbClientManager.useStaticClient(true);
}
// Install the Marklogic Provider after installing an authorization plugin.
const provider = require('./src/koop');
const https = require("https");
koop.register(provider);
// Create and configure the Express app server.
const app = express();
app.use('/', koop.server);
let port = config.port || 8080;
if (config.ssl.enabled) {
const https = require('https');
const options = {
key: fs.readFileSync(config.ssl.key, 'utf8'),
cert: fs.readFileSync(config.ssl.cert, 'utf8')
};
port = config.ssl.port || 443;
https.createServer(options, app).listen(port);
} else {
app.listen(port);
}
log.info("Koop listening on " + port + "; press ctrl-C to exit");