support for configurable http/https for cds-ui server
[ccsdk/cds.git] / cds-ui / server / index.js
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 const application = require('./dist');
23 const fs = require('fs')
24
25 module.exports = application;
26
27 if (require.main === module) {
28
29   try {
30     var p12File = process.env.KEYSTORE || "aaf.p12"
31     var passwdFile = process.env.PASSPHRASE || ".enc"
32
33     var data = fs.readFileSync(passwdFile, 'utf8')
34     var elements = data.match(/cadi_keystore_password_p12=(.*)\n/)
35     var passphrase = elements[1]
36     var p12 = fs.readFileSync(p12File)
37   } catch(e){
38     console.error('Reading keystore error :', e)
39     process.exit(11)
40   }
41
42   // Run the application
43   const config = {
44     rest: {
45       protocol: process.env.PROTOCOL || 'https',
46       pfx: p12,
47       passphrase: passphrase,
48       port: +process.env.PORT || 3000,
49       host: process.env.HOST || 'localhost',
50       openApiSpec: {
51         // useful when used with OASGraph to locate your application
52         setServersFromRequest: true,
53       },
54     },
55   };
56   application.main(config).catch(err => {
57     console.error('Cannot start the application.', err);
58     process.exit(1);
59   });
60 }