From c284d18935205e0b73ea87af5ddf27bd5763961c Mon Sep 17 00:00:00 2001 From: Konstantin Kulikov Date: Sun, 19 Jan 2020 19:52:24 +0300 Subject: [PATCH] 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 --- default.json | 1 + dev.json | 1 + docs/remote_rendering_using_docker.md | 16 ++++++++++++++++ src/app.ts | 8 ++++++++ src/config.ts | 2 ++ src/service/http-server.ts | 15 +++++++++++++-- 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/default.json b/default.json index 17171668..cb48c1b0 100644 --- a/default.json +++ b/default.json @@ -1,5 +1,6 @@ { "service": { + "host": null, "port": 8081, "metrics": { "enabled": false, diff --git a/dev.json b/dev.json index 359e23e9..41e7f7a8 100644 --- a/dev.json +++ b/dev.json @@ -1,5 +1,6 @@ { "service": { + "host": "localhost", "port": 8081, "metrics": { "enabled": true, diff --git a/docs/remote_rendering_using_docker.md b/docs/remote_rendering_using_docker.md index ee142763..aea4c383 100644 --- a/docs/remote_rendering_using_docker.md +++ b/docs/remote_rendering_using_docker.md @@ -8,6 +8,22 @@ The docker image are published at [Docker Hub](https://hub.docker.com/r/grafana/ You can override certain settings by using environment variables. +**HTTP host:** + +Change the listening host of the HTTP server. Default is unset and will use the local host. + +```bash +export HTTP_HOST=localhost +``` + +**HTTP port:** + +Change the listening port of the HTTP server. Default is `8081`. Setting `0` will automatically assign a port not in use. + +```bash +export HTTP_PORT=0 +``` + **Default timezone:** 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. diff --git a/src/app.ts b/src/app.ts index 6e6b9b5b..f74d95af 100644 --- a/src/app.ts +++ b/src/app.ts @@ -85,6 +85,14 @@ function populateServiceConfigFromEnv(config: ServiceConfig, env: NodeJS.Process config.rendering.timezone = env['TZ']; } + if (env['HTTP_HOST']) { + config.service.host = env['HTTP_HOST']; + } + + if (env['HTTP_PORT']) { + config.service.port = parseInt(env['HTTP_PORT'] as string, 10); + } + if (env['IGNORE_HTTPS_ERRORS']) { config.rendering.ignoresHttpsErrors = env['IGNORE_HTTPS_ERRORS'] === 'true'; } diff --git a/src/config.ts b/src/config.ts index 1e4edb99..be555259 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,6 +22,7 @@ export interface MetricsConfig { export interface ServiceConfig { service: { + host?: string; port: number; metrics: MetricsConfig; }; @@ -52,6 +53,7 @@ const defaultRenderingConfig: RenderingConfig = { export const defaultServiceConfig: ServiceConfig = { service: { + host: undefined, port: 8081, metrics: { enabled: false, diff --git a/src/service/http-server.ts b/src/service/http-server.ts index 81571064..30043f29 100644 --- a/src/service/http-server.ts +++ b/src/service/http-server.ts @@ -1,3 +1,5 @@ +import * as http from 'http'; +import * as net from 'net'; import express = require('express'); import { Logger } from '../logger'; import { Browser } from '../browser'; @@ -35,8 +37,17 @@ export class HttpServer { this.log.info(`Ignoring HTTPS errors`); } - this.app.listen(this.config.service.port); - this.log.info(`HTTP Server started, listening on ${this.config.service.port}`); + if (this.config.service.host) { + const server = this.app.listen(this.config.service.port, this.config.service.host, () => { + const info = server.address() as net.AddressInfo; + this.log.info(`HTTP Server started, listening at http://${this.config.service.host}:${info.port}`); + }); + } else { + const server = this.app.listen(this.config.service.port, () => { + const info = server.address() as net.AddressInfo; + this.log.info(`HTTP Server started, listening at http://localhost:${info.port}`); + }); + } const browserInfo = new promClient.Gauge({ name: 'grafana_image_renderer_browser_info',