Skip to content

Commit

Permalink
Update Grafana paths
Browse files Browse the repository at this point in the history
  • Loading branch information
serguun42 committed Dec 10, 2023
1 parent 7ca2718 commit ff2cd69
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]] &&
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions monitoring/grafana/grafana.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[server]
domain = panel.domain.tld
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
serve_from_sub_path = true
1 change: 1 addition & 0 deletions panel/panel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 20 additions & 26 deletions panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions panel/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ff2cd69

Please sign in to comment.