Skip to content

Instantly share code, notes, and snippets.

@cedricss
Created October 10, 2011 12:44
Show Gist options
  • Select an option

  • Save cedricss/1275216 to your computer and use it in GitHub Desktop.

Select an option

Save cedricss/1275216 to your computer and use it in GitHub Desktop.
OPA scheduler serving an http "Hello world" page.

How to run this example?

  • Clone https://github.com/MLstate/opalang
  • Install bld with make install-bld
  • In the opalang root directory, compile opa_scheduler.ml with bld opa_scheduler.native
  • Run it with _build/opa_scheduler.native
let _ =
let header =
"HTTP/1.1 200 OK\r\n\
Connection: close\r\n\
Expires: Fri, 31 Dec 2012 14:36:01 GMT\r\n\
Content-Length: 20\r\n\
Content-Type: text/html\r\n\
Server: OPA/7508\r\n\
Date: Fri, 31 Dec 2012 14:36:01 GMT\r\n\r\n"
in
let body = "<h1>Hello world</h1>" in
let r = Printf.sprintf "%s%s" header body in
let sched = Scheduler.make () in
let port_spec = Network.make_port_spec ~protocol:"HTTP" (Unix.inet_addr_of_string "0.0.0.0") 8080 in
let answer_hello conn =
Scheduler.write sched conn r (fun _ -> Scheduler.remove_connection sched conn);
in
let new_client _ conn =
Scheduler.read sched conn (fun (_, _) -> answer_hello conn)
in
let _ = Network.listen sched port_spec Network.Unsecured new_client in
Network.loop sched;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment