Skip to content

Commit 3b25bb3

Browse files
committed
build a api to make calls into services.
1 parent 0d4e1b9 commit 3b25bb3

File tree

3 files changed

+239
-18
lines changed

3 files changed

+239
-18
lines changed

index.html

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<html>
2+
<head>
3+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
4+
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
5+
crossorigin="anonymous"
6+
rel="stylesheet">
7+
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
8+
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
9+
crossorigin="anonymous"></script>
10+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
11+
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
12+
crossorigin="anonymous"></script>
13+
</head>
14+
<body>
15+
<div class="container">
16+
<h2>Service Testing Interface <small>Mesos Playground</small></h2>
17+
<hr>
18+
<p class="lead">Use this page to make calls back into the cluster to test different parts of the system.</p>
19+
<hr>
20+
21+
<div class="row">
22+
<h4>UUID->GENERATE</h4>
23+
<form class="col-md-12 form-inline" id="uuid_generate">
24+
<button type="submit" class="btn btn-default">Generate</button>
25+
</form>
26+
<div>Result: <span id="uuid_generate_result">0</span></div>
27+
</div>
28+
29+
<hr />
30+
31+
<div class="row">
32+
<h4>MATH->SUM</h4>
33+
<form class="col-md-12 form-inline" id="math_sum">
34+
<div class="form-group">
35+
<label for="math_sum_a">Left Addend: </label>
36+
<input type="text" class="form-control" id="math_sum_a" name="a" placeholder="Number">
37+
</div>
38+
<div class="form-group">
39+
<label for="math_sum_b">Right Addend: </label>
40+
<input type="text" class="form-control" id="math_sum_b" name="b" placeholder="Number">
41+
</div>
42+
<button type="submit" class="btn btn-default">SUM</button>
43+
</form>
44+
<div>Result: <span id="math_sum_result">0</span></div>
45+
</div>
46+
47+
<hr />
48+
49+
<div class="row">
50+
<h4>MATH->PRODUCT</h4>
51+
<form class="col-md-12 form-inline" id="math_product">
52+
<div class="form-group">
53+
<label for="math_product_a">Left Factor: </label>
54+
<input type="text" class="form-control" id="math_product_a" name="a" placeholder="Number">
55+
</div>
56+
<div class="form-group">
57+
<label for="math_product_b">Right Factor: </label>
58+
<input type="text" class="form-control" id="math_product_b" name="b" placeholder="Number">
59+
</div>
60+
<button type="submit" class="btn btn-default">PRODUCT</button>
61+
</form>
62+
<div>Result: <span id="math_product_result">0</span></div>
63+
</div>
64+
65+
<hr />
66+
67+
</div>
68+
69+
<script type="text/javascript">
70+
71+
$().ready(function () {
72+
$('#uuid_generate').submit(function(evt) {
73+
74+
evt.preventDefault();
75+
76+
$.ajax({
77+
method: 'GET',
78+
url: '/api/uuid',
79+
statusCode: {
80+
400: function(d) {
81+
$('#uuid_generate_result').text(d.responseJSON.message + " [FAILED]");
82+
}
83+
}
84+
}).done(function(d) {
85+
$('#uuid_generate_result').text(d.uuid + " [SUCCESS]");
86+
});
87+
});
88+
89+
$('#math_sum').submit(function(evt) {
90+
91+
evt.preventDefault();
92+
93+
var a = Number(this.a.value) || this.a.value;
94+
var b = Number(this.b.value) || this.b.value;
95+
96+
$.ajax({
97+
method: 'POST',
98+
url: '/api/math/sum',
99+
data: { a: a, b: b },
100+
statusCode: {
101+
400: function(d) {
102+
$('#math_sum_result').text(d.responseJSON.message + " [FAILED]");
103+
}
104+
}
105+
}).done(function(d) {
106+
$('#math_sum_result').text(d.sum + " [SUCCESS]");
107+
});
108+
});
109+
110+
$('#math_product').submit(function(evt) {
111+
112+
evt.preventDefault();
113+
114+
var a = Number(this.a.value) || this.a.value;
115+
var b = Number(this.b.value) || this.b.value;
116+
117+
$.ajax({
118+
method: 'POST',
119+
url: '/api/math/product',
120+
data: { a: a, b: b },
121+
statusCode: {
122+
400: function(d) {
123+
$('#math_product_result').text(d.responseJSON.message + " [FAILED]");
124+
}
125+
}
126+
}).done(function(d) {
127+
$('#math_product_result').text(d.product + " [SUCCESS]");
128+
});
129+
});
130+
});
131+
132+
</script>
133+
</body>
134+
</html>

index.js

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,83 @@
1-
const express = require('express')
2-
const app = express()
31

4-
app.get('/', function (req, res) {
5-
res.send('Hello World!')
6-
})
2+
/*
3+
* leftover soup + peta - KILDS LUNCH
4+
* charsia has leftovers for lunch + a peta
5+
* i can have soup + peta or pinto beans + chips and salsa
6+
*/
7+
8+
const express = require('express');
9+
const bodyParser = require('body-parser');
10+
const Proxies = require('@mesos-playground/seneca-proxies');
11+
const pkg = require('./package.json');
12+
13+
const srv = {
14+
math: Proxies.MathProxy(pkg.config.endpoints.MathService),
15+
uuid: Proxies.UuidProxy(pkg.config.endpoints.UuidService)
16+
}
17+
18+
const app = express();
19+
20+
app.use(bodyParser());
21+
22+
app.get('/', function (req, res) { res.sendFile(__dirname+'/index.html'); });
23+
24+
app.get('/api/health/_ping', function(req, res) { res.send('pong'); });
25+
26+
app.get('/api/health/:srv', function(req, res) {
27+
if(srv.hasOwnProperty(req.params.srv)) {
28+
srv[req.params.srv].check().then(()=>{
29+
var ret = {}
30+
ret[req.params.srv] = "OK";
31+
res.json(ret);
32+
}).catch(err=>{
33+
res.status(503).json({ "message": err.message });
34+
});
35+
36+
} else {
37+
res.status(404).json({ "message": "Unknown Service." });
38+
}
39+
});
40+
41+
app.get('/api/uuid', function (req, res) {
42+
srv.uuid.generate().then(id=>{
43+
res.json({ "uuid": id });
44+
}).catch(err=>{
45+
res.status(503).json({ "message": err.message });
46+
});
47+
});
48+
49+
app.post('/api/math/sum', function(req, res) {
50+
console.log(req.body);
51+
52+
var a = Number(req.body.a);
53+
var b = Number(req.body.b);
54+
55+
srv.math.sum(a, b).then(sum=>{
56+
res.json({ "sum": sum });
57+
}).catch(err=>{
58+
if(err.details)
59+
res.status(400).json({ "message": err.details.message });
60+
else
61+
res.status(503).json({ "message": "Service not available." });
62+
});
63+
});
64+
65+
app.post('/api/math/product', function(req, res) {
66+
console.log(req.body);
67+
68+
var a = Number(req.body.a) || req.body.a;
69+
var b = Number(req.body.b) || req.body.b;
70+
71+
srv.math.product(a, b).then(product=>{
72+
res.json({ "product": product });
73+
}).catch(err=>{
74+
if(err.details)
75+
res.status(400).json({ "message": err.details.message });
76+
else
77+
res.status(503).json({ "message": "Service not available." });
78+
});
79+
});
780

881
app.listen(3000, function () {
9-
console.log('Example app listening on port 3000!')
10-
})
82+
console.log('Example app listening on port 3000!')
83+
});

package.json

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
{
2-
"scripts": {
3-
"start": "node index.js"
4-
},
5-
"dependencies": {
6-
"express": "^4.15.4"
7-
},
8-
"devDependencies": {
9-
"grunt": "^1.0.1",
10-
"grunt-contrib-compress": "^1.4.3",
11-
"grunt-copy": "^0.1.0",
12-
"grunt-package-modules": "^1.0.0"
2+
"config": {
3+
"endpoints": {
4+
"MathService": {
5+
"host": "192.168.65.111",
6+
"port": 29144
7+
},
8+
"UuidService": {
9+
"host": "192.168.65.111",
10+
"port": 30965
11+
}
1312
}
13+
},
14+
"scripts": {
15+
"start": "node index.js"
16+
},
17+
"dependencies": {
18+
"@mesos-playground/seneca-proxies": "^1.2.0",
19+
"body-parser": "^1.18.1",
20+
"express": "^4.15.4"
21+
},
22+
"devDependencies": {
23+
"grunt": "^1.0.1",
24+
"grunt-contrib-compress": "^1.4.3",
25+
"grunt-copy": "^0.1.0",
26+
"grunt-package-modules": "^1.0.0"
27+
}
1428
}

0 commit comments

Comments
 (0)