var express = require("express");
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var request = require('request');
var pg = require('pg');

server.listen(process.env.PORT || 3000);

pg.connect(process.env.DATABASE_URL, function(err, client) {
  var create = client.query('CREATE TABLE trombi(
  	id SERIAL PRIMARY KEY,
  	nom VARCHAR(255),
  	prenom VARCHAR(255), 
  	promo INTEGER,
  	PRIMARY KEY(id)
  	)');
  });
});

// Use static folders with node.js
app.use("/bootstrap", express.static(__dirname + '/bootstrap'));
app.use("/bild", express.static(__dirname + '/build'));
app.use("/dist", express.static(__dirname + '/dist'));
app.use("/font", express.static(__dirname + '/font'));
app.use("/documentation", express.static(__dirname + '/documentation'));
app.use("/pages", express.static(__dirname + '/pages'));
app.use("/plugins", express.static(__dirname + '/plugins'));
app.use("/css", express.static(__dirname + '/css'));
app.use("/images", express.static(__dirname + '/images'));
app.use("/admin", express.static(__dirname + '/admin'));
app.use("/js", express.static(__dirname + '/js'));

// Render all HTML pages
app.get('/',function(req,res){
  res.sendFile(__dirname + '/index.html');
});

app.get('/index.html',function(req,res){
  res.sendFile(__dirname + '/index.html');
});

app.get('/pages/calendar.html',function(req,res){
  res.sendFile(__dirname + '/pages/calendar.html');
});

app.get('/documentation/index.html',function(req,res){
  res.sendFile(__dirname + '/documentation/index.html');
});

app.get('/admin/administration.html',function(req,res){
  res.sendFile(__dirname + '/admin/administration.html');
});

app.get('/admin/tromi.html',function(req,res){
  res.sendFile(__dirname + '/admin/trombi.html');
});

app.get('/admin/boite.html',function(req,res){
  res.sendFile(__dirname + '/admin/boite.html');
});

app.get('/admin/liens.html',function(req,res){
  res.sendFile(__dirname + '/admin/liens.html');
});
// End of rendering HTML pages

// OpenWeatherMAp : envoi des données MTO 
var openweathermeteo = function(url, callback){
	
	request(url, function(err, response, body){
		try{
			var result = JSON.parse(body);
			  		
			callback(null, result);
		}catch(e){
			callback(e); 
		}
	});
}

// Quand on client se connecte, on lui envoi un message pour lui avertir
io.sockets.on('connection', function (socket) {
	openweathermeteo('http://api.openweathermap.org/data/2.5/weather?q=montreuil,fr', function(err, result){
		if(err) return console.log(err);
		var mto = [];
		//mto['latitude'] = result['city']['coord']['lat'];
		//mto['longitude'] = result['city']['coord']['lat'];
		mto['temp'] = result['main']['temp'];
		//mto['temp_min'] = result['list'][0]['temp']['min'];
		//mto['temp_max'] = result['list'][0]['temp']['max'];
		mto['desc'] = result['weather'][0].description;
		console.log(result['weather'][0].description);
		socket.emit("meteo", {temperature : mto['temp'], description : mto['desc']});
		});
});
@@ -205,46 +205,239 @@
 );%0A%0A
-server.listen(process.env.PORT %7C%7C 3000
+var conString = %22postgres://yntviqlzscnbqq:w15ISVFDrLBp414QG9sXfbHBMY@ec2-54-204-35-248.compute-1.amazonaws.com:5432/d80v1h0fvs1qi5%22;%0A%0Aserver.listen(process.env.PORT %7C%7C 3000);%0A%0Avar client = new pg.Client(conString);%0Aclient.connect(
 );%0A%0A
@@ -503,22 +503,21 @@
 %7B%0A  var 
-create
+query
  = clien
@@ -524,17 +524,17 @@
 t.query(
-'
+%22
 CREATE T
@@ -542,138 +542,80 @@
 BLE 
-trombi(%0A  %09id SERIAL PRIMARY KEY,%0A  %09nom VARCHAR(255),%0A  %09prenom VARCHAR(255), %0A  %09promo INTEGER,%0A  %09PRIMARY KEY(id)%0A  %09)'
+IF NOT EXISTS emps(firstname varchar(64), lastname varchar(64))%22
 );%0A  %7D);
 %0A%7D);
@@ -610,20 +610,16 @@
 );%0A  %7D);
-%0A%7D);
 %0A%0A// Use

create ds trombi table