Created
September 9, 2011 14:02
-
-
Save mauricemach/1206296 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
zappa = | |
run: (f)-> | |
# Re-compile the function in a context that contains the Zappa | |
# methods as globals. | |
app = require('express').createServer() | |
vm = require 'vm' | |
code = 'zappa_code='+f.toString() | |
views = {} | |
router = {} | |
context = | |
view: (name,code) -> | |
views[name] = code | |
render: (name) -> | |
# ReferenceError: this_object is not defined | |
views[name].apply(this_object) | |
get: (location,code) -> | |
router[location] = code | |
try | |
vm.runInNewContext code, context, 'zappa.run() in bob.coffee' | |
catch error | |
console.log "Error = #{error}" | |
context.zappa_code() | |
# The following code gets called from the httpServer's router. | |
app.get '/', (req, res) -> | |
this_object = # Populated from request, etc. | |
params: req.query | |
request: req | |
response: res | |
router['/'].apply(this_object) | |
app.listen 3000 | |
zappa.run -> | |
get '/', -> | |
render 'bob' | |
view 'bob', -> | |
"Hello bob, #{@params.foo}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment