Would somebody please be kind enough to show a complete beginner how to create an asyncHTTPServer?
When I look at the signature from the library document it seems confusing to me:
proc asyncHTTPServer*(handleRequest: proc (server: PAsyncHTTPServer;
client: TSocket; path, query: string): bool {.closure.}; port = TPort(80);
address = ""): PAsyncHTTPServer
The outer procedure returns a PAsyncHTTPServer but the inner procedure passed to it in the handleRequest argument expects to receive a PAsyncHTTPServer as one of its parameters, it seems like a chicken and egg to me.
Thanks.
Welcome to the forum!
In this case the handleRequest parameter is simply a closure. You can either pass a function to it, or a closure. E.g.
import httpserver, asyncio, sockets
proc handleRequest(server: PAsyncHTTPServer; client: TSocket; path, query: string): bool =
echo("Got request with path of: ", path)
var disp = newDispatcher()
var http = asyncHTTPServer(handleRequest, TPort(6666))
disp.register(http)
while true:
if not disp.poll():
echo("HTTP server has been closed.")
Best of luck!
Thanks very much for your help dom96.
I have to confess I had to Google 'closure' :-)