-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.js
76 lines (56 loc) · 2.02 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
os = require('os'),
hostname = os.hostname();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
var mongoUser = process.env.MONGODB_USER,
mongoDatabase = process.env.MONGODB_DATABASE,
mongoPassword = process.env.MONGODB_PASSWORD,
mongoHost = process.env.TAXCALCDB_SERVICE_HOST,
mongoPort = process.env.TAXCALCDB_SERVICE_PORT,
mongoURL = 'mongodb://';
mongoURL += mongoUser + ':' + mongoPassword + '@';
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
mongoose.connect(mongoURL);
var Transaction = require('./models/transaction');
var route = express.Router();
// All our services are under the /api context
app.use('/api', route);
// Start defining routes for our app/microservice
// A route that dumps hostname information from pod
route.get('/', function(req, res) {
res.send('Hi! I am running on host -> ' + hostname + '\n');
});
// This route handles tax calculation for our service
route.route('/calculate')
.post(function(req, res) {
var tx = new Transaction();
tx.tx_id = req.body.id;
tx.amount = req.body.amount;
// Assume a 30% tax on all orders
var finalAmount = tx.amount + (tx.amount * .2);
tx.save(function(e) {
if (e)
res.send('ERROR: '+ e);
res.json({ message: 'OK',
finalAmount: finalAmount
});
});
});
// This route dumps all transactions
route.route('/list')
.get(function(req, res) {
Transaction.find(function(err, txs) {
if (err)
res.send(err);
res.json(txs);
});
});
app.listen(port, ip);
console.log('nodejs server running on http://%s:%s', ip, port);
module.exports = app;