-
-
Save earl/429900 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** config entry for upload routing: | |
** [ '/upload', './actions', 'upload', {methods: ['GET', 'POST']} ], | |
** alternatively: | |
** [ '/upload', './actions', 'upload_get', {methods: ['GET']} ], | |
** [ '/upload', './actions', 'upload_post', {methods: ['POST']} ], | |
*/ | |
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" | |
}); | |
}; | |
exports.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