Module jester

Types

Settings* = ref object
  staticDir*: string
  appName*: string
  mimes*: MimeDb
  port*: Port
  bindAddr*: string
  Source Edit
MatchType* = enum
  MRegex, MSpecial
  Source Edit
Request* = ref object
  params*: StringTableRef
  matches*: array[MaxSubpatterns, string] ## Matches if this is a regex
                                       ## pattern.
  body*: RequestBody           ## Body of the request, only for POST.
                   ## You're probably looking for ``formData``
                   ## instead.
  headers*: HttpHeaders        ## Headers received with the request.
                      ## Retrieving these is case insensitive.
  formData*: MultiPartMessage  ## Form data; only present for
                            ## multipart/form-data
  port*: int
  host*: string
  appName*: string             ## This is set by the user in ``run``, it is
                 ## overriden by the "SCRIPT_NAME" scgi
                 ## parameter.
  pathInfo*: string            ## This is ``.path`` without ``.appName``.
  secure*: bool
  path*: string                ## Path of request.
  cookies*: StringTableRef     ## Cookies from the browser.
  ip*: string                  ## IP address of the requesting client.
  reqMeth*: ReqMeth            ## Request method, eg. HttpGet, HttpPost
  settings*: Settings
Parameters from the pattern, but also the query string.   Source Edit
Response* = ref object
  client*: AsyncSocket
  data*: tuple[action: CallbackAction, code: HttpCode, headers: StringTableRef,
              content: string]
For raw mode.   Source Edit
ReqMeth* {.
pure
.} = enum HttpGet = "GET", HttpPost = "POST", HttpPut = "PUT", HttpDelete = "DELETE", HttpHead = "HEAD", HttpOptions = "OPTIONS", HttpTrace = "TRACE", HttpPatch = "PATCH"
  Source Edit
CallbackAction* = enum
  TCActionSend, TCActionRaw, TCActionPass, TCActionNothing
  Source Edit
MultiData* = OrderedTable[string, tuple[fields: StringTableRef, body: string]]
  Source Edit

Procs

proc toMultiData*(mp: MultiPartMessage): Future[MultiData] {.
async
.}
Converts mp into the old jester's   Source Edit
proc sendHeaders*(response: Response; status: HttpCode; headers: StringTableRef) {.
async
.}
Sends status and headers to the client socket immediately. The user is then able to send the content immediately to the client on the fly through the use of response.client.   Source Edit
proc sendHeaders*(response: Response; status: HttpCode): Future[void]
Sends status and Content-Type: text/html as the headers to the client socket immediately.   Source Edit
proc sendHeaders*(response: Response): Future[void]
Sends Http200 and Content-Type: text/html as the headers to the client socket immediately.   Source Edit
proc send*(response: Response; content: string) {.
async
.}
Sends content immediately to the client socket.   Source Edit
proc send*(response: Response; status: HttpCode; headers: StringTableRef;
          content: string): Future[void]
Sends out a HTTP response comprising of the status, headers and content specified. This is done immediately for greatest performance.   Source Edit
proc newSettings*(port = Port(5000); staticDir = getCurrentDir() / "public";
                 appName = ""; bindAddr = ""): Settings
  Source Edit
proc serveFuture*(match: proc (request: Request; response: Response): Future[bool] {.
gcsafe, closure
.}; settings: Settings = newSettings()): Future[void]

Starts the process of listening for incoming HTTP connections on the specified address and port.

When a request is made by a client the specified callback will be called.

This version of serve returns a Future that runs until the server encounters an error from which it cannot recover (for example, when attempting to bind on a port that is already taken), at which point the Future completes with an error.

  Source Edit
proc serve*(match: proc (request: Request; response: Response): Future[bool] {.
gcsafe, closure
.}; settings: Settings = newSettings()) {.
inline
.}

Starts the process of listening for incoming HTTP connections on the specified address and port.

When a request is made by a client the specified callback will be called.

  Source Edit
proc setStaticDir*(request: Request; dir: string)

Sets the directory in which Jester will look for static files. It is ./public by default.

The files will be served like so:

./public/css/style.css -> http://example.com/css/style.css

(./public is not included in the final URL)

  Source Edit
proc getStaticDir*(request: Request): string

Gets the directory in which Jester will look for static files.

./public by default.

  Source Edit
proc makeUri*(request: jester.Request; address = ""; absolute = true;
             addScriptName = true): string
Creates a URI based on the current request. If absolute is true it will add the scheme (Usually 'http://'), request.host and request.port. If addScriptName is true request.appName will be prepended before address.   Source Edit
proc daysForward*(days: int): TimeInfo
Returns a TimeInfo object referring to the current time plus days.   Source Edit
proc normalizeUri*(uri: string): string
Remove any trailing /.   Source Edit

Macros

macro routesFuture*(body: stmt): untyped {.
immediate
.}
This version of routes returns a Future that runs until the server encounters an error from which it cannot recover (for example, when attempting to bind on a port that is already taken), at which point the Future completes with an error.   Source Edit
macro settings*(body: stmt): stmt {.
immediate
.}
  Source Edit

Templates

template resp*(code: HttpCode; headers: openarray[tuple[key, value: string]];
              content: string): stmt
Sets (code, headers, content) as the response.   Source Edit
template resp*(content: string; contentType = "text/html;charset=utf-8"): stmt
Sets content as the response; Http200 as the status code and contentType as the Content-Type.   Source Edit
template resp*(code: HttpCode; content: string;
              contentType = "text/html;charset=utf-8"): stmt
Sets content as the response; code as the status code and contentType as the Content-Type.   Source Edit
template body*(): expr

Gets the body of the request.

Note: It's usually a better idea to use the resp templates.

  Source Edit
template headers*(): expr

Gets the headers of the request.

Note: It's usually a better idea to use the resp templates.

  Source Edit
template status*(): expr

Gets the status of the request.

Note: It's usually a better idea to use the resp templates.

  Source Edit
template redirect*(url: string): stmt
Redirects to url. Returns from this request handler immediately. Any set response headers are preserved for this request.   Source Edit
template pass*(): stmt

Skips this request handler.

If you want to stop this request from going further use halt.

  Source Edit
template cond*(condition: bool): stmt
If condition is False then pass will be called, i.e. this request handler will be skipped.   Source Edit
template halt*(code: HttpCode; headers: varargs[tuple[key, val: string]];
              content: string): stmt
Immediately replies with the specified request. This means any further code will not be executed after calling this template in the current route.   Source Edit
template halt*(): stmt
Halts the execution of this request immediately. Returns a 404. All previously set values are discarded.   Source Edit
template halt*(code: HttpCode): stmt
  Source Edit
template halt*(content: string): stmt
  Source Edit
template halt*(code: HttpCode; content: string): stmt
  Source Edit
template attachment*(filename = ""): stmt
Creates an attachment out of filename. Once the route exits, filename will be sent to the person making the request and web browsers will be hinted to open their Save As dialog box.   Source Edit
template `@`*(s: string): expr
Retrieves the parameter s from request.params. "" will be returned if parameter doesn't exist.   Source Edit
template uri*(address = ""; absolute = true; addScriptName = true): expr
Convenience template which can be used in a route.   Source Edit
template setCookie*(name, value: string; expires: TimeInfo): stmt
Creates a cookie which stores value under name.   Source Edit
template routes*(body: stmt): untyped {.
immediate
.}
  Source Edit