Commit 27a9b629f3324b6a834e466d2054d045dfe1615c
1 parent
c704ee3996
Exists in
master
Logo ke imageshack
Showing 1 changed file with 1 additions and 1 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 | if (!config.expresso_urls) { | 34 | if (!config.expresso_urls) { |
35 | config.expresso_urls = {}; | 35 | config.expresso_urls = {}; |
36 | } | 36 | } |
37 | 37 | ||
38 | app.engine('.html', whiskers.__express); | 38 | app.engine('.html', whiskers.__express); |
39 | app.engine('.txt', whiskers.__express); | 39 | app.engine('.txt', whiskers.__express); |
40 | app.engine('.json', whiskers.__express); | 40 | app.engine('.json', whiskers.__express); |
41 | 41 | ||
42 | app.set('views', __dirname+'/views'); | 42 | app.set('views', __dirname+'/views'); |
43 | 43 | ||
44 | function getBalances(suppliers, thousands, callback) { | 44 | function getBalances(suppliers, thousands, callback) { |
45 | 45 | ||
46 | var kw = []; | 46 | var kw = []; |
47 | var count = suppliers.length; | 47 | var count = suppliers.length; |
48 | 48 | ||
49 | for (var i=0; i < count; i++) { | 49 | for (var i=0; i < count; i++) { |
50 | var supplier = suppliers[i]; | 50 | var supplier = suppliers[i]; |
51 | kw.push('balance.gw:' + supplier); | 51 | kw.push('balance.gw:' + supplier); |
52 | } | 52 | } |
53 | 53 | ||
54 | redis.mget(kw, function(err, res) { | 54 | redis.mget(kw, function(err, res) { |
55 | var balances = []; | 55 | var balances = []; |
56 | 56 | ||
57 | for (var i=0; i<count; i++) { | 57 | for (var i=0; i<count; i++) { |
58 | var value = res[i]; | 58 | var value = res[i]; |
59 | if (!value) { | 59 | if (!value) { |
60 | value = 0; | 60 | value = 0; |
61 | } | 61 | } |
62 | 62 | ||
63 | if (thousands) { | 63 | if (thousands) { |
64 | value = numeral(res[i]).format('0,0'); | 64 | value = numeral(res[i]).format('0,0'); |
65 | } | 65 | } |
66 | 66 | ||
67 | var url = null; | 67 | var url = null; |
68 | var supplier = suppliers[i]; | 68 | var supplier = suppliers[i]; |
69 | 69 | ||
70 | if (config.expresso_urls[supplier]) { | 70 | if (config.expresso_urls[supplier]) { |
71 | url = config.expresso_urls[supplier]; | 71 | url = config.expresso_urls[supplier]; |
72 | } | 72 | } |
73 | 73 | ||
74 | balances.push({ | 74 | balances.push({ |
75 | title: supplier, | 75 | title: supplier, |
76 | balance: value, | 76 | balance: value, |
77 | url: url, | 77 | url: url, |
78 | }); | 78 | }); |
79 | } | 79 | } |
80 | 80 | ||
81 | callback(null, balances); | 81 | callback(null, balances); |
82 | }); | 82 | }); |
83 | } | 83 | } |
84 | 84 | ||
85 | app.get('/', function(req, res) { | 85 | app.get('/', function(req, res) { |
86 | 86 | ||
87 | Sync(function() { | 87 | Sync(function() { |
88 | var template_file; | 88 | var template_file; |
89 | var thousands = true; | 89 | var thousands = true; |
90 | 90 | ||
91 | res.format({ | 91 | res.format({ |
92 | html: function() { | 92 | html: function() { |
93 | template_file = 'index.html'; | 93 | template_file = 'index.html'; |
94 | }, | 94 | }, |
95 | text: function() { | 95 | text: function() { |
96 | template_file = 'index.txt'; | 96 | template_file = 'index.txt'; |
97 | }, | 97 | }, |
98 | json: function() { | 98 | json: function() { |
99 | thousands = false; | 99 | thousands = false; |
100 | template_file = 'index.json'; | 100 | template_file = 'index.json'; |
101 | }, | 101 | }, |
102 | 'default': function() { | 102 | 'default': function() { |
103 | template_file = 'index.html'; | 103 | template_file = 'index.html'; |
104 | }, | 104 | }, |
105 | 105 | ||
106 | }); | 106 | }); |
107 | 107 | ||
108 | var balances = getBalances.sync(null, suppliers, thousands); | 108 | var balances = getBalances.sync(null, suppliers, thousands); |
109 | //balances = getBalances(suppliers); | 109 | //balances = getBalances(suppliers); |
110 | 110 | ||
111 | res.render(template_file, { | 111 | res.render(template_file, { |
112 | logo: 'http://reload97.com/sites/default/files/web-reload97.png', | 112 | logo: 'https://imageshack.com/a/img922/2950/APPRuh.png', |
113 | title: 'Supplier Balances', | 113 | title: 'Supplier Balances', |
114 | balances: balances, | 114 | balances: balances, |
115 | json_string: JSON.stringify(balances), | 115 | json_string: JSON.stringify(balances), |
116 | timestamp: new Date(), | 116 | timestamp: new Date(), |
117 | }); | 117 | }); |
118 | }) | 118 | }) |
119 | 119 | ||
120 | }); | 120 | }); |
121 | 121 | ||
122 | var server = app.listen(config.globals.http_port, function () { | 122 | var server = app.listen(config.globals.http_port, function () { |
123 | var host = server.address().address; | 123 | var host = server.address().address; |
124 | var port = server.address().port; | 124 | var port = server.address().port; |
125 | 125 | ||
126 | console.log('Example app listening at http://%s:%s', host, port); | 126 | console.log('Example app listening at http://%s:%s', host, port); |
127 | }); | 127 | }); |
128 | 128 |