Skip to content

Commit c284d18

Browse files
kaeymarefr
andcommitted
Service: Support configuring host and port thru config and environment variables (#40)
Support configuring service host and port thru config and environment variables. Co-authored-by: Marcus Efraimsson <[email protected]>
1 parent d60c0b6 commit c284d18

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

default.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"service": {
3+
"host": null,
34
"port": 8081,
45
"metrics": {
56
"enabled": false,

dev.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"service": {
3+
"host": "localhost",
34
"port": 8081,
45
"metrics": {
56
"enabled": true,

docs/remote_rendering_using_docker.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ The docker image are published at [Docker Hub](https://hub.docker.com/r/grafana/
88

99
You can override certain settings by using environment variables.
1010

11+
**HTTP host:**
12+
13+
Change the listening host of the HTTP server. Default is unset and will use the local host.
14+
15+
```bash
16+
export HTTP_HOST=localhost
17+
```
18+
19+
**HTTP port:**
20+
21+
Change the listening port of the HTTP server. Default is `8081`. Setting `0` will automatically assign a port not in use.
22+
23+
```bash
24+
export HTTP_PORT=0
25+
```
26+
1127
**Default timezone:**
1228

1329
Instruct headless Chrome to use a default timezone when not provided by Grafana, .e.g. when rendering panel image of alert. See [ICU’s metaZones.txt](https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1) for a list of supported timezone IDs.

src/app.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ function populateServiceConfigFromEnv(config: ServiceConfig, env: NodeJS.Process
8585
config.rendering.timezone = env['TZ'];
8686
}
8787

88+
if (env['HTTP_HOST']) {
89+
config.service.host = env['HTTP_HOST'];
90+
}
91+
92+
if (env['HTTP_PORT']) {
93+
config.service.port = parseInt(env['HTTP_PORT'] as string, 10);
94+
}
95+
8896
if (env['IGNORE_HTTPS_ERRORS']) {
8997
config.rendering.ignoresHttpsErrors = env['IGNORE_HTTPS_ERRORS'] === 'true';
9098
}

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface MetricsConfig {
2222

2323
export interface ServiceConfig {
2424
service: {
25+
host?: string;
2526
port: number;
2627
metrics: MetricsConfig;
2728
};
@@ -52,6 +53,7 @@ const defaultRenderingConfig: RenderingConfig = {
5253

5354
export const defaultServiceConfig: ServiceConfig = {
5455
service: {
56+
host: undefined,
5557
port: 8081,
5658
metrics: {
5759
enabled: false,

src/service/http-server.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as http from 'http';
2+
import * as net from 'net';
13
import express = require('express');
24
import { Logger } from '../logger';
35
import { Browser } from '../browser';
@@ -35,8 +37,17 @@ export class HttpServer {
3537
this.log.info(`Ignoring HTTPS errors`);
3638
}
3739

38-
this.app.listen(this.config.service.port);
39-
this.log.info(`HTTP Server started, listening on ${this.config.service.port}`);
40+
if (this.config.service.host) {
41+
const server = this.app.listen(this.config.service.port, this.config.service.host, () => {
42+
const info = server.address() as net.AddressInfo;
43+
this.log.info(`HTTP Server started, listening at http://${this.config.service.host}:${info.port}`);
44+
});
45+
} else {
46+
const server = this.app.listen(this.config.service.port, () => {
47+
const info = server.address() as net.AddressInfo;
48+
this.log.info(`HTTP Server started, listening at http://localhost:${info.port}`);
49+
});
50+
}
4051

4152
const browserInfo = new promClient.Gauge({
4253
name: 'grafana_image_renderer_browser_info',

0 commit comments

Comments
 (0)