Multiple server ports and query parameters are easy, cookies have to be done mostly manually so far:
import asynchttpserver, asyncdispatch, uri, cookies, strtabs, math, tables, times
randomize()
var users = newTable[string, Time]()
proc server1(req: Request) {.async.} =
await req.respond(Http200, "Hello, World!")
proc server2(req: Request) {.async.} =
case $req.url # need to import uri for $
of "/authorize":
var headers = newStringTable()
let user = $random(1000)
headers["Set-Cookie"] = "user=" & user
users[user] = getTime()
await req.respond(Http200, "Authorized!", headers)
else:
let cookies = req.headers["Cookie"].parseCookies()
let user = cookies["user"]
if users.hasKey(user):
var content = "Hello user " & user & "\n"
content.add "You were authorized at " & $users[user] & "\n"
content.add "This is " & $req.url & "\n"
await req.respond(Http200, content)
else:
await req.respond(Http403, "Unauthorized user, authorize first on http://localhost:8081/authorize")
asyncCheck newAsyncHttpServer().serve(Port 8080, server1)
asyncCheck newAsyncHttpServer().serve(Port 8081, server2)
echo "Try visiting http://localhost:8080/ and http://localhost:8081/"
runForever()
There is a websockets library being worked on: https://github.com/onionhammer/onion-nimrod/tree/master/websockets
I don't see TLS for asynchttpserver right now, but it could probably be added. You could also just run it proxied behind nginx or another webserver of course.
I'm not sure what you mean by streaming, and haven't seen anything in that direction in Nim.
Hot code swapping is also a bit difficult. You would need to run your code in a dynamically linked library and swap that, while saving the state of everything. I don't think anyone has done that with Nim so far. There have been some approaches for other languages that may be applicable, like this: http://nullprogram.com/blog/2014/12/23/
@araq - Without a doubt! Good to hear straight from the horse's mouth!
@dom96 - Excellent! Quick question, does Jester support multipart uploads? Any code showing its usage?
@daimon - Any chance you could share your code for newbies like me?
@daimon - thank you, looking forward to it
@dom96 - btw is this site's source available somewhere?