Commit ee4b9bcb3d13e3a617c7b5d53fa475d88b458db0

Authored by Adhidarma Hadiwinoto
1 parent 492e219efa
Exists in master

timestamp

Showing 2 changed files with 4 additions and 1 deletions Inline Diff

1 var iniparser = require('iniparser'); 1 var iniparser = require('iniparser');
2 var config = iniparser.parseSync('./config.ini'); 2 var config = iniparser.parseSync('./config.ini');
3 var whiskers = require('whiskers'); 3 var whiskers = require('whiskers');
4 4
5 var numeral = require('numeral'); 5 var numeral = require('numeral');
6 6
7 var Sync = require('sync'); 7 var Sync = require('sync');
8 var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host); 8 var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host);
9 9
10 var express = require('express'); 10 var express = require('express');
11 var app = express(); 11 var app = express();
12 12
13 var suppliers = config.globals.suppliers.split(','); 13 var suppliers = config.globals.suppliers.split(',');
14 14
15 app.engine('.html', whiskers.__express); 15 app.engine('.html', whiskers.__express);
16 app.set('views', __dirname+'/views'); 16 app.set('views', __dirname+'/views');
17 17
18 function getBalances(suppliers, callback) { 18 function getBalances(suppliers, callback) {
19 19
20 var kw = []; 20 var kw = [];
21 var count = suppliers.length; 21 var count = suppliers.length;
22 22
23 for (var i=0; i < count; i++) { 23 for (var i=0; i < count; i++) {
24 var supplier = suppliers[i]; 24 var supplier = suppliers[i];
25 kw.push('balance.gw:' + supplier); 25 kw.push('balance.gw:' + supplier);
26 } 26 }
27 27
28 redis.mget(kw, function(err, res) { 28 redis.mget(kw, function(err, res) {
29 var balances = []; 29 var balances = [];
30 30
31 for (var i=0; i<count; i++) { 31 for (var i=0; i<count; i++) {
32 balances.push({ 32 balances.push({
33 title: suppliers[i], 33 title: suppliers[i],
34 balance: numeral(res[i]).format('0,0') 34 balance: numeral(res[i]).format('0,0')
35 }); 35 });
36 } 36 }
37 37
38 callback(null, balances); 38 callback(null, balances);
39 }); 39 });
40 } 40 }
41 41
42 app.get('/', function(req, res) { 42 app.get('/', function(req, res) {
43 43
44 Sync(function() { 44 Sync(function() {
45 45
46 var balances = getBalances.sync(null, suppliers); 46 var balances = getBalances.sync(null, suppliers);
47 //balances = getBalances(suppliers); 47 //balances = getBalances(suppliers);
48 48
49 res.render('basic_template.html', { 49 res.render('basic_template.html', {
50 title: 'Supplier Balances', 50 title: 'Supplier Balances',
51 balances: balances 51 balances: balances,
52 timestamp: new Date(),
52 }); 53 });
53 }) 54 })
54 55
55 }); 56 });
56 57
57 var server = app.listen(config.globals.http_port, function () { 58 var server = app.listen(config.globals.http_port, function () {
58 var host = server.address().address; 59 var host = server.address().address;
59 var port = server.address().port; 60 var port = server.address().port;
60 61
61 console.log('Example app listening at http://%s:%s', host, port); 62 console.log('Example app listening at http://%s:%s', host, port);
62 }); 63 });
63 64
views/basic_template.html
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <html lang="en"> 2 <html lang="en">
3 <head> 3 <head>
4 <meta charset="utf-8"> 4 <meta charset="utf-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta name="viewport" content="width=device-width, initial-scale=1"> 6 <meta name="viewport" content="width=device-width, initial-scale=1">
7 <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 7 <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
8 <title>{title}</title> 8 <title>{title}</title>
9 9
10 <!-- Latest compiled and minified CSS --> 10 <!-- Latest compiled and minified CSS -->
11 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 11 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
12 12
13 <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 13 <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
14 <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 14 <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
15 <!--[if lt IE 9]> 15 <!--[if lt IE 9]>
16 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 16 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
17 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 17 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
18 <![endif]--> 18 <![endif]-->
19 </head> 19 </head>
20 <body> 20 <body>
21 <h1>{title}</h1> 21 <h1>{title}</h1>
22 22
23 <table class="table table-hover table-striped"> 23 <table class="table table-hover table-striped">
24 {for balance in balances} 24 {for balance in balances}
25 <tr> 25 <tr>
26 <td>{balance.title}</td> 26 <td>{balance.title}</td>
27 <td> 27 <td>
28 <div class="pull-right"> 28 <div class="pull-right">
29 {balance.balance} 29 {balance.balance}
30 </div> 30 </div>
31 </td> 31 </td>
32 </tr> 32 </tr>
33 {/for} 33 {/for}
34 </table> 34 </table>
35
36 Generated on {timestamp}
35 37
36 38
37 <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 39 <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
38 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 40 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
39 <!-- Latest compiled and minified JavaScript --> 41 <!-- Latest compiled and minified JavaScript -->
40 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 42 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
41 </body> 43 </body>
42 </html> 44 </html>
43 45