2020-05-21 18:46:47 +00:00
|
|
|
"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"});
|
|
|
|
});
|
|
|
|
},
|
2020-05-23 21:26:51 +00:00
|
|
|
delete: (req,res) => {
|
|
|
|
const id= req.params.id;
|
|
|
|
// Reindex the doc to the "trash" index
|
|
|
|
var log = client.reindex({
|
|
|
|
refresh: true,
|
|
|
|
max_docs: 1,
|
|
|
|
body: {
|
|
|
|
source: {
|
|
|
|
index: 'changelog',
|
|
|
|
query: {
|
|
|
|
term: {
|
|
|
|
_id: id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
dest: {
|
|
|
|
index: 'changelog-trash',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then( (results, err) => {
|
|
|
|
|
|
|
|
console.log(`reindexing success for id ${id}`)
|
|
|
|
// Remove it from the original index
|
|
|
|
return client.delete({
|
|
|
|
index: "changelog",
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
|
|
|
|
}, (e) => {
|
|
|
|
|
|
|
|
console.log("reindexing error")
|
|
|
|
res.status(400);
|
|
|
|
res.end("error");
|
|
|
|
|
|
|
|
})
|
|
|
|
.then( (results, err) => {
|
|
|
|
|
|
|
|
console.log(`Delete success for id ${id}`)
|
|
|
|
res.end("ok");
|
|
|
|
|
|
|
|
},(results, err) => {
|
|
|
|
console.log(`Delete error for id ${id}`)
|
|
|
|
res.status(400);
|
|
|
|
res.end("error");
|
|
|
|
});
|
|
|
|
},
|
2020-05-21 18:46:47 +00:00
|
|
|
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");
|
|
|
|
});
|
2020-05-22 15:54:33 +00:00
|
|
|
} ,
|
|
|
|
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");
|
|
|
|
});
|
2020-05-21 18:46:47 +00:00
|
|
|
}
|
|
|
|
};
|
2020-05-22 15:54:33 +00:00
|
|
|
module.exports = routes;
|