src/mummy

Source   Edit  

Types

Message = object
  kind*: MessageKind
  data*: string
Source   Edit  
MessageKind = enum
  TextMessage, BinaryMessage, Ping, Pong
Source   Edit  
Request = ptr RequestObj
Source   Edit  
RequestHandler = proc (request: Request) {....gcsafe.}
Source   Edit  
RequestObj = object
  httpVersion*: HttpVersion  ## HTTP version from the request line.
  httpMethod*: string        ## HTTP method from the request line.
  uri*: string               ## Raw URI from the HTTP request line.
  path*: string              ## Decoded request URI path.
  queryParams*: QueryParams  ## Decoded request query parameter key-value pairs.
  pathParams*: PathParams    ## Router named path parameter key-value pairs.
  headers*: HttpHeaders      ## HTTP headers key-value pairs.
  body*: string              ## Request body.
  remoteAddress*: string     ## Network address of the request sender.
  
Source   Edit  
Server = ptr ServerObj
Source   Edit  
WebSocket = object
  
Source   Edit  
WebSocketEvent = enum
  OpenEvent, MessageEvent, ErrorEvent, CloseEvent
Source   Edit  
WebSocketHandler = proc (websocket: WebSocket; event: WebSocketEvent;
                         message: Message) {....gcsafe.}
Source   Edit  

Procs

proc `$`(request: Request): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc `$`(websocket: WebSocket): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc close(server: Server) {....raises: [], gcsafe, tags: [RootEffect], forbids: [].}
Cleanly stops and deallocates the server. In-flight request handler calls will be allowed to finish. No additional handler calls will be dispatched even if they are queued. Source   Edit  
proc close(websocket: WebSocket) {....raises: [], gcsafe, tags: [RootEffect],
                                   forbids: [].}
Begins the WebSocket closing handshake. This does not discard previously queued messages before starting the closing handshake. The handshake will only begin after the queued messages are sent. Source   Edit  
proc newServer(handler: RequestHandler;
               websocketHandler: WebSocketHandler = nil;
               logHandler: LogHandler = nil;
               workerThreads = max(countProcessors() * 10, 1);
               maxHeadersLen = 8 * 1024; maxBodyLen = 1024 * 1024;
               maxMessageLen = 64 * 1024): Server {....raises: [MummyError],
    tags: [], forbids: [].}
Creates a new HTTP server. The request handler will be called for incoming HTTP requests. The WebSocket handler will be called for WebSocket events. Calls to the HTTP, WebSocket and log handlers are made from worker threads. WebSocket events are dispatched serially per connection. This means your WebSocket handler must return from a call before the next call will be dispatched for the same connection. Source   Edit  
proc respond(request: Request; statusCode: int;
             headers: sink HttpHeaders = emptyHttpHeaders();
             body: sink string = "") {....raises: [], gcsafe, tags: [RootEffect],
                                       forbids: [].}
Sends the response for the request. This should usually only be called once per request. Source   Edit  
proc responded(request: Request): bool {....raises: [], tags: [], forbids: [].}
Check if this request has been responded. Informational responses (1xx status codes) do not mark a request responded. Source   Edit  
proc send(websocket: WebSocket; data: sink string; kind = TextMessage) {.
    ...raises: [], gcsafe, tags: [RootEffect], forbids: [].}
Enqueues the message to be sent over the WebSocket connection. Source   Edit  
proc serve(server: Server; port: Port; address = "localhost") {.
    ...raises: [MummyError],
    tags: [WriteIOEffect, ReadIOEffect, RootEffect, TimeEffect], forbids: [].}
The server will serve on the address and port. The default address is localhost. Use "0.0.0.0" to make the server externally accessible (with caution). This call does not return unless server.close() is called from another thread. Source   Edit  
proc upgradeToWebSocket(request: Request): WebSocket {....raises: [MummyError],
    gcsafe, tags: [RootEffect], forbids: [].}
Upgrades the request to a WebSocket connection. You can immediately start calling send(). Future updates for this WebSocket will be calls to the websocketHandler provided to newServer. The first event will be onOpen. Note: if the client disconnects before receiving this upgrade response, no onOpen event will be received. Source   Edit  
proc waitUntilReady(server: Server; timeout: float = 10.0) {.
    ...raises: [MummyError], tags: [TimeEffect], forbids: [].}
This proc blocks until the server is ready to receive requests or the timeout has passed. The timeout is in floating point seconds. This is useful when writing tests, where you need to know the server is ready before you begin sending requests. If the server is already ready this returns immediately. Source   Edit