Browse Source

Accept various URL inputs #416 (#429)

pull/1/head
sanke1 6 years ago committed by rozetko
parent
commit
335d8e7e36
  1. 1
      server/package.json
  2. 24
      server/spec/utils/url.jest.ts
  3. 6
      server/src/config.ts
  4. 26
      server/src/utils/url.ts

1
server/package.json

@ -50,6 +50,7 @@
"ts-jest": "^23.1.1", "ts-jest": "^23.1.1",
"ts-loader": "^4.4.1", "ts-loader": "^4.4.1",
"typescript": "^2.8.3", "typescript": "^2.8.3",
"url-parse": "^1.4.4",
"webpack": "^4.12.0", "webpack": "^4.12.0",
"webpack-cli": "^3.0.8", "webpack-cli": "^3.0.8",
"zeromq": "^4.6.0" "zeromq": "^4.6.0"

24
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);
});
});
});

6
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 path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import * as os from 'os'; import * as os from 'os';
import { getJsonDataSync } from './services/json_service';
let configFile = path.join(__dirname, '../../config.json'); 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_DEV_PORT = getConfigField('ZMQ_DEV_PORT', '8002');
export const ZMQ_HOST = getConfigField('ZMQ_HOST', '127.0.0.1'); export const ZMQ_HOST = getConfigField('ZMQ_HOST', '127.0.0.1');
export const HASTIC_API_KEY = getConfigField('HASTIC_API_KEY'); 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_URL = getConfigField('HASTIC_WEBHOOK_URL', null);
export const HASTIC_WEBHOOK_TYPE = getConfigField('HASTIC_WEBHOOK_TYPE', 'application/x-www-form-urlencoded'); export const HASTIC_WEBHOOK_TYPE = getConfigField('HASTIC_WEBHOOK_TYPE', 'application/x-www-form-urlencoded');
export const HASTIC_WEBHOOK_SECRET = getConfigField('HASTIC_WEBHOOK_SECRET', null); export const HASTIC_WEBHOOK_SECRET = getConfigField('HASTIC_WEBHOOK_SECRET', null);

26
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;
}
Loading…
Cancel
Save