You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.6 KiB
150 lines
4.6 KiB
const express = require('express'); |
|
const app = express(); |
|
const mongoose = require('mongoose'); |
|
const Schema = mongoose.Schema; |
|
const database = 'mongodb://' + process.env.mongousr + ':' + process.env.mongopwd + '@mongo:27017/test'; |
|
const today = new Date(); |
|
var counter = 0; |
|
const Prometheus = require('prom-client'); |
|
const fs = require('file-system'); |
|
const marked = require('marked'); |
|
const jwt = require('jsonwebtoken'); |
|
const bodyParser= require('body-parser'); |
|
|
|
// database connection (with retries) |
|
const options = { |
|
autoIndex: false, // Don't build indexes |
|
reconnectTries:30, // Retry up to 30 times |
|
reconnectInterval: 500, // Reconnect every 500ms |
|
poolSize: 10, // Maintain up to 10 socket connections |
|
// If not connected, return errors immediately rather than waiting for reconnect |
|
bufferMaxEntries: 0, |
|
useNewUrlParser: true |
|
} |
|
|
|
const connectWithRetry = () => { |
|
console.log('MongoDB connection with retry') |
|
mongoose.connect(database, options).then(()=>{ |
|
console.log('MongoDB is connected') |
|
}).catch(err=>{ |
|
console.log('MongoDB connection unsuccessful, retry after 5 seconds.') |
|
setTimeout(connectWithRetry, 5000) |
|
}) |
|
} |
|
|
|
const libCounter = new Prometheus.Counter({ |
|
name: 'lib_invocation_count', |
|
help: 'A simple counter for app access during runtime created with prometheus nodejs library' |
|
}); |
|
|
|
const libUptime = new Prometheus.Counter({ |
|
name: 'lib_upTime', |
|
help: 'uptime A counter of the application\'s uptime in seconds created with prometheus nodejs library.' |
|
}) |
|
|
|
console.log('mongousr: ', process.env.mongousr); |
|
console.log('mongopwd: ', process.env.mongopwd); |
|
|
|
// Prometheus Default Metrics collector |
|
const collectDefaultMetrics = Prometheus.collectDefaultMetrics; |
|
// Probe every 5th second. |
|
collectDefaultMetrics({ timeout: 5000 }); |
|
|
|
// new schema model object based on the structure of what I want to put on MongoDB collection |
|
var testSchema = new Schema({ |
|
color: { |
|
type: String |
|
} |
|
},{ |
|
collection: 'test' |
|
}); |
|
|
|
// new object that will hold the data using model structure made above |
|
var colors = mongoose.model('colorName', testSchema); |
|
|
|
// Prometheus metrics endpoint - Library |
|
app.get('/metrics', function(req, res){ |
|
libUptime.inc(Math.floor(process.uptime())); |
|
res.set('Content-Type', Prometheus.register.contentType) |
|
res.end(Prometheus.register.metrics()) |
|
libUptime.reset(); |
|
}); |
|
|
|
// Prometheus metrics endpoint - Handmade |
|
app.get('/metrics2', function(req, res){ |
|
var now = new Date(); |
|
var passedTime = now - today; |
|
res.writeHead(200, {'Content-Type':'text/plain'}); |
|
res.write('# HELP uptime A counter of the application\'s uptime in millisenconds.' + '\n'); |
|
res.write('# TYPE uptime counter' + '\n'); |
|
res.write('uptime ' + passedTime + '\n'); |
|
res.write('# HELP invocation_count A simple counter for app access during runtime' + '\n'); |
|
res.write('# TYPE invocation_count counter'+ '\n'); |
|
res.write('invocation_count ' + counter + '\n'); |
|
res.end(); |
|
}) |
|
|
|
// JWT generation |
|
// app.use(express.json()); |
|
app.use(bodyParser.urlencoded({ extended: false })); |
|
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 |
|
// }); |
|
res.redirect('http://localhost/yay.html'); |
|
} else { |
|
// res.status(500).send('this is not the password I expected'); |
|
res.redirect('http://localhost/nay.html'); |
|
} |
|
} else { |
|
// res.status(500).send('this is not the user I want'); |
|
res.redirect('http://localhost/nay.html'); |
|
} |
|
}); |
|
|
|
// 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); |
|
data.save(); |
|
res.send('color ' + req.params.name + ' added to database'); |
|
}); |
|
|
|
connectWithRetry(); |
|
|
|
app.listen(3001, () => { |
|
console.log('Server running on port 3001'); |
|
console.log('process.env.PORT: ' + process.env.PORT); |
|
}); |