Commit 492e219efaa3e9fe25f2fe4aacc8902248ec6a23

Authored by Adhidarma Hadiwinoto
1 parent 7600722787
Exists in master

beta

Showing 4 changed files with 119 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 4
5 var numeral = require('numeral');
6
7 var Sync = require('sync');
4 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);
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 });
6 63
1 { 1 {
2 "name": "r97-supplier-balances", 2 "name": "r97-supplier-balances",
3 "version": "0.0.1", 3 "version": "0.0.1",
4 "description": "List supplier's balances", 4 "description": "List supplier's balances",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1" 7 "test": "echo \"Error: no test specified\" && exit 1"
8 }, 8 },
9 "repository": { 9 "repository": {
10 "type": "git", 10 "type": "git",
11 "url": "git@gitlab.kodesumber.com:reload97/r97-supplier-balances.git" 11 "url": "git@gitlab.kodesumber.com:reload97/r97-supplier-balances.git"
12 }, 12 },
13 "keywords": [ 13 "keywords": [
14 "reload97", 14 "reload97",
15 "r97" 15 "r97"
16 ], 16 ],
17 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", 17 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>",
18 "license": "BSD", 18 "license": "BSD",
19 "dependencies": { 19 "dependencies": {
20 "redis": "~1.0.0", 20 "redis": "~1.0.0",
21 "iniparser": "~1.0.5", 21 "iniparser": "~1.0.5",
22 "whiskers": "~0.3.3", 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 }
26 28
views/basic_template.html
File was created 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>
43
File was created 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>
18