Commit f67b27e06e45a4a02876ab2ee27848ae0b22ebb5
1 parent
fdf150df47
Exists in
master
html
Showing 1 changed file with 3 additions and 0 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', { | 6 | numeral.language('id', { |
7 | delimiters: { | 7 | delimiters: { |
8 | thousands: '.', | 8 | thousands: '.', |
9 | decimal: ',' | 9 | decimal: ',' |
10 | }, | 10 | }, |
11 | abbreviations: { | 11 | abbreviations: { |
12 | thousand: 'k', | 12 | thousand: 'k', |
13 | million: 'm', | 13 | million: 'm', |
14 | billion: 'b', | 14 | billion: 'b', |
15 | trillion: 't' | 15 | trillion: 't' |
16 | }, | 16 | }, |
17 | ordinal : function (number) { | 17 | ordinal : function (number) { |
18 | return number === 1 ? 'er' : 'ème'; | 18 | return number === 1 ? 'er' : 'ème'; |
19 | }, | 19 | }, |
20 | currency: { | 20 | currency: { |
21 | symbol: 'Rp.' | 21 | symbol: 'Rp.' |
22 | } | 22 | } |
23 | }); | 23 | }); |
24 | numeral.language('id'); | 24 | numeral.language('id'); |
25 | 25 | ||
26 | var Sync = require('sync'); | 26 | var Sync = require('sync'); |
27 | 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); |
28 | 28 | ||
29 | var express = require('express'); | 29 | var express = require('express'); |
30 | var app = express(); | 30 | var app = express(); |
31 | 31 | ||
32 | var suppliers = config.globals.suppliers.split(','); | 32 | var suppliers = config.globals.suppliers.split(','); |
33 | 33 | ||
34 | app.engine('.html', whiskers.__express); | 34 | app.engine('.html', whiskers.__express); |
35 | app.engine('.txt', whiskers.__express); | 35 | app.engine('.txt', whiskers.__express); |
36 | app.engine('.json', whiskers.__express); | 36 | app.engine('.json', whiskers.__express); |
37 | 37 | ||
38 | app.set('views', __dirname+'/views'); | 38 | app.set('views', __dirname+'/views'); |
39 | 39 | ||
40 | function getBalances(suppliers, thousands, callback) { | 40 | function getBalances(suppliers, thousands, callback) { |
41 | 41 | ||
42 | var kw = []; | 42 | var kw = []; |
43 | var count = suppliers.length; | 43 | var count = suppliers.length; |
44 | 44 | ||
45 | for (var i=0; i < count; i++) { | 45 | for (var i=0; i < count; i++) { |
46 | var supplier = suppliers[i]; | 46 | var supplier = suppliers[i]; |
47 | kw.push('balance.gw:' + supplier); | 47 | kw.push('balance.gw:' + supplier); |
48 | } | 48 | } |
49 | 49 | ||
50 | redis.mget(kw, function(err, res) { | 50 | redis.mget(kw, function(err, res) { |
51 | var balances = []; | 51 | var balances = []; |
52 | 52 | ||
53 | for (var i=0; i<count; i++) { | 53 | for (var i=0; i<count; i++) { |
54 | var value = res[i]; | 54 | var value = res[i]; |
55 | if (!value) { | 55 | if (!value) { |
56 | value = 0; | 56 | value = 0; |
57 | } | 57 | } |
58 | 58 | ||
59 | if (thousands) { | 59 | if (thousands) { |
60 | value = numeral(res[i]).format('0,0'); | 60 | value = numeral(res[i]).format('0,0'); |
61 | } | 61 | } |
62 | balances.push({ | 62 | balances.push({ |
63 | title: suppliers[i], | 63 | title: suppliers[i], |
64 | balance: value, | 64 | balance: value, |
65 | }); | 65 | }); |
66 | } | 66 | } |
67 | 67 | ||
68 | callback(null, balances); | 68 | callback(null, balances); |
69 | }); | 69 | }); |
70 | } | 70 | } |
71 | 71 | ||
72 | app.get('/', function(req, res) { | 72 | app.get('/', function(req, res) { |
73 | 73 | ||
74 | Sync(function() { | 74 | Sync(function() { |
75 | var template_file; | 75 | var template_file; |
76 | var thousands = true; | 76 | var thousands = true; |
77 | 77 | ||
78 | res.format({ | 78 | res.format({ |
79 | html: function() { | ||
80 | template_file = 'index.html'; | ||
81 | }, | ||
79 | text: function() { | 82 | text: function() { |
80 | template_file = 'index.txt'; | 83 | template_file = 'index.txt'; |
81 | }, | 84 | }, |
82 | json: function() { | 85 | json: function() { |
83 | thousands = false; | 86 | thousands = false; |
84 | template_file = 'index.json'; | 87 | template_file = 'index.json'; |
85 | }, | 88 | }, |
86 | 'default': function() { | 89 | 'default': function() { |
87 | template_file = 'index.html'; | 90 | template_file = 'index.html'; |
88 | }, | 91 | }, |
89 | 92 | ||
90 | }); | 93 | }); |
91 | 94 | ||
92 | var balances = getBalances.sync(null, suppliers, thousands); | 95 | var balances = getBalances.sync(null, suppliers, thousands); |
93 | //balances = getBalances(suppliers); | 96 | //balances = getBalances(suppliers); |
94 | 97 | ||
95 | res.render(template_file, { | 98 | res.render(template_file, { |
96 | logo: 'http://reload97.com/sites/default/files/web-reload97.png', | 99 | logo: 'http://reload97.com/sites/default/files/web-reload97.png', |
97 | title: 'Supplier Balances', | 100 | title: 'Supplier Balances', |
98 | balances: balances, | 101 | balances: balances, |
99 | timestamp: new Date(), | 102 | timestamp: new Date(), |
100 | }); | 103 | }); |
101 | }) | 104 | }) |
102 | 105 | ||
103 | }); | 106 | }); |
104 | 107 | ||
105 | var server = app.listen(config.globals.http_port, function () { | 108 | var server = app.listen(config.globals.http_port, function () { |
106 | var host = server.address().address; | 109 | var host = server.address().address; |
107 | var port = server.address().port; | 110 | var port = server.address().port; |
108 | 111 | ||
109 | console.log('Example app listening at http://%s:%s', host, port); | 112 | console.log('Example app listening at http://%s:%s', host, port); |
110 | }); | 113 | }); |
111 | 114 |