-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
151 lines (119 loc) · 3.08 KB
/
web.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var bodyParser = require('body-parser');
var express = require('express');
var fs = require('fs');
var http = require('http');
var https = require('https');
var livereload = require('livereload');
var logfmt = require('logfmt');
var pkg = require('./package.json');
var request = require('request');
var PROJECT_ROOT = __dirname;
var SERVER_ROOT = PROJECT_ROOT;
var ENV = require('./get-env.js');
////
var app = express();
var PORT = process.env.PORT || 8000;
var sslOptions = {
cert: fs.readFileSync('fixtures/dev-cert.pem'),
key: fs.readFileSync('fixtures/dev-key.pem'),
};
// Livereload
var lrOptions = {
exts: [
'html',
'css',
'js',
'png',
'gif',
'jpg',
'jpeg',
],
exclusions: [
'.git/',
'.idea/',
'bower_components/',
'node_modules/',
],
applyJSLive: false,
applyCSSLive: true,
https: sslOptions
};
function keepAlive() {
setInterval(function() {
request.get("https://three-strand-code.herokuapp.com/");
}, 300000);
}
/////////////////////////////////////////////////////////////
// SERVER
if (ENV.IN_PRODUCTION) {
http.createServer(app).listen(PORT);
keepAlive();
} else {
https.createServer(sslOptions, app).listen(PORT, function() {
console.log('Server listening at https://localhost:' + PORT);
livereload.createServer(lrOptions).watch(SERVER_ROOT);
});
}
/////////////////////////////////////////////////////////////
// Routing
//
// Logger
app.use(logfmt.requestLogger());
//
// Parse Req Body
app.use(bodyParser.json());
//
// Serve Static
app.use('/', express.static(__dirname + '/'));
//
// Payments
app.use('/charge', function(req, res) {
var stripe = require("stripe")(ENV.STRIPE_SECRET);
var stripeToken = req.body.stripeToken;
//res.send(req.body);
var charge = stripe.charges.create({
amount: req.body.amount,
currency: "usd",
source: stripeToken,
description: req.body.description
}, function(err, charge) {
var errMsg;
if (err) {
switch (err.type) {
case 'StripeCardError':
errMsg = err;
break;
case 'StripeInvalidRequest':
errMsg = 'Please try again or contact hello@3strandcode.';
break;
default:
errMsg = 'Please try again or contact hello@3strandcode.'
}
res.status(400).send(errMsg)
} else {
res.status(200).send(charge);
}
});
});
//
// Custom kinvey endpoints
app.use('/kinvey/:endpoint/', function(req, res) {
var url = 'https://baas.kinvey.com/rpc/' + encodeURIComponent(ENV.KINVEY_APP_KEY) + '/custom/' + req.params.endpoint;
var headers = {
'Authorization': 'Basic ' + new Buffer(ENV.KINVEY_APP_KEY + ':' + ENV.KINVEY_MASTER_SECRET).toString('base64'),
'Content-Type': 'application/json'
};
var form = req.body || {};
request.post({url: url, headers: headers, form: form}, function(error, response, body) {
if (error) {
res.status(400).send(error)
} else {
res.status(response.statusCode).send(body);
}
});
});
//
// Angular html5mode
app.all('/*', function(req, res) {
res.sendFile(__dirname + '/index.html');
});