Skip to content

Commit

Permalink
Merge pull request #13 from LN-Zap/baseurl
Browse files Browse the repository at this point in the history
Make server base url configurable
  • Loading branch information
mrfelton authored Jan 6, 2024
2 parents 2ae2d53 + cbcfa3a commit f9b8302
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,25 @@ This project uses the [`config`](https://www.npmjs.com/package/config) package f

Here are the available configuration options:

- `server.port`: The port on which the server runs. Default is `3000`.
- `esplora.baseUrl`: The base URL of the Esplora API instance to connect to. Default is `https://blockstream.info`.
- `mempool.baseUrl`: The base URL of the Mempool instance to connect to. Default is `https://mempool.space`.
- `mempool.depth`: The number of blocks to use for mempool-based fee estimates. Default is `6`. Valid options are `1`, `3`, and `6`.
- `settings.feeMultiplier`: The multiplier to apply to the fee estimates. Default is `1` (a conservative approach to ensure that the fee estimates are always slightly higher than the raw estimates).
- `cache.stdTTL`: The standard time to live in seconds for every generated cache element. Default is `15`.
- `cache.checkperiod`: The period in seconds, used for the automatic delete check interval. Default is `20`.
- `server.port`: The port on which the server runs. Default is `3000`
- `server.baseUrl`: The base url port on which the server is accessible. Default is `http://localhost:3000`
- `esplora.baseUrl`: The base URL of the Esplora API instance to connect to. Default is `https://blockstream.info`
- `mempool.baseUrl`: The base URL of the Mempool instance to connect to. Default is `https://mempool.space`
- `mempool.depth`: The number of blocks to use for mempool-based fee estimates. Default is `6`. Valid options are `1`, `3`, and `6`
- `settings.feeMultiplier`: The multiplier to apply to the fee estimates. Default is `1` (a conservative approach to ensure that the fee estimates are always slightly higher than the raw estimates)
- `cache.stdTTL`: The standard time to live in seconds for every generated cache element. Default is `15`
- `cache.checkperiod`: The period in seconds, used for the automatic delete check interval. Default is `20`

You can override these options by setting the corresponding environment variables:

- `PORT`: Overrides `server.port`.
- `ESPLORA_BASE_URL`: Overrides `esplora.baseUrl`.
- `MEMPOOL_BASE_URL`: Overrides `mempool.baseUrl`.
- `MEMPOOL_DEPTH`: Overrides `mempool.depth`.
- `FEE_MULTIPLIER`: Overrides `settings.feeMultiplier`.
- `CACHE_STDTTL`: Overrides `cache.stdTTL`.
- `CACHE_CHECKPERIOD`: Overrides `cache.checkperiod`.
- `PORT`: Overrides `server.port`
- `BASE_URL`: Overrides `server.baseUrl`
- `ESPLORA_BASE_URL`: Overrides `esplora.baseUrl`
- `MEMPOOL_BASE_URL`: Overrides `mempool.baseUrl`
- `MEMPOOL_DEPTH`: Overrides `mempool.depth`
- `FEE_MULTIPLIER`: Overrides `settings.feeMultiplier`
- `CACHE_STDTTL`: Overrides `cache.stdTTL`
- `CACHE_CHECKPERIOD`: Overrides `cache.checkperiod`

For example, to run the server on port 4000 and connect to a local Mempool instance, you can start the server like this:

Expand Down
1 change: 1 addition & 0 deletions config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"server": {
"baseUrl": "BASE_URL",
"port": "PORT"
},
"settings": {
Expand Down
1 change: 1 addition & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"server": {
"baseUrl": "http://localhost:3000",
"port": 3000
},
"settings": {
Expand Down
6 changes: 4 additions & 2 deletions src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import NodeCache from 'node-cache';

// Get application configuration values from the config package.
const port = config.get<number>('server.port');
const baseUrl = config.get<number>('server.baseUrl');
const esploraBaseUrl = config.get<string>('esplora.baseUrl');
const mempoolBaseUrl = config.get<string>('mempool.baseUrl');
const mempoolDepth = config.get<number>('mempool.depth');
Expand All @@ -22,6 +23,7 @@ const TIMEOUT: number = 3000;
// Log the configuration values.
console.info('---');
console.info(`Using port: ${port}`);
console.info(`Using base URL: ${baseUrl}`);
console.info(`Using Esplora host: ${esploraBaseUrl}`);
console.info(`Using Mempool base URL: ${mempoolBaseUrl}`);
console.info(`Using Mempool base URL: ${mempoolDepth}`);
Expand Down Expand Up @@ -91,7 +93,7 @@ async function fetchAndHandle(url: string): Promise<string | object | null> {

// Initialize the Express app.
const app = new Hono();
console.info(`Fee Estimates available at http://localhost:${port}/v1/fee-estimates`);
console.info(`Fee Estimates available at ${baseUrl}/v1/fee-estimates`);

// Add a health/ready endpoint.
app.get('/health/ready', async (c) => {
Expand Down Expand Up @@ -238,7 +240,7 @@ const Content = (props: { siteData: SiteData; data: object }) => (
</div>

<pre>
<span class="blue">curl</span> -L -X GET <span class="green">'http://localhost:{port}/v1/fee-estimates'</span> -H <span class="green">'Accept: application/json'</span>
<span class="blue">curl</span> -L -X GET <span class="green">'{baseUrl}/v1/fee-estimates'</span> -H <span class="green">'Accept: application/json'</span>
</pre>

<pre>
Expand Down

0 comments on commit f9b8302

Please sign in to comment.