Skip to content

Instantly share code, notes, and snippets.

@surma
Created November 6, 2017 15:02
Show Gist options
  • Save surma/5e4a943a652937e683b357ef21f5119e to your computer and use it in GitHub Desktop.
Save surma/5e4a943a652937e683b357ef21f5119e to your computer and use it in GitHub Desktop.
use "net/http"
actor Main
new create(env: Env) =>
match env.root
| None =>
env.out.print("No access to network")
return
| let aa: AmbientAuth =>
HTTPServer(
aa,
Listener(env),
ConnectionHandler(env)
where host = "localhost", service = "8080"
)
end
class Listener
let _env: Env
new iso create(env': Env) =>
_env = env'
fun listening(server: HTTPServer ref) =>
try
(let host, let port) = server.local_address().name()?
_env.out.print("Listening on port: " + host + ":" + port)
else
_env.out.print("Listening somewhere??")
end
fun not_listening(server: HTTPServer ref) =>
_env.out.print("Listening error")
fun closed(server: HTTPServer ref) =>
_env.out.print("Server closed")
class ConnectionHandler is HandlerFactory
let _env: Env
new iso create(env': Env) =>
_env = env'
fun apply(session: HTTPSession tag): HTTPHandler ref^ =>
RequestHandler(_env, session)
class RequestHandler is HTTPHandler
let _env: Env
let _session: HTTPSession
new create(env': Env, session': HTTPSession) =>
_env = env'
_session = session'
fun apply(payload: Payload val): Any tag =>
_env.out.print("Same session? " + if payload.session is _session then "yes" else "no" end)
let reply = Payload.response(StatusOK)
reply.add_chunk("Hai\n")
// With this line it works fine
_session(consume reply)
// With this line it doesn’t
// payload.respond(consume reply)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment