170 lines
3.9 KiB
JavaScript
170 lines
3.9 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"});
|
|
});
|
|
},
|
|
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");
|
|
});
|
|
},
|
|
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");
|
|
});
|
|
} ,
|
|
get: (req, res) => {
|
|
|
|
const id= req.params.id;
|
|
var log = client.get({
|
|
index: 'changelog',
|
|
id: id
|
|
}).then( (results, err) => {
|
|
res.json(results);
|
|
}, (e) => {
|
|
res.status(400);
|
|
res.json({msg:"Failed to get record"});
|
|
});
|
|
},
|
|
patch: (req,res) => {
|
|
const id= req.params.id;
|
|
const content= req.body.content;
|
|
var log = client.update({
|
|
|
|
index: 'changelog',
|
|
id: id,
|
|
body:{
|
|
doc:{
|
|
content:content
|
|
}
|
|
}
|
|
}).then( (results, err) => {
|
|
res.json(results);
|
|
}, (e) => {
|
|
res.status(400);
|
|
res.json({msg:"Failed to get record"});
|
|
}); },
|
|
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;
|