diff --git a/.gitignore b/.gitignore index 7d8e622..b6debd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ data dist +config.json node_modules/ diff --git a/README.md b/README.md index 95e7cf4..fdb5388 100644 --- a/README.md +++ b/README.md @@ -37,12 +37,6 @@ Possible to install on: ### Linux -#### Environment variables - -It is possible to export the following environment variables for hastic-server to use: -- HASTIC_API_KEY - (required) API-key of your Grafana instance -- HASTIC_PORT - (optional) port you want to run server on, default: 8000 - #### System prerequisites: * [git](https://git-scm.com/download/linux) @@ -59,11 +53,28 @@ npm install npm run build ``` -#### Run +#### Configuration + +You can configure hastic-server using either *environment variables* or *config file*. + +> NOTE: environment variables have higher priority than config file. + +##### Environment variables +You can export the following environment variables for hastic-server to use: +- HASTIC_API_KEY - (required) API-key of your Grafana instance +- HASTIC_PORT - (optional) port you want to run server on, default: 8000 + +e.g. ```bash -export HASTIC_API_KEY= -export HASTIC_PORT= +export HASTIC_API_KEY=eyJrIjoiVjZqMHY0dHk4UEE3eEN4MzgzRnd2aURlMWlIdXdHNW4iLCJuIjoiaGFzdGljIiwiaWQiOjF9 +export HASTIC_PORT=8080 +``` +##### Config file +You can also rename `config.example.json` to `config.json` and set your values there. + +#### Run +```bash cd ./hastic-server/server npm start ``` diff --git a/analytics/config.py b/analytics/config.py index 602c714..709a198 100644 --- a/analytics/config.py +++ b/analytics/config.py @@ -1,7 +1,30 @@ import os +import json + +def get_config_field(field, default_val = None): + val = default_val + + config_exists = os.path.isfile(CONFIG_FILE) + if config_exists: + with open(CONFIG_FILE) as f: + config = json.load(f) + + if field in os.environ: + val = os.environ[field] + elif config_exists and field in config: + val = config[field] + else: + raise Exception('Please configure {}'.format(field)) + + return val DATA_FOLDER = '../data' -DATASET_FOLDER = os.path.join(DATA_FOLDER, "datasets/") -ANOMALIES_FOLDER = os.path.join(DATA_FOLDER, "anomalies/") -MODELS_FOLDER = os.path.join(DATA_FOLDER, "models/") -METRICS_FOLDER = os.path.join(DATA_FOLDER, "metrics/") +CONFIG_FILE = '../config.json' + +DATASET_FOLDER = os.path.join(DATA_FOLDER, 'datasets/') +ANOMALIES_FOLDER = os.path.join(DATA_FOLDER, 'anomalies/') +MODELS_FOLDER = os.path.join(DATA_FOLDER, 'models/') +METRICS_FOLDER = os.path.join(DATA_FOLDER, 'metrics/') + +HASTIC_API_KEY = get_config_field('HASTIC_API_KEY') + diff --git a/analytics/data_provider.py b/analytics/data_provider.py index 4cecbd6..5653084 100644 --- a/analytics/data_provider.py +++ b/analytics/data_provider.py @@ -5,6 +5,7 @@ from urllib.parse import urlencode, urlparse import urllib.request import json from time import time +from config import HASTIC_API_KEY MS_IN_WEEK = 604800000 @@ -138,7 +139,7 @@ class DataProvider: def __query_grafana(self, params): - headers = { 'Authorization': 'Bearer ' + os.environ['HASTIC_API_KEY'] } + headers = { 'Authorization': 'Bearer ' + HASTIC_API_KEY } url = self.datasource['origin'] + '/' + self.datasource['url'] + '?' + urlencode(params) req = urllib.request.Request(url, headers=headers) diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..a27c12f --- /dev/null +++ b/config.example.json @@ -0,0 +1,4 @@ +{ + "HASTIC_PORT": 8000, + "HASTIC_API_KEY": "eyJrIjoiVjZqMHY0dHk4UEE3eEN4MzgzRnd2aURlMWlIdXdHNW4iLCJuIjoiaGFzdGljIiwiaWQiOjF9" +} diff --git a/server/src/config.ts b/server/src/config.ts index 0b3678c..0bf14f8 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -1,4 +1,10 @@ import * as path from 'path'; +import * as fs from 'fs'; +import { getJsonDataSync } from './services/json'; + + +let configFile = path.join(__dirname, '../../config.json'); +let configExists = fs.existsSync(configFile); export const ANALYTICS_PATH = path.join(__dirname, '../../analytics'); @@ -10,3 +16,22 @@ export const MODELS_PATH = path.join(DATA_PATH, 'models'); export const METRICS_PATH = path.join(DATA_PATH, 'metrics'); export const SEGMENTS_PATH = path.join(DATA_PATH, 'segments'); +export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000'); +function getConfigField(field, defaultVal?) { + let val = defaultVal; + + if(process.env[field] !== undefined) { + val = process.env[field]; + } else if(configExists) { + let config: any = getJsonDataSync(configFile); + + if(config[field] !== undefined) { + val = config[field]; + } + } + + if(val === undefined) { + throw new Error(`Please configure ${field}`) + } + return val; +} diff --git a/server/src/index.ts b/server/src/index.ts index 0e2db65..38fd74f 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -9,10 +9,11 @@ import { router as alertsRouter } from './routes/alerts'; import { checkDataFolders } from './services/data'; +import { HASTIC_PORT } from './config'; + checkDataFolders(); var app = new Koa(); -const PORT = process.env.HASTIC_PORT || 8000; app.use(bodyParser()) @@ -36,7 +37,7 @@ app .use(rootRouter.routes()) .use(rootRouter.allowedMethods()) -app.listen(PORT, () => { - console.log(`Server is running on :${PORT}`) +app.listen(HASTIC_PORT, () => { + console.log(`Server is running on :${HASTIC_PORT}`) });