Hi,
I copy example code from
https://nim-lang.org/docs/asynchttpserver.html#basic-usage
import asynchttpserver, asyncdispatch
proc main {.async.} =
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
"Content-type": "text/plain; charset=utf-8"}
await req.respond(Http200, "Hello World", headers.newHttpHeaders())
server.listen Port(5555)
while true:
if server.shouldAcceptRequest(5):
var (address, client) = await server.socket.acceptAddr()
asyncCheck processClient(server, client, address, cb)
else:
poll()
asyncCheck main()
runForever()
If I compile it, return error:
examples/async_server.nim(13, 43) Error: undeclared field: 'socket' for type asynchttpserver.AsyncHttpServer [type declared in /home/domogled/.choosenim/toolchains/nim-#devel/lib/pure/asynchttpserver.nim(68, 3)]
Where is error? In example in documentation or in library?
thanks
P.
I did change library code in nim-#devel/lib/pure/asynchttpserver.nim(68, 3) set codket to "public"
AsyncHttpServer* = ref object
socket*: AsyncSocket # I add *
reuseAddr: bool
reusePort: bool
maxBody: int ## The maximum content-length that will be read for the body.
maxFDs: int
then I got this error
examples/async_server.nim(13, 50) Error: type mismatch: got <AsyncSocket>
but expected one of:
proc acceptAddr(socket: AsyncFD; flags = {SafeDisconn};
inheritable = defined(nimInheritHandles)): owned(
Future[tuple[address: string, client: AsyncFD]])
first type mismatch at position: 1
required type for socket: AsyncFD
but expression 'server.socket' is of type: AsyncSocket
expression: acceptAddr(server.socket)
Probably, the library was updated without an update to the documentation. If you look around the library code you'll find pieces of the example in there, inside the new API you're intended to use.
import asynchttpserver, asyncdispatch
proc main {.async.} =
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
"Content-type": "text/plain; charset=utf-8"}
await req.respond(Http200, "Hello World", headers.newHttpHeaders())
await server.serve(Port(5555), cb)
asyncCheck main()
runForever()
It seems to be in incomplete backport. The code example should read:
import asynchttpserver, asyncdispatch
proc main {.async.} =
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
"Content-type": "text/plain; charset=utf-8"}
await req.respond(Http200, "Hello World", headers.newHttpHeaders())
server.listen Port(5555)
while true:
if server.shouldAcceptRequest():
asyncCheck server.acceptRequest(cb)
else:
poll()
asyncCheck main()
runForever()
The devel example works with devel Nim, but still fails against 1.4.2
Thanks, I'll investigate and backport needed stuff to 1.4.x, so it can work again in 1.4.4