Created
May 6, 2019 13:00
-
-
Save graduta/75ae4bfff65eb2db7d5fb4b9dad251c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
var request = require('request'); | |
const http = require('http'); | |
var fs = require('fs'); | |
const app = express(); | |
const port = 3000; | |
const ID_PREFIX = 'QC-TASK-'; | |
const OBJECTS = ['Object1', 'Object2', 'Object3', 'Object4', 'Object5']; | |
app.listen(port, () => console.log(`App listening on port ${port}!`)); | |
app.get('/register:id', (req, res) => { | |
const id = ID_PREFIX + req.params.id; | |
console.log('Task: ' + id + ' is being register as a new service'); | |
const tags = ['qc' + id, 'dev']; | |
const data = getServiceJSON(id, tags); | |
registerService(id, data); | |
res.send(id + ' successfully registered.'); | |
}); | |
app.get('/deregister:id', (req, res) => { | |
const id = ID_PREFIX + req.params.id; | |
console.log('My work (' + id + ') here is done.\nI will now gracefully deregister now'); | |
deregisterService(id); | |
res.send('Deregistered ' + id); | |
}); | |
app.get('/healthcheck:id', (req, res) => { | |
const id = req.params.id.split('-')[2]; | |
if (id % 2 == 0) { | |
res.send('Working on objects...' + OBJECTS); | |
} else if (id % 3 == 0) { | |
res.sendStatus(404); | |
} else { | |
res.sendStatus(200); | |
} | |
}); | |
app.get('/box-healthcheck', (req, res) => { | |
const id = parseInt((Math.random() * 100), 10); | |
if (id % 2 == 0) { | |
res.sendStatus(500); | |
} else { | |
res.sendStatus(200); | |
} | |
}); | |
app.get('/healthcheck', (req, res) => { | |
res.send('Loading Objects...'); | |
}); | |
/* | |
Helpers | |
*/ | |
/** | |
* Method to register a new service | |
* @param {string} id - unique id of the new service | |
* @param {JSON} data - service configuration | |
*/ | |
function registerService(id, data) { | |
const options = getOptions('/v1/agent/service/register'); | |
makeHTTPRequest(options, data); | |
} | |
/** | |
* Method to deregister a service by its unique ID | |
* @param {string} id - unique ID of the service we wish to deregister | |
*/ | |
function deregisterService(id) { | |
const options = getOptions('/v1/agent/service/deregister/' + id); | |
makeHTTPRequest(options); | |
} | |
/** | |
* Method to execute HTTP Request based on options and data passed | |
* @param {JSON} options - containing HTTP params | |
* @param {JSON} data - that should be sent | |
*/ | |
function makeHTTPRequest(options, data = JSON.stringify({})) { | |
const req = http.request(options, (res) => { | |
// console.log(`statusCode: ${res.statusCode}`);a | |
res.on('data', (d) => { | |
process.stdout.write(d); | |
}); | |
}); | |
req.on('error', (error) => { | |
console.error(error); | |
}); | |
req.write(data); | |
req.end(); | |
} | |
/** | |
* Method to return a JSON object containing data for HTTP request to the consul server | |
* @param {string} path | |
*/ | |
function getOptions(path) { | |
const options = { | |
hostname: 'localhost', | |
port: 8500, | |
path: path, | |
method: 'PUT', | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}; | |
return options; | |
} | |
/** | |
* Method to generate configuration of the service | |
* @param {string} id - unique ID of the service | |
* @param {array} tags - tags by which services can be searched | |
*/ | |
function getServiceJSON(id, tags) { | |
const data = JSON.stringify({ | |
Name: id, | |
ID: id, | |
port: 20, | |
Tags: tags, | |
Checks: [ | |
{ | |
Id: id, | |
Name: 'Objects still being generated on ' + id, | |
Interval: '5s', | |
HTTP: 'http://localhost:3000/healthcheck' + id | |
}, | |
{ | |
Id: id, | |
Name: 'All secondary tasks running ' + id, | |
Interval: '5s', | |
HTTP: 'http://localhost:3000/box-healthcheck' | |
} | |
] | |
}); | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment