var express = require('express');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.json({type: "text/json"}));
// app.use(bodyParser.urlencoded());
console.log(process.cwd());
app.use(express.static(process.cwd()));

var savedGraphs = {};

var graphsPath = "../graphs/";
var graphs = {
    "Directed": "AutoGeneratedDirectedGraph.json",
    "NotDirected": "AutoGeneratedNotDirectedGraph.json",
    "BFS": "BFSGraph",
    "Dijkstra": "DijkstraGraph",
    "Sample": "SampleGraph.json",
    "AStar": "AStar.json"
};

for (var name in graphs) {
    var content = require(graphsPath + graphs[name]);
    savedGraphs[name] = {
        graph: content,
        edges: Object.keys(content.edges).length,
        nodes: Object.keys(content.nodes).length,
    }
}

app.post('/save', function(req, res) {
	// console.log(req.body.graph);

	savedGraphs[req.body.name] = {
		graph: req.body.graph,
		edges: req.body.edgeCounter,
		nodes: req.body.nodeCounter
	};
	res.send("ok");
});

app.get('/load', function(req, res) {
	console.log(savedGraphs[req.query.name]);

	res.send(savedGraphs[req.query.name]);
});

app.listen(8080);
console.log('listening to http://localhost:8080');
var express = require('express');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.json({type: "text/json"}));
// app.use(bodyParser.urlencoded());
console.log(process.cwd());
app.use(express.static(process.cwd()));

var savedGraphs = {};

var graphsPath = "../graphs/";
var graphs = {
    "Directed": "AutoGeneratedDirectedGraph.json",
    "NotDirected": "AutoGeneratedNotDirectedGraph.json",
    "BFS": "BFSGraph",
    "Dijkstra": "DijkstraGraph",
    "Sample": "SampleGraph.json",
    "AStar": "AStar.json"
};

for (var name in graphs) {
    var content = require(graphsPath + graphs[name]);
    savedGraphs[name] = {
        graph: content,
        edges: Object.keys(content.edges).length,
        nodes: Object.keys(content.nodes).length,
    }
}

app.post('/save', function(req, res) {
	// console.log(req.body.graph);

	savedGraphs[req.body.name] = {
		graph: req.body.graph,
		edges: req.body.edgeCounter,
		nodes: req.body.nodeCounter
	};
	res.send("ok");
});

app.get('/load', function(req, res) {
	console.log(savedGraphs[req.query.name]);

	res.send(savedGraphs[req.query.name]);
});

app.listen(process.env.PORT || 8080);
console.log('listening to http://localhost:8080');
Set environment port to start http server