var User = require('./domain/user').User,
	Handler = require('./domain/handler').Handler,
	Project = require('./domain/project').Project;
// The router for the api requests.
var router = exports.router = function (app) {
	// Request for bootstrapping actions.
	app.get('/init', function (req, res, next) {
		var user, project, handler;
		// TODO: find the current user and return his current project.
		user = new User(process.env.USER);
		// XXX: don't store the user in here when we have a proper data store.
		router.user = user;
		if (user.projects.length === 0) {
			project = new Project('MyProject', user.id);
			user.addProject(project);
			handler = new Handler('GET', '/', 'var a = 1;', user.id);
			project.addHandler(handler);
		}
		var body = JSON.stringify({'user': user, 'project': project.id});
		sendResult(res, body);
	});

	app.put('/init', function(req, res, next) {
	    // TODO: find the current user and update the requested handler.
	    var user = router.user;
	    req.body = req.body || {};
		console.log(require('sys').inspect(req));
	    var code = req.body.code;
	    var uri = req.body.uri;
	    var method = req.body.method;
	    var project = req.body.project;
	    if (!code || !method || !uri || !project) {
			console.log("ERROR: project="+project+",uri="+uri+",method="+method+",code="+code);
	        sendError(res, 400);
	        return;
	    }
	    if (!user.projects[project] || !user.projects[project].handlers[method + " " + uri]) {
	        sendError(res, 404);
	        return;
	    }
	    user.projects[project].handlers[method + " " + uri].code = code;
	    sendResult(res);
	});
};

// Helper function to send the result.
var sendResult = function (res, data) {
	res.writeHead(200, {'Content-Type': 'application/json'});
	if (data)
		res.end(data);
	else
		res.end();
};

// Helper function to send the result in error cases.
var sendError = function (res, status, data) {
	res.writeHead(status, {'Content-Type': 'text/plain'});
	if (data)
		res.end(data);
	else
		res.end();
};


@@ -394,16 +394,39 @@
 roject.%0A
+%09%09if (!router.user) %7B%0A%09
 %09%09user =
@@ -454,16 +454,17 @@
 .USER);%0A
+%09
 %09%09// XXX
@@ -530,16 +530,17 @@
 tore.%0A%09%09
+%09
 router.u
@@ -551,16 +551,48 @@
 = user;%0A
+%09%09%7D else%0A%09%09%09user = router.user;%0A
 %09%09if (us
@@ -891,17 +891,51 @@
 );%0A%09%7D);%0A
+%09// Request to store the contents.
 %0A
-
 %09app.put
@@ -1106,52 +1106,8 @@
 %7B%7D;%0A
-%09%09console.log(require('sys').inspect(req));%0A
 %09   
@@ -1570,21 +1570,28 @@
 %0A%09    %7D%0A
-
 %09    
+router.
 user.pro

Make GET /api/init actually work for invocations after the first one.