Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created Nov 17, 2010
Embed
What would you like to do?
Expose any Io object over the web. An interesting little snippet
#!/usr/bin/env io
Object webapp := method (
block (reqMsg,
p := reqMsg split("/")
self doMessage(p first asMessage setArguments( p rest map(asMessage) ))
)
)
doRelativeFile("webserver.io")
WebServer setExpose(list() webapp) start
# http://localhost:9292/append/1 -> list(1)
# http://localhost:9292/append/2/3 -> list(1,2,3)
# http://localhost:9292/join -> 123
# http://localhost:9292/size -> 3
# http://localhost:9292/pop -> 3
# http://localhost:9292/removeFirst -> 1
# http://localhost:9292/asString -> list(2)
# see: "Expose any Ruby object over the web. An interesting little snippet"
# http://news.ycombinator.com/item?id=1910120
# https://gist.github.com/675667
WebRequest := Object clone do(
handleSocket := method (socket, obj,
request := socket read readBuffer betweenSeq("GET /", " HTTP")
if (request == "favicon.ico", socket close; return)
try (result := obj call(request)) catch (result := "Pull the other one!")
socket streamWrite(result)
socket close
)
)
WebServer := Server clone do (
setPort(9292)
expose ::= nil
handleSocket := method (socket,
WebRequest clone @handleSocket(socket, expose)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment