This is a fully typesafe version of the API. All responses should be done by returning like a normal function. For getting data, Context can be used by using context hooks is strongly encouraged
Types
AfterEachHandler = proc (ctx: Context; err: ref Exception) {.async, ...gcsafe.}
- Handler that runs after every request. If there is an error then the exception will not be nil Source Edit
BeforeEachHandler = AsyncHandler
- Handler that runs before every request. Source Edit
ThreadStartHook = proc () {.closure.}
- Hook that is called for each thread that is spawned when app starts. Source Edit
Procs
proc afterEach(mapp; handler: AfterEachHandler) {....raises: [], tags: [], forbids: [].}
- Handler that runs after every request Source Edit
proc beforeEach(mapp; handler: BeforeEachHandler) {....raises: [], tags: [], forbids: [].}
- Handler that runs before every request Source Edit
proc handle[E: Exception](mapp; err: typedesc[E]; handler: DispatchMethod[ref E, Context, Future[void]])
- Adds an exception handler to the app. This handler is then called whenever the exception is raised when handling a route Source Edit
proc onStart(mapp; handler: ThreadStartHook) {....raises: [], tags: [], forbids: [].}
- Add handler that only runs once when the server starts up Source Edit
proc onThreadStart(mapp; handler: ThreadStartHook) {....raises: [], tags: [], forbids: [].}
- Add handler that runs when a worker thread is spawned Source Edit
proc run(app: var App; port: int = 8080; threads: Natural = 0; bindAddr: string = "0.0.0.0") {....gcsafe, raises: [Exception, OSError, ValueError, KeyError], tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect], forbids: [].}
- Starts the server, should be called after you have added all your routes Source Edit
proc runAsync(app: var App; port: int = 8080; threads: Natural = 0; bindAddr: string = "0.0.0.0"): Future[void] {....gcsafe, raises: [Exception, OSError, ResourceExhaustedError, ValueError], tags: [RootEffect], forbids: [].}
- Starts the server in the background, useful for spawning a test server or integration with other async procs Source Edit
Templates
template afterConnect(mapp`gensym110; path`gensym110: static[string]; handler`gensym110: proc)
- Source Edit
template afterDelete(mapp`gensym107; path`gensym107: static[string]; handler`gensym107: proc)
- Source Edit
template afterOptions(mapp`gensym109; path`gensym109: static[string]; handler`gensym109: proc)
- Source Edit
template afterPatch(mapp`gensym111; path`gensym111: static[string]; handler`gensym111: proc)
- Source Edit
template afterTrace(mapp`gensym108; path`gensym108: static[string]; handler`gensym108: proc)
- Source Edit
template beforeConnect(mapp`gensym92; path`gensym92: static[string]; handler`gensym92: proc)
- Source Edit
template beforeDelete(mapp`gensym89; path`gensym89: static[string]; handler`gensym89: proc)
- Source Edit
template beforeHead(mapp`gensym85; path`gensym85: static[string]; handler`gensym85: proc)
- Source Edit
template beforeOptions(mapp`gensym91; path`gensym91: static[string]; handler`gensym91: proc)
- Source Edit
template beforePatch(mapp`gensym93; path`gensym93: static[string]; handler`gensym93: proc)
- Source Edit
template beforePost(mapp`gensym87; path`gensym87: static[string]; handler`gensym87: proc)
- Source Edit
template beforeTrace(mapp`gensym90; path`gensym90: static[string]; handler`gensym90: proc)
- Source Edit
template map(mapp; verbs: set[HttpMethod]; path: static[string]; position: HandlerPos; handler: AsyncHandler)
- Optimised version of map that doesn't wrap the proc since its already an AsyncHandler Source Edit
template map[P: proc](mapp; verbs: set[HttpMethod]; path: static[string]; position: HandlerPos; handler: P)
- Low level function for adding a handler into the router. Handler gets transformed According to parameters/return Source Edit
template map[P: proc](mapp; verbs: set[HttpMethod]; path: static[string]; handler: P)
- Like map(mapp, verbs, path, position, handler) except it defaults to a normal handler Source Edit
Exports
-
fromRequest, Form, fromForm, queryFromRequest, formFromRequest, Query, sendResponse, HeaderTypes, fromForm, queryFromRequest, Json, queryFromRequest, PathParam, queryFromRequest, Header, useCtxHook, getCtxHook, cookieFromRequest, ContextHookHandler, getHeaderVal, fromForm, formFromRequest, getPathValue, Data, getHeaderVal, BasicType, sendResponse, sendResponse, sendResponse, Cookie, getHeaderHook, getPathValue, cookieFromRequest