Created
November 17, 2010 14:24
-
-
Save draegtun/703431 to your computer and use it in GitHub Desktop.
Expose any Io object over the web. An interesting little snippet
This file contains 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
#!/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 |
This file contains 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
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