parent
901c5ded52
commit
5fd150b7f7
2 changed files with 8 additions and 7 deletions
|
|
@ -41,7 +41,7 @@ runnableExamples("-r:off"):
|
||||||
|
|
||||||
import asyncnet, asyncdispatch, parseutils, uri, strutils
|
import asyncnet, asyncdispatch, parseutils, uri, strutils
|
||||||
import httpcore
|
import httpcore
|
||||||
from nativesockets import getLocalAddr, AF_INET
|
from nativesockets import getLocalAddr, Domain, AF_INET, AF_INET6
|
||||||
import std/private/since
|
import std/private/since
|
||||||
|
|
||||||
export httpcore except parseHeader
|
export httpcore except parseHeader
|
||||||
|
|
@ -82,7 +82,7 @@ proc getPort*(self: AsyncHttpServer): Port {.since: (1, 5, 1).} =
|
||||||
server.listen(Port(0))
|
server.listen(Port(0))
|
||||||
assert server.getPort.uint16 > 0
|
assert server.getPort.uint16 > 0
|
||||||
server.close()
|
server.close()
|
||||||
result = getLocalAddr(self.socket.getFd, AF_INET)[1]
|
result = getLocalAddr(self.socket)[1]
|
||||||
|
|
||||||
proc newAsyncHttpServer*(reuseAddr = true, reusePort = false,
|
proc newAsyncHttpServer*(reuseAddr = true, reusePort = false,
|
||||||
maxBody = 8388608): AsyncHttpServer =
|
maxBody = 8388608): AsyncHttpServer =
|
||||||
|
|
@ -372,13 +372,13 @@ const
|
||||||
## This can be set on the command line during compilation
|
## This can be set on the command line during compilation
|
||||||
## via `-d:nimMaxDescriptorsFallback=N`
|
## via `-d:nimMaxDescriptorsFallback=N`
|
||||||
|
|
||||||
proc listen*(server: AsyncHttpServer; port: Port; address = "") =
|
proc listen*(server: AsyncHttpServer; port: Port; address = ""; domain = AF_INET) =
|
||||||
## Listen to the given port and address.
|
## Listen to the given port and address.
|
||||||
when declared(maxDescriptors):
|
when declared(maxDescriptors):
|
||||||
server.maxFDs = try: maxDescriptors() except: nimMaxDescriptorsFallback
|
server.maxFDs = try: maxDescriptors() except: nimMaxDescriptorsFallback
|
||||||
else:
|
else:
|
||||||
server.maxFDs = nimMaxDescriptorsFallback
|
server.maxFDs = nimMaxDescriptorsFallback
|
||||||
server.socket = newAsyncSocket()
|
server.socket = newAsyncSocket(domain)
|
||||||
if server.reuseAddr:
|
if server.reuseAddr:
|
||||||
server.socket.setSockOpt(OptReuseAddr, true)
|
server.socket.setSockOpt(OptReuseAddr, true)
|
||||||
if server.reusePort:
|
if server.reusePort:
|
||||||
|
|
@ -404,7 +404,8 @@ proc acceptRequest*(server: AsyncHttpServer,
|
||||||
proc serve*(server: AsyncHttpServer, port: Port,
|
proc serve*(server: AsyncHttpServer, port: Port,
|
||||||
callback: proc (request: Request): Future[void] {.closure, gcsafe.},
|
callback: proc (request: Request): Future[void] {.closure, gcsafe.},
|
||||||
address = "";
|
address = "";
|
||||||
assumedDescriptorsPerRequest = -1) {.async.} =
|
assumedDescriptorsPerRequest = -1;
|
||||||
|
domain = AF_INET) {.async.} =
|
||||||
## Starts the process of listening for incoming HTTP connections on the
|
## Starts the process of listening for incoming HTTP connections on the
|
||||||
## specified address and port.
|
## specified address and port.
|
||||||
##
|
##
|
||||||
|
|
@ -417,7 +418,7 @@ proc serve*(server: AsyncHttpServer, port: Port,
|
||||||
##
|
##
|
||||||
## You should prefer to call `acceptRequest` instead with a custom server
|
## You should prefer to call `acceptRequest` instead with a custom server
|
||||||
## loop so that you're in control over the error handling and logging.
|
## loop so that you're in control over the error handling and logging.
|
||||||
listen server, port, address
|
listen server, port, address, domain
|
||||||
while true:
|
while true:
|
||||||
if shouldAcceptRequest(server, assumedDescriptorsPerRequest):
|
if shouldAcceptRequest(server, assumedDescriptorsPerRequest):
|
||||||
var (address, client) = await server.socket.acceptAddr()
|
var (address, client) = await server.socket.acceptAddr()
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ tgcsafety.nim(31, 18) Error: type mismatch: got <AsyncHttpServer, Port, proc (re
|
||||||
but expected one of:
|
but expected one of:
|
||||||
proc serve(server: AsyncHttpServer; port: Port;
|
proc serve(server: AsyncHttpServer; port: Port;
|
||||||
callback: proc (request: Request): Future[void] {.closure, gcsafe.};
|
callback: proc (request: Request): Future[void] {.closure, gcsafe.};
|
||||||
address = ""; assumedDescriptorsPerRequest = -1): owned(
|
address = ""; assumedDescriptorsPerRequest = -1; domain = AF_INET): owned(
|
||||||
Future[void])
|
Future[void])
|
||||||
first type mismatch at position: 3
|
first type mismatch at position: 3
|
||||||
required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
|
required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue