[enh] there should be a signed cookie authentification

This commit is contained in:
alban 2020-05-21 20:46:47 +02:00
parent 69734ba649
commit 61fae38dde
5 changed files with 104 additions and 83 deletions

73
routes/index.js Normal file
View file

@ -0,0 +1,73 @@
"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");
});
}
};
module.exports = routes;