changelog-server/routes/index.js
2020-05-22 15:54:33 +00:00

92 lines
2.0 KiB
JavaScript

"use strict"
const elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: process.env.ES_CONNECT,
// log: 'trace',
apiVersion: '7.7'
});
const routes = {
main: (req, res) => {
client.search({index:"changelog", "size":100,"sort":"created_at:desc"}).then( (results,err) => {
res.render('index', {
title: 'changelog',
error: err,
data: JSON.stringify( results),
authorizationToken: process.env.AUTH_TOKEN
});
});
},
search: (req, res) => {
const query = req.query.q;
const search = {
index:"changelog",
size:100,
body:{
query:{
multi_match:{
query: query
}
}
},
sort:"_score,created_at:desc"
};
client.search(search).then( (results,err) => {
res.json(results );
}, (err) => {
res.status(404);
res.json({data: {} });
});
},
health: (req, res) => {
// Do an ES request
client.ping({ requestTimeout: 100}).then(
() => {
res.json({"health":100,"msg":"OK"});
}, () => {
res.json({"health":0,"msg":"Lost connection to ES"});
});
},
add: (req, res) => {
const body = req.body;
body.created_at = new Date().toISOString();
client.index({
index: 'changelog',
body: body
}).then( (e) => {
res.end("ok");
}, (e) => {
res.status(400);
res.end("error");
});
} ,
log: (req, res) => {
const id= req.params.id;
var log = client.get({
index: 'changelog',
id: id
}).then( (results, err) => {
res.render('index', {
title: 'changelog',
error: err,
data: JSON.stringify( results),
authorizationToken: process.env.AUTH_TOKEN
});
}, (e) => {
res.status(400);
res.end("error");
});
}
};
module.exports = routes;