From 335d8e7e36cc37841ee48dbf06ae3a1833c58db2 Mon Sep 17 00:00:00 2001 From: sanke1 <22073083+sankerust@users.noreply.github.com> Date: Sun, 24 Feb 2019 20:19:44 +0300 Subject: [PATCH] Accept various URL inputs #416 (#429) --- server/package.json | 1 + server/spec/utils/url.jest.ts | 24 ++++++++++++++++++++++++ server/src/config.ts | 6 ++++-- server/src/utils/url.ts | 26 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 server/spec/utils/url.jest.ts create mode 100644 server/src/utils/url.ts diff --git a/server/package.json b/server/package.json index d7dba2e..1fc344c 100644 --- a/server/package.json +++ b/server/package.json @@ -50,6 +50,7 @@ "ts-jest": "^23.1.1", "ts-loader": "^4.4.1", "typescript": "^2.8.3", + "url-parse": "^1.4.4", "webpack": "^4.12.0", "webpack-cli": "^3.0.8", "zeromq": "^4.6.0" diff --git a/server/spec/utils/url.jest.ts b/server/spec/utils/url.jest.ts new file mode 100644 index 0000000..db0746c --- /dev/null +++ b/server/spec/utils/url.jest.ts @@ -0,0 +1,24 @@ +import { normalizeUrl } from '../../src/utils/url'; + +describe('Normalize URL', function() { + const cases = [ + { value: 'localhost:8000', expected: 'http://localhost:8000' }, + { value: 'localhost:8000/', expected: 'http://localhost:8000' }, + { value: 'http://localhost:3000', expected: 'http://localhost:3000' }, + { value: 'http://localhost:3000/', expected: 'http://localhost:3000' }, + { value: 'https://localhost:8000', expected: 'https://localhost:8000' }, + { value: 'https://localhost:8000/', expected: 'https://localhost:8000' }, + { value: 'http://example.com', expected: 'http://example.com' }, + { value: 'http://example.com/', expected: 'http://example.com' }, + { value: 'https://example.com', expected: 'https://example.com' }, + { value: 'https://example.com/', expected: 'https://example.com' }, + { value: 'https://example.com/grafana', expected: 'https://example.com/grafana' }, + { value: 'https://example.com/grafana/', expected: 'https://example.com/grafana' }, + ]; + + it('should normalize URLs correctly', function() { + cases.forEach(testCase => { + expect(normalizeUrl(testCase.value)).toBe(testCase.expected); + }); + }); +}); diff --git a/server/src/config.ts b/server/src/config.ts index 1d17688..3302ecd 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -1,7 +1,9 @@ +import { getJsonDataSync } from './services/json_service'; +import { normalizeUrl } from './utils/url'; + import * as path from 'path'; import * as fs from 'fs'; import * as os from 'os'; -import { getJsonDataSync } from './services/json_service'; let configFile = path.join(__dirname, '../../config.json'); @@ -23,7 +25,7 @@ export const ZMQ_IPC_PATH = getConfigField('ZMQ_IPC_PATH', path.join(os.tmpdir() export const ZMQ_DEV_PORT = getConfigField('ZMQ_DEV_PORT', '8002'); export const ZMQ_HOST = getConfigField('ZMQ_HOST', '127.0.0.1'); export const HASTIC_API_KEY = getConfigField('HASTIC_API_KEY'); -export const GRAFANA_URL = getConfigField('GRAFANA_URL', null); +export const GRAFANA_URL = normalizeUrl(getConfigField('GRAFANA_URL', null)); export const HASTIC_WEBHOOK_URL = getConfigField('HASTIC_WEBHOOK_URL', null); export const HASTIC_WEBHOOK_TYPE = getConfigField('HASTIC_WEBHOOK_TYPE', 'application/x-www-form-urlencoded'); export const HASTIC_WEBHOOK_SECRET = getConfigField('HASTIC_WEBHOOK_SECRET', null); diff --git a/server/src/utils/url.ts b/server/src/utils/url.ts new file mode 100644 index 0000000..aee3754 --- /dev/null +++ b/server/src/utils/url.ts @@ -0,0 +1,26 @@ +import * as url from 'url-parse'; + +export function normalizeUrl(grafanaUrl: string) { + let urlObj = new url(grafanaUrl); + if(urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') { + grafanaUrl = `http://${grafanaUrl}`; + urlObj = new url(grafanaUrl); + console.log('No protocol provided in GRAFANA_URL -> inserting "http://"'); + } + if(urlObj.slashes === false) { + urlObj = new url(`${urlObj.protocol}//${urlObj.pathname}`); + console.log('No slashes were provided after the protocol -> inserting slashes'); + } + if(urlObj.pathname.slice(-1) === '/') { + urlObj.pathname = urlObj.pathname.slice(0, -1); + console.log('Removing the slash at the end of GRAFANA_URL'); + } + let finalUrl = `${urlObj.protocol}//${urlObj.hostname}`; + if(urlObj.port !== '') { + finalUrl = finalUrl + ':' + urlObj.port; + } + if(urlObj.pathname !== '') { + finalUrl = finalUrl + urlObj.pathname; + } + return finalUrl; +}