remove duplication in asynchttpserver examples (#16586)
* remove duplication in asynchttpserver examples * fixup * add comment showing how to run snippet locally
This commit is contained in:
parent
9f1dd5c00f
commit
3b2f94810e
1 changed files with 28 additions and 48 deletions
|
|
@ -13,34 +13,34 @@
|
||||||
## for testing applications locally. Because of this, when deploying your
|
## for testing applications locally. Because of this, when deploying your
|
||||||
## application in production you should use a reverse proxy (for example nginx)
|
## application in production you should use a reverse proxy (for example nginx)
|
||||||
## instead of allowing users to connect directly to this server.
|
## instead of allowing users to connect directly to this server.
|
||||||
##
|
|
||||||
## Example
|
runnableExamples:
|
||||||
## =======
|
# This example will create an HTTP server on port 8080. The server will
|
||||||
##
|
# respond to all requests with a `200 OK` response code and "Hello World"
|
||||||
## This example will create an HTTP server on port 8080. The server will
|
# as the response body. Run locally with:
|
||||||
## respond to all requests with a ``200 OK`` response code and "Hello World"
|
# `nim doc --doccmd:-d:nimAsynchttpserverEnableTest --lib:lib lib/pure/asynchttpserver.nim`
|
||||||
## as the response body.
|
import asyncdispatch
|
||||||
##
|
if defined(nimAsynchttpserverEnableTest):
|
||||||
## .. code-block:: Nim
|
proc main {.async.} =
|
||||||
##
|
const port = 8080
|
||||||
## import asynchttpserver, asyncdispatch
|
var server = newAsyncHttpServer()
|
||||||
##
|
proc cb(req: Request) {.async.} =
|
||||||
## proc main {.async.} =
|
echo (req.reqMethod, req.url, req.headers)
|
||||||
## var server = newAsyncHttpServer()
|
let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
|
||||||
## proc cb(req: Request) {.async.} =
|
"Content-type": "text/plain; charset=utf-8"}
|
||||||
## let headers = {"Date": "Tue, 29 Apr 2014 23:40:08 GMT",
|
await req.respond(Http200, "Hello World", headers.newHttpHeaders())
|
||||||
## "Content-type": "text/plain; charset=utf-8"}
|
|
||||||
## await req.respond(Http200, "Hello World", headers.newHttpHeaders())
|
echo "test this with: curl localhost:" & $port & "/"
|
||||||
##
|
server.listen Port(port)
|
||||||
## server.listen Port(8080)
|
while true:
|
||||||
## while true:
|
if server.shouldAcceptRequest():
|
||||||
## if server.shouldAcceptRequest():
|
await server.acceptRequest(cb)
|
||||||
## await server.acceptRequest(cb)
|
else:
|
||||||
## else:
|
# too many concurrent connections, `maxFDs` exceeded
|
||||||
## poll()
|
poll()
|
||||||
##
|
|
||||||
## asyncCheck main()
|
asyncCheck main()
|
||||||
## runForever()
|
runForever()
|
||||||
|
|
||||||
import asyncnet, asyncdispatch, parseutils, uri, strutils
|
import asyncnet, asyncdispatch, parseutils, uri, strutils
|
||||||
import httpcore
|
import httpcore
|
||||||
|
|
@ -368,23 +368,3 @@ proc serve*(server: AsyncHttpServer, port: Port,
|
||||||
proc close*(server: AsyncHttpServer) =
|
proc close*(server: AsyncHttpServer) =
|
||||||
## Terminates the async http server instance.
|
## Terminates the async http server instance.
|
||||||
server.socket.close()
|
server.socket.close()
|
||||||
|
|
||||||
when not defined(testing) and isMainModule:
|
|
||||||
proc main {.async.} =
|
|
||||||
var server = newAsyncHttpServer()
|
|
||||||
proc cb(req: Request) {.async.} =
|
|
||||||
#echo(req.reqMethod, " ", req.url)
|
|
||||||
#echo(req.headers)
|
|
||||||
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():
|
|
||||||
await server.acceptRequest(cb)
|
|
||||||
else:
|
|
||||||
poll()
|
|
||||||
|
|
||||||
asyncCheck main()
|
|
||||||
runForever()
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue