-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathapp.js
97 lines (79 loc) · 2.92 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
// Include the cluster module
const cluster = require('cluster');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
const cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (let i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for terminating workers
cluster.on('exit', function (worker) {
// Replace the terminated workers
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
const AWS = require('aws-sdk');
const express = require('express');
const bodyParser = require('body-parser');
AWS.config.region = process.env.REGION
const sns = new AWS.SNS();
const ddb = new AWS.DynamoDB();
const ddbTable = process.env.STARTUP_SIGNUP_TABLE;
const snsTopic = process.env.NEW_SIGNUP_TOPIC;
const app = express();
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({extended:false}));
app.get('/', function(req, res) {
res.render('index', {
static_path: 'static',
theme: process.env.THEME || 'flatly',
flask_debug: process.env.FLASK_DEBUG || 'false'
});
});
app.post('/signup', function(req, res) {
const item = {
'email': {'S': req.body.email},
'name': {'S': req.body.name},
'preview': {'S': req.body.previewAccess},
'theme': {'S': req.body.theme}
};
ddb.putItem({
'TableName': ddbTable,
'Item': item,
'Expected': { email: { Exists: false } }
}, function(err, data) {
if (err) {
let returnStatus = 500;
if (err.code === 'ConditionalCheckFailedException') {
returnStatus = 409;
}
res.status(returnStatus).end();
console.log('DDB Error: ' + err);
} else {
sns.publish({
'Message': 'Name: ' + req.body.name + "\r\nEmail: " + req.body.email
+ "\r\nPreviewAccess: " + req.body.previewAccess
+ "\r\nTheme: " + req.body.theme,
'Subject': 'New user sign up!!!',
'TopicArn': snsTopic
}, function(err, data) {
if (err) {
res.status(500).end();
console.log('SNS Error: ' + err);
} else {
res.status(201).end();
}
});
}
});
});
const port = process.env.PORT || 3000;
const server = app.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});
}