Skip to content

Commit 379f23e

Browse files
committed
Use config.json for options related to getting metadata/mappings.
1 parent f115f5e commit 379f23e

File tree

6 files changed

+17
-24
lines changed

6 files changed

+17
-24
lines changed

config/config.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
"templatesPath": {
1313
"relative": true,
1414
"path": "/rule_templates"
15-
}
16-
}
15+
},
16+
"es_host": "elastalert",
17+
"es_port": 9200,
18+
"writeback_index": "elastalert_status"
19+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"express": "^4.14.0",
2929
"fs-extra": "^5.0.0",
3030
"joi": "^13.1.2",
31-
"js-yaml": "^3.12.0",
3231
"lodash": "^4.15.0",
3332
"mkdirp": "^0.5.1",
3433
"object-resolve-path": "^1.1.1",

src/common/config/schema.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Joi from 'joi';
33

44
const schema = Joi.object().keys({
55
'appName': Joi.string().default('elastalert-server'),
6+
'es_host': Joi.string().default('elastalert'),
7+
'es_port': Joi.number().default(9200),
8+
'writeback_index': Joi.string().default('elastalert_status'),
69
'port': Joi.number().default(3030),
710
'elastalertPath': Joi.string().default('/opt/elastalert'),
811
'rulesPath': Joi.object().keys({

src/common/elasticsearch_client.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import yaml from 'js-yaml';
2-
import fs from 'fs';
3-
import process from 'process';
41
import elasticsearch from 'elasticsearch';
5-
6-
export function getConfig() {
7-
try {
8-
var appRoot = process.cwd();
9-
var config = yaml.safeLoad(fs.readFileSync(appRoot + '/config/elastalert.yaml', 'utf8'));
10-
return config;
11-
} catch (e) {
12-
console.error(e);
13-
}
14-
}
2+
import config from './config';
153

164
export function getClient() {
17-
var config = getConfig();
185
var client = new elasticsearch.Client({
19-
hosts: [ `http://${config.es_host}:${config.es_port}`]
6+
hosts: [ `http://${config.get('es_host')}:${config.get('es_port')}`]
207
});
218
return client;
229
}

src/handlers/mapping/get.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { getClient } from '../../common/elasticsearch_client';
22

3-
var client = getClient();
4-
53
export default function metadataHandler(request, response) {
64
/**
75
* @type {ElastalertServer}
86
*/
97

8+
var client = getClient();
9+
1010
client.indices.getMapping({
1111
index: request.params.index
1212
}).then(function(resp) {

src/handlers/metadata/get.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { getConfig, getClient } from '../../common/elasticsearch_client';
1+
import config from '../../common/config';
2+
import { getClient } from '../../common/elasticsearch_client';
23

3-
var config = getConfig();
4-
var client = getClient();
54

65
function getQueryString(request) {
76
if (request.params.type === 'elastalert_error') {
@@ -16,8 +15,10 @@ export default function metadataHandler(request, response) {
1615
/**
1716
* @type {ElastalertServer}
1817
*/
18+
var client = getClient();
19+
1920
client.search({
20-
index: config.writeback_index,
21+
index: config.get('writeback_index'),
2122
type: request.params.type,
2223
body: {
2324
from : request.query.from || 0,

0 commit comments

Comments
 (0)