Commit fdf150df4730f1837e68eb0bef9c9773b317f37a
1 parent
f1fc4a3bf5
Exists in
master
json dan plain
Showing 5 changed files with 100 additions and 67 deletions Inline Diff
index.js
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 | numeral.language('id', { | ||
7 | delimiters: { | ||
8 | thousands: '.', | ||
9 | decimal: ',' | ||
10 | }, | ||
11 | abbreviations: { | ||
12 | thousand: 'k', | ||
13 | million: 'm', | ||
14 | billion: 'b', | ||
15 | trillion: 't' | ||
16 | }, | ||
17 | ordinal : function (number) { | ||
18 | return number === 1 ? 'er' : 'ème'; | ||
19 | }, | ||
20 | currency: { | ||
21 | symbol: 'Rp.' | ||
22 | } | ||
23 | }); | ||
24 | numeral.language('id'); | ||
6 | 25 | ||
7 | var Sync = require('sync'); | 26 | var Sync = require('sync'); |
8 | var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host); | 27 | var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host); |
9 | 28 | ||
10 | var express = require('express'); | 29 | var express = require('express'); |
11 | var app = express(); | 30 | var app = express(); |
12 | 31 | ||
13 | var suppliers = config.globals.suppliers.split(','); | 32 | var suppliers = config.globals.suppliers.split(','); |
14 | 33 | ||
15 | app.engine('.html', whiskers.__express); | 34 | app.engine('.html', whiskers.__express); |
35 | app.engine('.txt', whiskers.__express); | ||
36 | app.engine('.json', whiskers.__express); | ||
37 | |||
16 | app.set('views', __dirname+'/views'); | 38 | app.set('views', __dirname+'/views'); |
17 | 39 | ||
18 | function getBalances(suppliers, callback) { | 40 | function getBalances(suppliers, thousands, callback) { |
19 | 41 | ||
20 | var kw = []; | 42 | var kw = []; |
21 | var count = suppliers.length; | 43 | var count = suppliers.length; |
22 | 44 | ||
23 | for (var i=0; i < count; i++) { | 45 | for (var i=0; i < count; i++) { |
24 | var supplier = suppliers[i]; | 46 | var supplier = suppliers[i]; |
25 | kw.push('balance.gw:' + supplier); | 47 | kw.push('balance.gw:' + supplier); |
26 | } | 48 | } |
27 | 49 | ||
28 | redis.mget(kw, function(err, res) { | 50 | redis.mget(kw, function(err, res) { |
29 | var balances = []; | 51 | var balances = []; |
30 | 52 | ||
31 | for (var i=0; i<count; i++) { | 53 | for (var i=0; i<count; i++) { |
54 | var value = res[i]; | ||
55 | if (!value) { | ||
56 | value = 0; | ||
57 | } | ||
58 | |||
59 | if (thousands) { | ||
60 | value = numeral(res[i]).format('0,0'); | ||
61 | } | ||
32 | balances.push({ | 62 | balances.push({ |
33 | title: suppliers[i], | 63 | title: suppliers[i], |
34 | balance: numeral(res[i]).format('0,0') | 64 | balance: value, |
35 | }); | 65 | }); |
36 | } | 66 | } |
37 | 67 | ||
38 | callback(null, balances); | 68 | callback(null, balances); |
39 | }); | 69 | }); |
40 | } | 70 | } |
41 | 71 | ||
42 | app.get('/', function(req, res) { | 72 | app.get('/', function(req, res) { |
43 | 73 | ||
44 | Sync(function() { | 74 | Sync(function() { |
75 | var template_file; | ||
76 | var thousands = true; | ||
77 | |||
78 | res.format({ | ||
79 | text: function() { | ||
80 | template_file = 'index.txt'; | ||
81 | }, | ||
82 | json: function() { | ||
83 | thousands = false; | ||
84 | template_file = 'index.json'; | ||
85 | }, | ||
86 | 'default': function() { | ||
87 | template_file = 'index.html'; | ||
88 | }, | ||
89 | |||
90 | }); | ||
45 | 91 | ||
46 | var balances = getBalances.sync(null, suppliers); | 92 | var balances = getBalances.sync(null, suppliers, thousands); |
47 | //balances = getBalances(suppliers); | 93 | //balances = getBalances(suppliers); |
48 | 94 | ||
49 | res.render('basic_template.html', { | 95 | res.render(template_file, { |
50 | // logo, berdasar isu #1 | ||
51 | logo: 'http://reload97.com/sites/default/files/web-reload97.png', | 96 | logo: 'http://reload97.com/sites/default/files/web-reload97.png', |
52 | title: 'Supplier Balances', | 97 | title: 'Supplier Balances', |
53 | balances: balances, | 98 | balances: balances, |
54 | timestamp: new Date(), | 99 | timestamp: new Date(), |
55 | }); | 100 | }); |
56 | }) | 101 | }) |
57 | 102 | ||
58 | }); | 103 | }); |
59 | 104 | ||
60 | var server = app.listen(config.globals.http_port, function () { | 105 | var server = app.listen(config.globals.http_port, function () { |
61 | var host = server.address().address; | 106 | var host = server.address().address; |
62 | var port = server.address().port; | 107 | var port = server.address().port; |
63 | 108 | ||
64 | console.log('Example app listening at http://%s:%s', host, port); | 109 | console.log('Example app listening at http://%s:%s', host, port); |
65 | }); | 110 | }); |
views/basic_template.html
1 | <!DOCTYPE html> | File was deleted | |
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 | <div class="pull-left"> | ||
22 | <img src="{logo}"> | ||
23 | </div> | ||
24 | <h1> | ||
25 | <br/><br/> | ||
26 | {title} | ||
27 | </h1> | ||
28 | |||
29 | <table class="table table-hover table-striped"> | ||
30 | {for balance in balances} | ||
31 | <tr> | ||
32 | <td>{balance.title}</td> | ||
33 | <td> | ||
34 | <div class="pull-right"> | ||
35 | {balance.balance} | ||
36 | </div> | ||
37 | </td> | ||
38 | </tr> | ||
39 | {/for} | ||
40 | </table> | ||
41 | |||
42 | Generated on {timestamp} | ||
43 | |||
44 | |||
45 | <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | ||
46 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | ||
47 | <!-- Latest compiled and minified JavaScript --> | ||
48 | <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | ||
49 | </body> | ||
50 | </html> | ||
51 | 1 | <!DOCTYPE html> |
views/index.html
1 | <html> | 1 | <!DOCTYPE html> |
2 | <head> | 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 --> | ||
3 | <title>{title}</title> | 8 | <title>{title}</title> |
4 | </head> | ||
5 | <body> | ||
6 | <h1>{title}</h1> | ||
7 | 9 | ||
8 | <table> | 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 | <div class="pull-left"> | ||
22 | <img src="{logo}"> | ||
23 | </div> | ||
24 | <h1> | ||
25 | <br/><br/> | ||
26 | {title} | ||
27 | </h1> | ||
28 | |||
29 | <table class="table table-hover table-striped"> | ||
9 | {for balance in balances} | 30 | {for balance in balances} |
10 | <tr> | 31 | <tr> |
11 | <td>{balance.title}</td> | 32 | <td>{balance.title}</td> |
12 | <td class="pull-right">{balance.balance}</td> | 33 | <td> |
13 | </tr> | 34 | <div class="pull-right"> |
35 | {balance.balance} | ||
36 | </div> | ||
37 | </td> | ||
38 | </tr> | ||
14 | {/for} | 39 | {/for} |
15 | </table> | 40 | </table> |
16 | </body> | 41 | |
42 | Generated on {timestamp} | ||
43 | |||
44 | |||
45 | <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | ||
46 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | ||
47 | <!-- Latest compiled and minified JavaScript --> | ||
48 | <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> |
views/index.json
File was created | 1 | { | |
2 | {for balance in balances}{balance.title}: {balance.balance}, | ||
3 | {/for}} | ||
4 |
views/index.txt
File was created | 1 | {for balance in balances}{balance.title} {balance.balance} | |
2 | {/for} | ||
3 |