forked from drakmail/bills-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (97 loc) · 3.01 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
var express = require('express')
var serveStatic = require('serve-static')
var bodyParser = require('body-parser')
var nunjucks = require('nunjucks')
var rubles = require('rubles').rubles
var numeral = require('numeral')
var redis = require('redis')
var client = redis.createClient(process.env.REDIS_URL)
var dateFormat = require('dateformat')
var fs = require('fs')
var pdf = require('html-pdf')
var config = require('./config/config.json')
var app = express()
var server = require('http').createServer(app)
var env = nunjucks.configure({
autoescape: true
})
numeral.language('ru', {
delimiters: {
thousands: ' ',
decimal: ','
},
currency: {
symbol: '₽'
}
})
numeral.language('ru')
env.addFilter('countSum', function (services) {
var totalSum = 0
for (var i = 0; i < services.length; i++) {
totalSum = totalSum + (services[i].amount * services[i].price)
}
return totalSum
})
env.addFilter('rubles', function (amount) {
return rubles(amount)
})
env.addFilter('formatMoney', function (number) {
return numeral(number).format('0,0.00')
})
app.use(bodyParser.urlencoded({ extended: true }))
if (!process.env.API_MODE) {
app.use(serveStatic('public', {'index': ['index.html']}))
app.get('/config', function (req, res) {
res.send({
dadata: config.dadata
})
})
}
app.get('/status', function (req, res) {
res.send({
status: 'ok'
})
})
app.post('/request', function (req, httpRes) {
var curDate = dateFormat(new Date(), 'dd.mm.yyyy')
var redisDate = dateFormat(new Date(), 'dmyy')
client.incr('gc:bill-number:' + redisDate, function (err, billNumber) {
if (err) {
console.error(err)
}
billNumber = req.body.bill || (redisDate + '-' + billNumber)
var billname = 'bill-' + billNumber + '.pdf'
var html = fs.readFileSync('./public/data/bill.html', 'utf8')
var options = {
filename: './public/data/bills/' + billname,
format: 'A4'
}
var pdfTemplate = env.renderString(html, {data: req.body, curDate: curDate, billNumber: '' + billNumber, config: config})
pdf.create(pdfTemplate, options).toFile(function (err, res) {
if (err) return console.log(err)
httpRes.set('Content-Disposition', ['attachment; filename=', billname, '.pdf'].join(''))
httpRes.type('pdf')
httpRes.sendFile(res.filename)
})
})
})
var nodeConfig = {
ip: process.env.IP || undefined,
port: process.env.PORT || 8080
}
if (isNaN(nodeConfig.port)) { // unix socket
fs.unlink(nodeConfig.port, function () {
server.listen(nodeConfig.port, nodeConfig.ip, function () {
console.log('Express server listening on %d, in %s mode', nodeConfig.port, app.get('env'))
})
fs.chmod(nodeConfig.port, 0x777)
})
} else { // TCP port
server.listen(nodeConfig.port, nodeConfig.ip, function () {
console.log('Express server listening on %d, in %s mode', nodeConfig.port, app.get('env'))
})
}
// Expose app
exports = module.exports = app