Commit 27e500f940c363d6cac3b32c0f78795b5fc06585
1 parent
f67b27e06e
Exists in
master
expresso urls
Showing 2 changed files with 19 additions and 2 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) { | ||
35 | config.expresso_urls = {}; | ||
36 | } | ||
37 | |||
34 | app.engine('.html', whiskers.__express); | 38 | app.engine('.html', whiskers.__express); |
35 | app.engine('.txt', whiskers.__express); | 39 | app.engine('.txt', whiskers.__express); |
36 | app.engine('.json', whiskers.__express); | 40 | app.engine('.json', whiskers.__express); |
37 | 41 | ||
38 | app.set('views', __dirname+'/views'); | 42 | app.set('views', __dirname+'/views'); |
39 | 43 | ||
40 | function getBalances(suppliers, thousands, callback) { | 44 | function getBalances(suppliers, thousands, callback) { |
41 | 45 | ||
42 | var kw = []; | 46 | var kw = []; |
43 | var count = suppliers.length; | 47 | var count = suppliers.length; |
44 | 48 | ||
45 | for (var i=0; i < count; i++) { | 49 | for (var i=0; i < count; i++) { |
46 | var supplier = suppliers[i]; | 50 | var supplier = suppliers[i]; |
47 | kw.push('balance.gw:' + supplier); | 51 | kw.push('balance.gw:' + supplier); |
48 | } | 52 | } |
49 | 53 | ||
50 | redis.mget(kw, function(err, res) { | 54 | redis.mget(kw, function(err, res) { |
51 | var balances = []; | 55 | var balances = []; |
52 | 56 | ||
53 | for (var i=0; i<count; i++) { | 57 | for (var i=0; i<count; i++) { |
54 | var value = res[i]; | 58 | var value = res[i]; |
55 | if (!value) { | 59 | if (!value) { |
56 | value = 0; | 60 | value = 0; |
57 | } | 61 | } |
58 | 62 | ||
59 | if (thousands) { | 63 | if (thousands) { |
60 | value = numeral(res[i]).format('0,0'); | 64 | value = numeral(res[i]).format('0,0'); |
61 | } | 65 | } |
66 | |||
67 | var url = '#'; | ||
68 | var supplier = suppliers[i]; | ||
69 | |||
70 | if (config.expresso_urls[supplier]) { | ||
71 | url = config.expresso_urls[supplier]; | ||
72 | } | ||
73 | |||
62 | balances.push({ | 74 | balances.push({ |
63 | title: suppliers[i], | 75 | title: supplier, |
64 | balance: value, | 76 | balance: value, |
77 | url: url, | ||
65 | }); | 78 | }); |
66 | } | 79 | } |
67 | 80 | ||
68 | callback(null, balances); | 81 | callback(null, balances); |
69 | }); | 82 | }); |
70 | } | 83 | } |
71 | 84 | ||
72 | app.get('/', function(req, res) { | 85 | app.get('/', function(req, res) { |
73 | 86 | ||
74 | Sync(function() { | 87 | Sync(function() { |
75 | var template_file; | 88 | var template_file; |
76 | var thousands = true; | 89 | var thousands = true; |
77 | 90 | ||
78 | res.format({ | 91 | res.format({ |
79 | html: function() { | 92 | html: function() { |
80 | template_file = 'index.html'; | 93 | template_file = 'index.html'; |
81 | }, | 94 | }, |
82 | text: function() { | 95 | text: function() { |
83 | template_file = 'index.txt'; | 96 | template_file = 'index.txt'; |
84 | }, | 97 | }, |
85 | json: function() { | 98 | json: function() { |
86 | thousands = false; | 99 | thousands = false; |
87 | template_file = 'index.json'; | 100 | template_file = 'index.json'; |
88 | }, | 101 | }, |
89 | 'default': function() { | 102 | 'default': function() { |
90 | template_file = 'index.html'; | 103 | template_file = 'index.html'; |
91 | }, | 104 | }, |
92 | 105 | ||
93 | }); | 106 | }); |
94 | 107 | ||
95 | var balances = getBalances.sync(null, suppliers, thousands); | 108 | var balances = getBalances.sync(null, suppliers, thousands); |
96 | //balances = getBalances(suppliers); | 109 | //balances = getBalances(suppliers); |
97 | 110 | ||
98 | res.render(template_file, { | 111 | res.render(template_file, { |
99 | logo: 'http://reload97.com/sites/default/files/web-reload97.png', | 112 | logo: 'http://reload97.com/sites/default/files/web-reload97.png', |
100 | title: 'Supplier Balances', | 113 | title: 'Supplier Balances', |
101 | balances: balances, | 114 | balances: balances, |
102 | timestamp: new Date(), | 115 | timestamp: new Date(), |
103 | }); | 116 | }); |
104 | }) | 117 | }) |
105 | 118 | ||
106 | }); | 119 | }); |
107 | 120 | ||
108 | var server = app.listen(config.globals.http_port, function () { | 121 | var server = app.listen(config.globals.http_port, function () { |
109 | var host = server.address().address; | 122 | var host = server.address().address; |
110 | var port = server.address().port; | 123 | var port = server.address().port; |
111 | 124 | ||
112 | console.log('Example app listening at http://%s:%s', host, port); | 125 | console.log('Example app listening at http://%s:%s', host, port); |
113 | }); | 126 | }); |
114 | 127 |
views/index.html
1 | <!DOCTYPE html> | 1 | <!DOCTYPE html> |
2 | <html lang="en"> | 2 | <html lang="en"> |
3 | <head> | 3 | <head> |
4 | <meta charset="utf-8"> | 4 | <meta charset="utf-8"> |
5 | <meta http-equiv="X-UA-Compatible" content="IE=edge"> | 5 | <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
6 | <meta name="viewport" content="width=device-width, initial-scale=1"> | 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 --> | 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> | 8 | <title>{title}</title> |
9 | 9 | ||
10 | <!-- Latest compiled and minified CSS --> | 10 | <!-- Latest compiled and minified CSS --> |
11 | <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | 11 | <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> |
12 | 12 | ||
13 | <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> | 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:// --> | 14 | <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> |
15 | <!--[if lt IE 9]> | 15 | <!--[if lt IE 9]> |
16 | <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> | 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> | 17 | <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> |
18 | <![endif]--> | 18 | <![endif]--> |
19 | </head> | 19 | </head> |
20 | <body> | 20 | <body> |
21 | <div class="pull-left"> | 21 | <div class="pull-left"> |
22 | <img src="{logo}"> | 22 | <img src="{logo}"> |
23 | </div> | 23 | </div> |
24 | <h1> | 24 | <h1> |
25 | <br/><br/> | 25 | <br/><br/> |
26 | {title} | 26 | {title} |
27 | </h1> | 27 | </h1> |
28 | 28 | ||
29 | <table class="table table-hover table-striped"> | 29 | <table class="table table-hover table-striped"> |
30 | {for balance in balances} | 30 | {for balance in balances} |
31 | <tr> | 31 | <tr> |
32 | <td>{balance.title}</td> | 32 | <td> |
33 | <a href="{balance.url}"> | ||
34 | {balance.title} | ||
35 | </a> | ||
36 | </td> | ||
33 | <td> | 37 | <td> |
34 | <div class="pull-right"> | 38 | <div class="pull-right"> |
35 | {balance.balance} | 39 | {balance.balance} |
36 | </div> | 40 | </div> |
37 | </td> | 41 | </td> |
38 | </tr> | 42 | </tr> |
39 | {/for} | 43 | {/for} |
40 | </table> | 44 | </table> |
41 | 45 | ||
42 | Generated on {timestamp} | 46 | Generated on {timestamp} |
43 | 47 | ||
44 | 48 | ||
45 | <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | 49 | <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> |
46 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | 50 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> |
47 | <!-- Latest compiled and minified JavaScript --> | 51 | <!-- Latest compiled and minified JavaScript --> |
48 | <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | 52 | <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> |
49 | </body> | 53 | </body> |
50 | </html> | 54 | </html> |
51 | 55 |