From 492e219efaa3e9fe25f2fe4aacc8902248ec6a23 Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <gua@adhisimon.org>
Date: Thu, 3 Sep 2015 23:24:13 +0700
Subject: [PATCH] beta

---
 index.js                  | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 package.json              |  4 +++-
 views/basic_template.html | 42 ++++++++++++++++++++++++++++++++++
 views/index.html          | 17 ++++++++++++++
 4 files changed, 119 insertions(+), 1 deletion(-)
 create mode 100644 views/basic_template.html
 create mode 100644 views/index.html

diff --git a/index.js b/index.js
index 1d40433..519a971 100644
--- a/index.js
+++ b/index.js
@@ -1,5 +1,62 @@
 var iniparser = require('iniparser');
 var config = iniparser.parseSync('./config.ini');
+var whiskers = require('whiskers');
 
+var numeral = require('numeral');
+
+var Sync = require('sync');
 var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host);
 
+var express = require('express');
+var app = express();
+
+var suppliers = config.globals.suppliers.split(',');
+
+app.engine('.html', whiskers.__express);
+app.set('views', __dirname+'/views');
+
+function getBalances(suppliers, callback) {
+    
+    var kw = [];
+    var count = suppliers.length;
+    
+    for (var i=0; i < count; i++) {
+        var supplier = suppliers[i];
+        kw.push('balance.gw:' + supplier);
+    }
+    
+    redis.mget(kw, function(err, res) {
+        var balances = [];
+        
+        for (var i=0; i<count; i++) {
+            balances.push({
+                title: suppliers[i],
+                balance: numeral(res[i]).format('0,0')
+            });
+        }
+        
+        callback(null, balances);
+    });
+}
+
+app.get('/', function(req, res) {
+    
+    Sync(function() {
+        
+        var balances = getBalances.sync(null, suppliers);
+        //balances = getBalances(suppliers);
+        
+        res.render('basic_template.html', {
+            title: 'Supplier Balances',
+            balances: balances
+        });
+    })
+
+});
+
+var server = app.listen(config.globals.http_port, function () {
+  var host = server.address().address;
+  var port = server.address().port;
+
+  console.log('Example app listening at http://%s:%s', host, port);
+});
diff --git a/package.json b/package.json
index 0143c14..57be70f 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,8 @@
     "redis": "~1.0.0",
     "iniparser": "~1.0.5",
     "whiskers": "~0.3.3",
-    "express": "~4.13.3"
+    "express": "~4.13.3",
+    "sync": "~0.2.5",
+    "numeral": "~1.5.3"
   }
 }
diff --git a/views/basic_template.html b/views/basic_template.html
new file mode 100644
index 0000000..bdc88ec
--- /dev/null
+++ b/views/basic_template.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
+    <title>{title}</title>
+
+    <!-- Latest compiled and minified CSS -->
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
+
+    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
+    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
+    <!--[if lt IE 9]>
+      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
+      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
+    <![endif]-->
+  </head>
+  <body>
+    <h1>{title}</h1>
+    
+    <table class="table table-hover table-striped">
+    {for balance in balances}
+        <tr>
+            <td>{balance.title}</td>
+            <td>
+                <div class="pull-right">
+                    {balance.balance}
+                </div>
+            </td>
+        </tr>
+    {/for}
+    </table>
+
+
+    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
+    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
+    <!-- Latest compiled and minified JavaScript -->
+    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
+  </body>
+</html>
diff --git a/views/index.html b/views/index.html
new file mode 100644
index 0000000..b58b312
--- /dev/null
+++ b/views/index.html
@@ -0,0 +1,17 @@
+<html>
+<head>
+    <title>{title}</title>
+</head>
+<body>
+<h1>{title}</h1>
+
+<table>
+    {for balance in balances}
+    <tr>
+        <td>{balance.title}</td>
+        <td class="pull-right">{balance.balance}</td>
+    </tr>
+    {/for}
+</table>
+</body>
+</html>
-- 
1.9.0