diff --git a/app.js b/app.js index 8fbeff5..24d624d 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,7 @@ var bodyParser = require('body-parser'); var express = require('express'); var fs = require('fs'); var connection = require("./js/mysql.js"); +var mysql = require("mysql"); var rhg = require('random-hash-generator'); @@ -19,36 +20,10 @@ app.use(bodyParser.urlencoded({ })); app.use(bodyParser.json()); -const PORT = 8081; +const PORT = 8077; var types; -function connect() { - // if (connection !== undefined && connection !== null) { - // connection.destroy(); - // } - - connection.connect(function(err) { - if (err) { - console.log(err); - connect(); - } - else { - connection.on('error', function(err) { - console.log('db error', err); - if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually - connect(); - } - else { // connnection idle timeout (the wait_timeout - throw err; // server variable configures this) - } - }); - } - }); - -} - -connect(); function query(str, callback) { query(str, [], callback); @@ -56,8 +31,8 @@ function query(str, callback) { function query(str, arg, callback) { var isConnected = true; - - connection.query(str, arg, callback); + + connection.query(str, arg, callback); } query("SELECT * FROM types", function(err, result) { @@ -66,7 +41,7 @@ query("SELECT * FROM types", function(err, result) { app.port = PORT; -app.host = "127.0.0.1"; +app.host = "0.0.0.0"; function generateId(data) { pastesThisSession++; @@ -195,5 +170,5 @@ app.get("/:path", handleRequest); app.get("/:path/:option", handleRequest); app.listen(PORT, "127.0.0.1", 511, function() { - + console.log("Static Webserver started on port "+PORT); }); diff --git a/js/mysql.js b/js/mysql.js index 649279a..67de413 100644 --- a/js/mysql.js +++ b/js/mysql.js @@ -1,26 +1,53 @@ -var mysql = require('mysql'); -var fs = require("fs"); +var mysql = require('mysql'); +var fs = require("fs"); -var file = null; -var connection = null; +var file = null; +var connectionConfig; +var connection = null; + +function handleDisconnect() { + connection = mysql.createConnection(connectionConfig); + + connection.connect(function(err) { + if(err) { + console.log('error when connecting to db:', err); + setTimeout(handleDisconnect, 2000); + } + else { + console.log("Connected to MySQL database ( "+connectionConfig.user+"@"+connectionConfig.host+"/"+connectionConfig.database+" )"); + } + }); + connection.on('error', function(err) { + console.log('db error', err); + if(err.code === 'PROTOCOL_CONNECTION_LOST') { + handleDisconnect(); + } else if(err.code === 'ECONNRESET'){ + handleDisconnect(); + } else{ + throw err; + } + }); +} try { - file = fs.readFileSync("./config.json"); + file = fs.readFileSync("./config.json"); } catch(exception) { - console.log(exception.toString()); + console.log(exception.toString()); } if(file != null) { - var json = JSON.parse(file); + var json = JSON.parse(file); - connection = mysql.createConnection({ - host: json.host, - database: json.database, - user: json.user, - password: json.password - }); + connectionConfig = { + host: json.host, + database: json.database, + user: json.user, + password: json.password + }; + + handleDisconnect(); } -module.exports = connection; \ No newline at end of file +module.exports = connection; diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..11d5d52 --- /dev/null +++ b/start.sh @@ -0,0 +1 @@ +screen -dmS paste 'while true; do node ./app.js; done' diff --git a/static/insert.html b/static/insert.html index ef340e2..7bc6f98 100644 --- a/static/insert.html +++ b/static/insert.html @@ -69,8 +69,8 @@ viewModel.selectedTypeName = ko.observable(); viewModel.selectedTypeIndex = ko.observable(); - viewModel.selectedTypeName(viewModel.types[0].type); - viewModel.selectedTypeIndex(0); + viewModel.selectedTypeName(viewModel.types[31].type); + viewModel.selectedTypeIndex(31); $("#pasteButton").on("click", function(){ $.ajax({ @@ -85,11 +85,11 @@ console.log(data); if(data.id !== null){ - window.location = "./"+data.id+"/"; + window.location = "./"+data.id+"/"; } } }) - }) ; + }); $("#typeList").bind( "click" , function(element){ selectedType = viewModel.types[$(element.target).attr("typeindex")];