Skip to content

Commit 335fdd2

Browse files
josedahlquistronaldsg20
authored andcommitted
Update API to not expose explorer in prod
- Add logic in app boot to avoid exposing explorer and openapi in production - Update index.html to dynamically display information according to env
1 parent 912a394 commit 335fdd2

File tree

6 files changed

+47
-19
lines changed

6 files changed

+47
-19
lines changed

ENV_VARIABLES.md

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This table was created to guide and centralize the **environment variables** nec
3232
|SESSIONDB_INDEX |1 |'Redis index' |
3333
|MAX_AMOUNT_ALLOWED_IN_SATOSHI | |'Pegin Pegout max allowed in satoshis' |
3434
|METRICS_ENABLED |`true or false` |'enable trace log' |
35+
|NODE_ENV|`production or development`|'Indicates if the app should be built for a production environment or not'
3536

3637

3738
##Example for .env.local.test file
@@ -82,4 +83,6 @@ MAX_FEE_AMOUNT_ALLOWED=5000000
8283
8384
#Dust value (Satoshi)
8485
BURN_DUST_VALUE=2000
86+
87+
NODE_ENV=development
8588
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"type": "git",
4545
"url": ""
4646
},
47-
"author": "Mesi Rendon <[email protected]>",
47+
"author": "IOVLabs",
4848
"license": "",
4949
"files": [
5050
"README.md",

public/index.html

+24-9
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,37 @@
6767
}
6868
}
6969
</style>
70+
<script lang="javascript">
71+
let apiVersion = new XMLHttpRequest();
72+
apiVersion.onreadystatechange = function() {
73+
if (this.readyState == 4 && this.status == 200) {
74+
document.getElementById("version").innerHTML = JSON.parse(apiVersion.responseText).version;
75+
}
76+
};
77+
apiVersion.open("GET", "/api", true);
78+
apiVersion.send();
79+
80+
let shouldIShowAPI = new XMLHttpRequest();
81+
shouldIShowAPI.onreadystatechange = function() {
82+
if (this.readyState == 4 && this.status !== 404) {
83+
document.getElementById('api-section').setAttribute('style', 'display:block');
84+
}
85+
}
86+
shouldIShowAPI.open("GET", "/explorer", true);
87+
shouldIShowAPI.send();
88+
</script>
7089
</head>
7190

7291
<body>
7392
<div class="info">
7493
<h1>2wp-api</h1>
75-
<p><a href='../api'>Version</a></p>
94+
<p>Version: <b id="version">loading...</b></p>
7695

77-
<h3>OpenAPI spec: <a href="/openapi.json">/openapi.json</a></h3>
78-
<h3>API Explorer: <a href="/explorer">/explorer</a></h3>
96+
<div id="api-section" style="display:none">
97+
<h3>OpenAPI spec: <a href="/openapi.json">/openapi.json</a></h3>
98+
<h3>API Explorer: <a href="/explorer">/explorer</a></h3>
99+
</div>
79100
</div>
80-
81-
<footer class="power">
82-
<a href="https://loopback.io" target="_blank" rel="noopener">
83-
<img src="https://loopback.io/images/branding/powered-by-loopback/blue/powered-by-loopback-sm.png" alt=""/>
84-
</a>
85-
</footer>
86101
</body>
87102

88103
</html>

src/application.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {ServiceMixin} from '@loopback/service-proxy';
1010
import path from 'path';
1111
import {DependencyInjectionHandler} from './dependency-injection-handler';
1212
import {MySequence} from './sequence';
13+
import { ENVIRONMENT_PRODUCTION } from './constants';
1314

1415
export {ApplicationConfig};
1516

@@ -23,11 +24,14 @@ export class TwpapiApplication extends BootMixin(ServiceMixin(RepositoryMixin(Re
2324
// Set up default home page
2425
this.static('/', path.join(__dirname, '../public'));
2526

26-
// Customize @loopback/rest-explorer configuration here
27-
this.configure(RestExplorerBindings.COMPONENT).to({
28-
path: '/explorer',
29-
});
30-
this.component(RestExplorerComponent);
27+
// For production environments we will not load the explorer component
28+
if (process.env.NODE_ENV !== ENVIRONMENT_PRODUCTION) {
29+
// Customize @loopback/rest-explorer configuration here
30+
this.configure(RestExplorerBindings.COMPONENT).to({
31+
path: '/explorer',
32+
});
33+
this.component(RestExplorerComponent);
34+
}
3135

3236
this.projectRoot = __dirname;
3337
// Customize @loopback/boot Booter Conventions here

src/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ export const BITCOIN_MAX_SATOSHI_FEE = 5000000;
1313
// network
1414
export const NETWORK_TESTNET = 'testnet';
1515
export const NETWORK_MAINNET = 'mainnet';
16+
// environment
17+
export const ENVIRONMENT_PRODUCTION = 'production';
18+
export const ENVIRONMENT_DEVELOPMENT = 'development';

src/index.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {config} from 'dotenv';
22
import {configure, getLogger} from 'log4js';
33
import {ApplicationConfig, TwpapiApplication} from './application';
44
import {DaemonRunner} from './daemon-runner';
5+
import { ENVIRONMENT_PRODUCTION } from './constants';
56

67
export * from './application';
78

@@ -44,12 +45,10 @@ export async function main(options: ApplicationConfig = {}): Promise<void> {
4445
}
4546

4647
//catches ctrl+c event
47-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
48-
process.on('SIGINT', shutdown.bind(null));
48+
process.on('SIGINT', () => { shutdown().catch(logger.error); });
4949

5050
//catches uncaught exceptions
51-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
52-
process.on('uncaughtException', shutdown.bind(null));
51+
process.on('uncaughtException', () => { shutdown().catch(logger.error); });
5352

5453
const appMode = searchAppMode();
5554

@@ -83,7 +82,11 @@ if (require.main === module) {
8382
openApiSpec: {
8483
// useful when used with OpenAPI-to-GraphQL to locate your application
8584
setServersFromRequest: true,
85+
disabled: process.env.NODE_ENV === ENVIRONMENT_PRODUCTION
8686
},
87+
apiExplorer: {
88+
disabled: process.env.NODE_ENV === ENVIRONMENT_PRODUCTION
89+
}
8790
},
8891
};
8992
main(config).catch(err => {

0 commit comments

Comments
 (0)