|
|
|
|
@ -6,9 +6,9 @@ const database = 'mongodb://' + process.env.mongousr + ':' + process.env.mongopw
|
|
|
|
|
const today = new Date(); |
|
|
|
|
var counter = 0; |
|
|
|
|
const Prometheus = require('prom-client'); |
|
|
|
|
var fs = require('file-system'); |
|
|
|
|
var marked = require('marked'); |
|
|
|
|
const bodyParser = require('body-parser'); |
|
|
|
|
const fs = require('file-system'); |
|
|
|
|
const marked = require('marked'); |
|
|
|
|
// const bodyParser = require('body-parser');
|
|
|
|
|
const jwt = require('jsonwebtoken'); |
|
|
|
|
|
|
|
|
|
// database connection (with retries)
|
|
|
|
|
@ -84,31 +84,53 @@ app.get('/metrics2', function(req, res){
|
|
|
|
|
res.end(); |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// JWT generation
|
|
|
|
|
app.use(express.json()); |
|
|
|
|
|
|
|
|
|
app.post('/token', function(request, response){ |
|
|
|
|
console.log(request.body); // your JSON
|
|
|
|
|
response.send(request.body); // echo the result back
|
|
|
|
|
app.post('/token', function(req, res){ |
|
|
|
|
console.log(req.body); |
|
|
|
|
console.log(req.body.username); |
|
|
|
|
console.log(req.body.password); |
|
|
|
|
if (req.body.username=='user1') { |
|
|
|
|
if (req.body.password=='pass1') { |
|
|
|
|
var token = jwt.sign(req.body, 'wowmuchsecretveryhiddenwow'); |
|
|
|
|
console.log(token); |
|
|
|
|
// res.json(token);
|
|
|
|
|
res.status(200).json({ |
|
|
|
|
success: 'SUCCESS! You\'re in.', |
|
|
|
|
token: token |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
res.status(500).send('this is not the password I expected'); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
res.status(500).send('this is not the user I want');; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Default message for testing
|
|
|
|
|
app.get('/', (req, res)=>{ |
|
|
|
|
res.json([{message:'yes, your nodejs app is really running'}]); |
|
|
|
|
counter++; // for prometheus invocation_count metric
|
|
|
|
|
libCounter.inc(); // for prometheus lib_invocation_count metric
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Test endpoint for md files rendering
|
|
|
|
|
app.get('/test', function(req, res) { |
|
|
|
|
var path = '/app/README.md'; |
|
|
|
|
var file = fs.readFileSync(path, 'utf8'); |
|
|
|
|
res.send(marked(file.toString())); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mongo query
|
|
|
|
|
app.get('/info', function(req, res){ |
|
|
|
|
colors.find({}).then(function (colors) { |
|
|
|
|
res.json(colors); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mongo insert
|
|
|
|
|
app.post('/info/add/:name', function(req, res){ |
|
|
|
|
var item = {color: req.params.name}; |
|
|
|
|
var data = new colors(item); |
|
|
|
|
@ -116,24 +138,8 @@ app.post('/info/add/:name', function(req, res){
|
|
|
|
|
res.send('color ' + req.params.name + ' added to database'); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// app.get('/token/:user&:password', function(req, res){
|
|
|
|
|
// res.json(req.params);
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
connectWithRetry(); |
|
|
|
|
|
|
|
|
|
app.listen(3001, () => { |
|
|
|
|
console.log('Server running on port 3001'); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// var today = new Date();
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// setInterval(()=>{
|
|
|
|
|
// // console.log('process.uptime(): ' + process.uptime());
|
|
|
|
|
// var now = new Date();
|
|
|
|
|
// var passed = now - today;
|
|
|
|
|
// console.log('# HELP uptime A summary of the application\'s uptime.')
|
|
|
|
|
// console.log('# TYPE uptime summary')
|
|
|
|
|
// console.log('uptime ' + passed);
|
|
|
|
|
// }, 1000)
|
|
|
|
|
}); |