Commit 492e219efaa3e9fe25f2fe4aacc8902248ec6a23

Authored by Adhidarma Hadiwinoto
1 parent 7600722787
Exists in master

beta

Showing 4 changed files with 119 additions and 1 deletions Side-by-side Diff

1 1 var iniparser = require('iniparser');
2 2 var config = iniparser.parseSync('./config.ini');
  3 +var whiskers = require('whiskers');
3 4  
  5 +var numeral = require('numeral');
  6 +
  7 +var Sync = require('sync');
4 8 var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host);
5 9  
  10 +var express = require('express');
  11 +var app = express();
  12 +
  13 +var suppliers = config.globals.suppliers.split(',');
  14 +
  15 +app.engine('.html', whiskers.__express);
  16 +app.set('views', __dirname+'/views');
  17 +
  18 +function getBalances(suppliers, callback) {
  19 +
  20 + var kw = [];
  21 + var count = suppliers.length;
  22 +
  23 + for (var i=0; i < count; i++) {
  24 + var supplier = suppliers[i];
  25 + kw.push('balance.gw:' + supplier);
  26 + }
  27 +
  28 + redis.mget(kw, function(err, res) {
  29 + var balances = [];
  30 +
  31 + for (var i=0; i<count; i++) {
  32 + balances.push({
  33 + title: suppliers[i],
  34 + balance: numeral(res[i]).format('0,0')
  35 + });
  36 + }
  37 +
  38 + callback(null, balances);
  39 + });
  40 +}
  41 +
  42 +app.get('/', function(req, res) {
  43 +
  44 + Sync(function() {
  45 +
  46 + var balances = getBalances.sync(null, suppliers);
  47 + //balances = getBalances(suppliers);
  48 +
  49 + res.render('basic_template.html', {
  50 + title: 'Supplier Balances',
  51 + balances: balances
  52 + });
  53 + })
  54 +
  55 +});
  56 +
  57 +var server = app.listen(config.globals.http_port, function () {
  58 + var host = server.address().address;
  59 + var port = server.address().port;
  60 +
  61 + console.log('Example app listening at http://%s:%s', host, port);
  62 +});
... ... @@ -20,6 +20,8 @@
20 20 "redis": "~1.0.0",
21 21 "iniparser": "~1.0.5",
22 22 "whiskers": "~0.3.3",
23   - "express": "~4.13.3"
  23 + "express": "~4.13.3",
  24 + "sync": "~0.2.5",
  25 + "numeral": "~1.5.3"
24 26 }
25 27 }
views/basic_template.html
... ... @@ -0,0 +1,42 @@
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 + <head>
  4 + <meta charset="utf-8">
  5 + <meta http-equiv="X-UA-Compatible" content="IE=edge">
  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 -->
  8 + <title>{title}</title>
  9 +
  10 + <!-- Latest compiled and minified CSS -->
  11 + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  12 +
  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:// -->
  15 + <!--[if lt IE 9]>
  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>
  18 + <![endif]-->
  19 + </head>
  20 + <body>
  21 + <h1>{title}</h1>
  22 +
  23 + <table class="table table-hover table-striped">
  24 + {for balance in balances}
  25 + <tr>
  26 + <td>{balance.title}</td>
  27 + <td>
  28 + <div class="pull-right">
  29 + {balance.balance}
  30 + </div>
  31 + </td>
  32 + </tr>
  33 + {/for}
  34 + </table>
  35 +
  36 +
  37 + <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  38 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  39 + <!-- Latest compiled and minified JavaScript -->
  40 + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  41 + </body>
  42 +</html>
... ... @@ -0,0 +1,17 @@
  1 +<html>
  2 +<head>
  3 + <title>{title}</title>
  4 +</head>
  5 +<body>
  6 +<h1>{title}</h1>
  7 +
  8 +<table>
  9 + {for balance in balances}
  10 + <tr>
  11 + <td>{balance.title}</td>
  12 + <td class="pull-right">{balance.balance}</td>
  13 + </tr>
  14 + {/for}
  15 +</table>
  16 +</body>
  17 +</html>