-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.js
135 lines (119 loc) · 4.4 KB
/
server.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var express = require('express')
var app = express()
var nodemailer = require("nodemailer")
var compression = require('compression')()
// Helpers.
var p = console.log.bind(console)
var info = function(msg){
var withZero = function(number){return (number < 10) ? ('0' + number) : number}
var timestamp = function(){
date = new Date()
return date.getFullYear() + '/' + withZero(date.getMonth() + 1) + '/' +
withZero(date.getDate()) + ' ' +
withZero(date.getHours()) + ':' + withZero(date.getMinutes()) + ':' +
withZero(date.getSeconds())
}
console.info(' ', timestamp(), msg.toString().substring(0, 1000))
}
// Parsing environment variables.
var env = process.env
var options = {}
options.port = parseInt(env.port || 3000)
options.requestLimit = parseInt(env.requestLimit || 5 * 1024)
options.smtpHost = env.smtpHost || 'smtp.mailgun.org'
options.smtpPort = parseInt(env.smtpPort || 25)
options.fromAddress = env.fromAddress || '[email protected]'
options.smtpUser = env.smtpUser
options.smtpPassword = env.smtpPassword
// Compression.
app.use(compression)
// Templates, I don't want to use express templates because I want to keep
// all the templates for each language in one file.
app.templates = {}
app.template = function(language, name, fn){
(this.templates[language] = this.templates[language] || {})[name] = function(){
var buff = []
var args = Array.prototype.slice.call(arguments)
args.unshift(function(str){buff.push(str)})
fn.apply(null, args)
return buff.join("\n")
}
}
// Render template - `render(language, name, args...)`.
app.render = function(){
var args = Array.prototype.slice.call(arguments, 2)
var localizedTemplates = this.templates[arguments[0]]
if(!localizedTemplates) throw new Error('no templates for ' + arguments[0] + ' language!')
var localizedTemplate = localizedTemplates[arguments[1]]
if(!localizedTemplates)
throw new Error('no template ' + arguments[1] + ' for ' + arguments[0] + ' language!')
return localizedTemplate.apply(null, args)
}
// Helper.
app.priceWithCurrency = function(price, currency){
if(['$', '£'].indexOf(currency) >= 0) return currency + price
else return price + currency
}
// Translations.
require('./server/languages/english')(app)
require('./server/languages/russian')(app)
require('./server/languages/ukrainian')(app)
// Preparing email.
var transporter = nodemailer.createTransport({
host : options.smtpHost,
port : options.smtpPort,
auth: {
user: options.smtpUser,
pass: options.smtpPassword
}
})
// Serving static files from `client` folder and telling browser
// to cache it.
//
// Using version in path in order to provide backward compatibile API
// in future.
app.use('/v1', function(req, res, next){
var referer = req.get('Referer')
if(referer && /\/cart.js/.test(req.path)) info("serving " + decodeURI(referer))
next()
})
app.use('/v1', express.static(__dirname + '/client', {maxAge: 31557600000}))
// Serving site.
app.use(express.static(__dirname + '/documentation', {maxAge: 31557600000}))
// Middleware for request parsing.
app.use(express.json({limit: options.requestLimit}))
app.use(express.urlencoded())
app.use(express.multipart({limit: options.requestLimit}))
// Processing orders.
app.post('/v1/orders', function(req, res){
var order = JSON.parse(req.body.data)
// Need this for logging.
info('order from ' + order.site + ' for '
+ app.priceWithCurrency(order.price, order.currency) + ' (' + req.body.data + ')')
// Setting special headers to allow cross domain requsts.
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
res.header('Access-Control-Allow-Headers', 'Content-Type')
// Sending mail to shop owner.
transporter.sendMail({
from : options.fromAddress,
to : order.emailOrdersTo,
subject : app.render(order.language, 'owner-email-subject', order),
text : app.render(order.language, 'owner-email-text', order)
}, function(err){
if(err){
info("can't send email to " + order.emailOrdersTo + ' because of '
+ (err.message || err))
res.end(500, '{}')
}
else res.end('{}')
})
})
// Starting server.
app.listen(options.port)
info("server started on " + options.port + " port")
// Node.js sometimes leek memory, restarting the process periodically.
setTimeout(function(){
info('restarting by timeout')
process.exit()
}, 4 * 60 * 60 * 1000)