# Mu - http-server.mu

     1 # example program: a single-request HTTP server
     2 #   listen for connections from clients on a server socket
     3 #   when a connection occurs, transfer it to a session socket
     4 #   read from it using channels
     5 #   write to it directly
     6 #
     7 # After running it, navigate to localhost:8080. Your browser should display
     8 # "SUCCESS!" and the server will terminate after one connection.
     9
    10 def main [
    11   local-scope
    12   socket:num <- $open-server-socket 8080/port
    13   $print [Mu socket creation returned ], socket, 10/newline
    14   return-unless socket
    15   session:num <- $accept socket
    16   contents:&:source:char, sink:&:sink:char <- new-channel 30
    17   sink <- start-running receive-from-socket session, sink
    18   query:text <- drain contents
    19   $print [Done reading from socket.], 10/newline
    20   write-to-socket session, [HTTP/1.0 200 OK
    21 Content-type: text/plain
    22
    23 SUCCESS!
    24 ]
    25   $print 10/newline, [Wrote to and closing socket...], 10/newline
    26   session <- $close-socket session
    27   socket <- $close-socket socket
    28 ]