Skip to content

Instantly share code, notes, and snippets.

@hns
Created June 8, 2010 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hns/429711 to your computer and use it in GitHub Desktop.
Save hns/429711 to your computer and use it in GitHub Desktop.
var {Response, skinResponse} = require('ringo/webapp/response');
var {ContinuationSession} = require('ringo/webapp/continuation');
var log = require('ringo/logging').getLogger(module.id);
// the main action is invoked for http://localhost:8080/
exports.index = function(req) {
return skinResponse('./skins/welcome.txt', {title: 'Demo'});
};
// additional path elements are passed to the action as arguments,
// e.g. /extra.path/2008/09
exports.extra_path = function(req, year, month) {
return new Response("Extra arguments:", year, month);
};
exports.upload = {
GET: function(req) {
return skinResponse('./skins/upload.txt', {
title: "File Upload"
});
},
POST: function(req) {
return {
status: 200,
headers: {"Content-Type": req.params.file.contentType},
body: [req.params.file.value]
};
}
};
exports.testing = function(req) {
if (req.params.runtests) {
var test = require("ringo/engine").getRingoHome().getResource("test/all")
var tests = require(test.path);
var formatter = new (require("./helpers").HtmlTestFormatter)();
require("ringo/unittest").run(tests, formatter);
return new Response(formatter);
}
return skinResponse('./skins/testing.txt', {
title: "Unit Testing"
});
};
// demo for skins, macros, filters
exports.skins = function(req) {
return skinResponse('./skins/skins.txt', {
title: 'Skins',
name: 'Luisa',
names: ['Benni', 'Emma', 'Luca', 'Selma']
});
};
// demo for log4j logging
exports.logging = function(req) {
if (req.params.info) {
log.info("Hello world!");
} else if (req.params.error) {
try {
throw new Error("something went wrong");
} catch (e) {
log.error(e);
}
} else if (req.params.profile) {
// build and run a small profiler middleware stack
var profiler = require('ringo/middleware/profiler');
return profiler.middleware(function() {
return skinResponse('./skins/logging.txt', {
title: "Logging & Profiling"
});
})(req);
}
return skinResponse('./skins/logging.txt', { title: "Logging & Profiling" });
};
// demo for continuation support
exports.continuation = function(req, cont_id, cont_step) {
var session = new ContinuationSession(req, cont_id, cont_step);
if (!session.isActive()) {
// render welcome page
return skinResponse('./skins/continuation.txt', {
session: session,
page: "welcome",
title: "Continuations"
});
}
session.addPage("ask_name", function(req) {
return skinResponse('./skins/continuation.txt', {
session: session,
page: session.page,
title: "Question 1"
})
});
session.addPage("ask_food", function(req) {
if (req.isPost)
session.data.name = req.params.name;
return skinResponse('./skins/continuation.txt', {
session: session,
page: session.page,
title: "Question 2"
});
});
session.addPage("ask_animal", function(req) {
if (req.isPost)
session.data.food = req.params.food;
return skinResponse('./skins/continuation.txt', {
session: session,
page: session.page,
title: "Question 3"
});
});
session.addPage("result", function(req) {
if (req.isPost)
session.data.animal = req.params.animal;
return skinResponse('./skins/continuation.txt', {
session: session,
page: session.page,
title: "Thank you!"
});
});
return session.run();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment