Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ jobs:
steps:
- run:
name: deploy-application
command: ssh -o StrictHostKeyChecking=no $EC2_USERNAME@ec2-54-173-235-121.compute-1.amazonaws.com "rm -rf ExampleApplication/;git clone https://github.com/Carrick7/ExampleApplication.git; source ExampleApplication/deploy.sh"
command: ssh -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_PUBLIC_DNS "export SERVER=\"$SERVER\"; export PRIVATE_KEY=\"$PRIVATE_KEY\"; rm -rf ExampleApplication/;git clone https://github.com/Carrick7/ExampleApplication.git; source ExampleApplication/deploy.sh"

95 changes: 67 additions & 28 deletions bin/www
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('mathapp:server');
var debug = require('debug')('phishsense:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/
Expand Down Expand Up @@ -88,3 +61,69 @@ function onListening() {
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

// Add HTTPS Section
var fs = require('fs');
var https = require('https');
var port = normalizePort(process.env.PORT || '8080');
var https_port = process.env.PORT_HTTPS || 8443;
var options = {}

if(process.env.ENV !== "DEV") {
var privatekey = fs.readFileSync('privatekey.pem', "utf8")
console.log(typeof privatekey)
var cert = fs.readFileSync('server.crt', "utf8")
var header = "-----BEGIN PRIVATE KEY-----"
var footer = "-----END PRIVATE KEY-----"
console.log(privatekey.split(header))
privatekey= privatekey.split(header)[1]
privatekey = privatekey.split(footer)[0]
privatekey = header + "\n" + privatekey.replace(/ /g, "\n") + footer+"\n"

console.log(privatekey)

var header = "-----BEGIN CERTIFICATE-----"
var footer = "-----END CERTIFICATE-----"
cert= cert.split(header)[1]
cert = cert.split(footer)[0]
cert = header + "\n" + cert.replace(/ /g, "\n") + footer+"\n"

var options = {
key : privatekey,
cert : cert
};
app.set("port",https_port);

/*
° Create HTTPS server.
*/
var server = https.createServer(options, app).listen(https_port, function () {
console.log('Magic happens on port ' + https_port);
});

/**
* Listen on provided port, on all network interfaces.
*/

server.on('error', onError);
server.on('listening', onListening);

// Redirect from http port to https
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'].replace(port,https_port) + req.url });
console.log("http requet, will go to >> ");
console.log("https://" + req.headers['host'].replace(port,https_port) + req.url );
res.end();
}).listen(port);
} else {

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
}
4 changes: 4 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ pm2 stop ExampleApplication
cd ExampleApplication/
# Install application dependencies
npm install
echo $PRIVATE_KEY > privatekey.pem
echo $SERVER > server.crt
# pm2 save error
sudo pm2 save
# Start the applciation with the process name ExampleApplication using pm2
pm2 start ./bin/www --name ExampleApplication