From ff2cd6993302d41f9564b64afd527e5279091360 Mon Sep 17 00:00:00 2001 From: serguun42 Date: Mon, 11 Dec 2023 00:17:13 +0300 Subject: [PATCH] Update Grafana paths --- .github/workflows/build.yml | 1 + docker-compose.yml | 1 + monitoring/grafana/grafana.ini | 4 +++ panel/panel.config.json | 1 + panel/panel.js | 46 +++++++++++++++------------------- panel/types/index.d.ts | 1 + 6 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 monitoring/grafana/grafana.ini diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 45bfc12..c2f5c55 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,6 +22,7 @@ jobs: cp $CONFIGS_LOCATION_BASE/scrapper.config.json ./scrapper/; cp $CONFIGS_LOCATION_BASE/backend.config.json ./backend/; cp $CONFIGS_LOCATION_BASE/panel.config.json ./panel/; + cp $CONFIGS_LOCATION_BASE/grafana.ini ./monitoring/grafana/; cp $CONFIGS_LOCATION_BASE/healthcheck.config.json ./healthcheck/; cp $CONFIGS_LOCATION_BASE/telegram-bot.config.json ./telegram/ [[ -f $CONFIGS_LOCATION_BASE/tag-manager.html ]] && diff --git a/docker-compose.yml b/docker-compose.yml index a8263d9..c4c3aa2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -124,6 +124,7 @@ services: GF_AUTH_ANONYMOUS_ENABLED: true GF_AUTH_ANONYMOUS_ORG_ROLE: Admin volumes: + - ./monitoring/grafana/grafana.ini:/etc/grafana/grafana.ini - ./monitoring/grafana/datasource.yml:/etc/grafana/provisioning/datasources/datasource.yml - ./monitoring/grafana/dashboard.yml:/etc/grafana/provisioning/dashboards/dashboard.yml - ./monitoring/grafana/node-dashboard.json:/etc/grafana/provisioning/dashboards/node-dashboard.json diff --git a/monitoring/grafana/grafana.ini b/monitoring/grafana/grafana.ini new file mode 100644 index 0000000..de756e5 --- /dev/null +++ b/monitoring/grafana/grafana.ini @@ -0,0 +1,4 @@ +[server] +domain = panel.domain.tld +root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/ +serve_from_sub_path = true diff --git a/panel/panel.config.json b/panel/panel.config.json index 992ac63..7f30a7f 100644 --- a/panel/panel.config.json +++ b/panel/panel.config.json @@ -5,6 +5,7 @@ "KEYCLOAK_CLIENT_SECRET": "client-secret", "GRAFANA_ORIGIN": "http://mss-grafana:3000", + "GRAFANA_PUBLIC_URL": "https://panel.domain.tld/grafana/", "PANEL_ORIGIN": "https://panel.domain.tld", "PANEL_COOKIE_TTL_SECONDS": 7200, diff --git a/panel/panel.js b/panel/panel.js index 084cced..46f7746 100644 --- a/panel/panel.js +++ b/panel/panel.js @@ -44,6 +44,25 @@ createServer((req, res) => { validateAccessToken(cookies.access_token) .then(() => { + if (path[0] === "grafana") { + fetch(new URL(requestedURL, PANEL_CONFIG.GRAFANA_ORIGIN).href, { + method, + headers: JSON.parse(JSON.stringify(req.headers)), + body: method === "GET" ? undefined : req + }) + .then((grafanaResponse) => { + res.statusCode = grafanaResponse.status; + res.statusMessage = grafanaResponse.statusText; + + Array.from(grafanaResponse.headers.entries()).forEach(([name, value]) => res.setHeader(name, value)); + + grafanaResponse.body.pipe(res); + }) + .catch(() => sendError()); + + return; + } + if (path[0] === "params-panel" || path[0] === "favicon.ico") { if (path[1] === "api" && path[2] === "list") { if (method !== "GET") { @@ -96,34 +115,9 @@ createServer((req, res) => { res.end("405 Method Not Allowed"); return; } - - serveStatic(req, res); - return; } - const sendingHeaders = req.headers; - sendingHeaders.host = "grafana"; - delete sendingHeaders["X-Real-IP"]; - delete sendingHeaders["Forwarded"]; - delete sendingHeaders["X-Forwarded-For"]; - delete sendingHeaders["X-Forwarded-Proto"]; - delete sendingHeaders["X-Forwarded-Host"]; - delete sendingHeaders["X-Forwarded-Port"]; - - fetch(new URL(requestedURL, PANEL_CONFIG.GRAFANA_ORIGIN).href, { - method, - headers: sendingHeaders, - body: method === "GET" ? undefined : req - }) - .then((grafanaResponse) => { - res.statusCode = grafanaResponse.status; - res.statusMessage = grafanaResponse.statusText; - - for (const [name, value] of grafanaResponse.headers) res.setHeader(name, value); - - grafanaResponse.body.pipe(res); - }) - .catch(() => sendError()); + serveStatic(req, res); }) .catch(() => { if ("session_state" in queries && queries.code) { diff --git a/panel/types/index.d.ts b/panel/types/index.d.ts index fa041ed..bf65188 100644 --- a/panel/types/index.d.ts +++ b/panel/types/index.d.ts @@ -5,6 +5,7 @@ export type PanelConfig = { KEYCLOAK_CLIENT_SECRET: string; GRAFANA_ORIGIN: string; + GRAFANA_PUBLIC_URL: string; PANEL_ORIGIN: string; PANEL_COOKIE_TTL_SECONDS: number;