Mojo · Quickstart

Quickstart

1.0.0-rc.1 · 60202332

from ucall import HTTPStatus, Interface, Library, Request, Server

def hello(var request: Request) raises:
    request^.reply(HTTPStatus.OK, "Hello!".as_bytes())

def echo(var request: Request) raises:
    var body = List[Byte](capacity=len(request.body()))
    body.extend(request.body())  # a reply consumes the request its body borrows from
    request^.reply(HTTPStatus.OK, body)

def main() raises:
    var server = Server(Library(), [Interface("127.0.0.1:8545")])
    server.get["/hello", hello]()
    server.post["/echo", echo]()
    server.take_calls(0)  # until server.stop() from a handler or another thread

Library() opens the shared object once, and every server, client and response created through it keeps it mapped.