SHOW:
|
|
- or go back to the newest paste.
| 1 | #nosp.nim / main | |
| 2 | import nosppkg/async_server | |
| 3 | import asyncdispatch | |
| 4 | ||
| 5 | when isMainModule: | |
| 6 | - | let f = fileTable.hasKey(path) |
| 6 | + | asyncCheck serve() #from here |
| 7 | runForever() | |
| 8 | ||
| 9 | #async_server | |
| 10 | import asynchttpserver, asyncdispatch, strutils | |
| 11 | import templates/home as th | |
| 12 | import templates/static as ts | |
| 13 | ||
| 14 | proc parseRequest(req: Request) {.async.} =
| |
| 15 | if "css/" in req.url.path: | |
| 16 | await ts.serveStatic(req) #to here | |
| 17 | else: | |
| 18 | await req.respond(Http200, th.index()) | |
| 19 | ||
| 20 | proc serve*() {.async.} =
| |
| 21 | var server = newAsyncHttpServer() | |
| 22 | ||
| 23 | proc cb(req: Request) {.async.} =
| |
| 24 | await parseRequest(req) | |
| 25 | ||
| 26 | waitFor server.serve(Port(5000), cb) | |
| 27 | ||
| 28 | #static.min | |
| 29 | import asynchttpserver, asyncdispatch, strutils, os, tables | |
| 30 | ||
| 31 | var fileTable = initTable[string, string]() | |
| 32 | ||
| 33 | proc getFileCont(path: string): string = | |
| 34 | let f = fileTable.hasKey(path) #to here | |
| 35 | "sdflkj" | |
| 36 | ||
| 37 | proc serveStatic*(req: Request) {.async.} =
| |
| 38 | await req.respond(Http200, getFileCont(req.url.path)) | |
| 39 | ||
| 40 | # error | |
| 41 | ... C:\Users\sveri\IdeaProjects\nim\nosp\src\nosppkg\async_server.nim(12, 17) template/generic instantiation of `async` from here | |
| 42 | ... C:\Users\sveri\IdeaProjects\nim\nosp\src\nosppkg\async_server.nim(18, 17) Error: type mismatch: got <AsyncHttpServer, Port, proc (req: Request): Future[system.void]{.locks: <unknown>.}>
| |
| 43 | ... but expected one of: | |
| 44 | ... proc serve(server: AsyncHttpServer; port: Port; | |
| 45 | ... callback: proc (request: Request): Future[void] {.closure, gcsafe.};
| |
| 46 | ... address = ""): owned(Future[void]) | |
| 47 | ... first type mismatch at position: 3 | |
| 48 | ... required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
| |
| 49 | ... but expression 'cb' is of type: proc (req: Request): Future[system.void]{.locks: <unknown>.}
| |
| 50 | ... This expression is not GC-safe. Annotate the proc with {.gcsafe.} to get extended error information.
| |
| 51 | ... 1 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them | |
| 52 | ... expression: serve(server, Port(5000), cb) |