src/prologue/core/context

Search:
Group by:
Source   Edit  

Types

AsyncEvent = proc (): Future[void] {.closure, ...gcsafe.}
Source   Edit  
BasePatternNode = object of RootObj
  isGreedy*: bool
  case kind*: PatternMatchingType
  of ptrnParam, ptrnText:
      value*: string

  of ptrnWildcard:
      nil

  
A token within a URL to be mapped. The URL is broken into 'knots' that make up a 'rope' (seq[BasePatternNode]) Source   Edit  
Context = ref object of RootObj
  request*: Request
  response*: Response
  handled*: bool
  session*: Session
  ctxData*: StringTableRef
  
Source   Edit  
ErrorHandler = proc (ctx: Context): Future[void] {.nimcall, ...gcsafe.}
Source   Edit  
ErrorHandlerTable = TableRef[HttpCode, ErrorHandler]
Source   Edit  
Event = object
  case async*: bool
  of true:
      asyncHandler*: AsyncEvent

  of false:
      syncHandler*: SyncEvent

  
startup or shutdown event which is executed once. Source   Edit  
GlobalScope = ref object
  router*: Router
  reversedRouter*: ReversedRouter
  reRouter*: ReRouter
  appData*: StringTableRef
  settings*: Settings
  ctxSettings*: CtxSettings
Contains global data passed to Context. Source   Edit  
HandlerAsync = proc (ctx: Context): Future[void] {.closure, ...gcsafe.}
Source   Edit  
Path = object
  route*: string
  httpMethod*: HttpMethod
Source   Edit  
PathHandler = ref object
  handler*: HandlerAsync
  middlewares*: seq[HandlerAsync]
Source   Edit  
PatternMatchingType = enum
  ptrnWildcard, ptrnParam, ptrnText
Kinds of elements that may appear in a mapping Source   Edit  
PatternNode = object of BasePatternNode
  case isLeaf*: bool
  of true:
      nil

  of false:
      children*: seq[PatternNode]

  case isTerminator*: bool
  of true:
      handler*: PathHandler

  of false:
      nil

  
A node within a routing tree, usually constructed from a BasePatternNode Source   Edit  
RePath = object
  route*: Regex
  httpMethod*: HttpMethod
Source   Edit  
ReRouter = ref object
  callable*: seq[(RePath, PathHandler)]
Source   Edit  
ReversedRouter = StringTableRef
Source   Edit  
Router = ref object
  data*: CritBitTree[PatternNode]
Container that holds HTTP mappings to handler functions Source   Edit  
SyncEvent = proc () {.closure, ...gcsafe.}
Source   Edit  
UploadFile = object
  filename*: string
  body*: string
Contains filename and body of a file uploaded by users. Source   Edit  

Procs

func abortExit(ctx: Context; code = Http401; body = "";
               headers = initResponseHeaders(); version = HttpVer11) {.inline,
    ...raises: [AbortError], tags: [], forbids: [].}
Aborts the program. It raises AbortError. Source   Edit  
proc addMiddlewares(ctx: Context; middleware: HandlerAsync) {.inline,
    ...raises: [], tags: [], forbids: [].}
Internal function. Do not use. Source   Edit  
proc addMiddlewares(ctx: Context; middleware: seq[HandlerAsync]) {.inline,
    ...raises: [], tags: [], forbids: [].}
Internal function. Do not use. Source   Edit  
proc attachment(ctx: Context; downloadName: string; charset = "utf-8") {.inline,
    ...raises: [ValueError], tags: [], forbids: [].}

attachment is used to specify the file which will be downloaded.

Params:

- ``downloadName``: The name of the file to be downloaded. If the
                    length of the name is zero, the function will return immediately.
- ``charset``: The Encoding of the file. ``utf-8`` is the default encoding.
Source   Edit  
proc default404Handler(ctx: Context): Future[void] {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc default500Handler(ctx: Context): Future[void] {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc defaultHandler(ctx: Context): Future[void] {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
Default handler with HttpCode 404. Source   Edit  
proc deleteCookie(ctx: Context; key: string; path = ""; domain = "") {.inline,
    ...raises: [KeyError], tags: [TimeEffect], forbids: [].}
Deletes Cookie from Response. Source   Edit  
proc execEvent(event: Event) {.inline, ...raises: [ValueError, Exception, OSError],
                               tags: [TimeEffect, RootEffect], forbids: [].}
Source   Edit  
func first(ctx: Context): bool {....raises: [], tags: [], forbids: [].}
Internal function. Do not use. Source   Edit  
proc first=(ctx: Context; first: bool) {....raises: [], tags: [], forbids: [].}
Internal function. Do not use. Source   Edit  
proc flash(ctx: Context; msgs: string; category = FlashLevel.Info) {.inline,
    ...raises: [], tags: [], forbids: [].}
Sets flash messages. Source   Edit  
proc flash(ctx: Context; msgs: string; category: string) {.inline, ...raises: [],
    tags: [], forbids: [].}
Sets flash messages. Source   Edit  
func getCookie(ctx: Context; key: string; default = ""): string {.inline,
    ...raises: [], tags: [], forbids: [].}
Gets the value of ctx.request.cookies[key] if key is in cookies. Otherwise, the default value will be returned. Source   Edit  
proc getFlashedMsg(ctx: Context; category: FlashLevel): Option[string] {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Source   Edit  
proc getFlashedMsg(ctx: Context; category: string): Option[string] {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Source   Edit  
proc getFlashedMsgs(ctx: Context): seq[string] {.inline, ...raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc getFlashedMsgsWithCategory(ctx: Context): seq[(string, string)] {.inline,
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
func getFormParams(ctx: Context; key: string; default = ""): string {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}

Gets the contents of the form if key exists. Otherwise default will be returned. If you need the filename of the form, use getUploadFile instead. Returns the default value (which is an empty string if unspecified) if the form param does not exist.

getFormParams handles both form-urlencoded and multipart/form-data.

Source   Edit  
func getFormParamsOption(ctx: Context; key: string): Option[string] {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}

Gets the contents of the form if key exists. If you need the filename of the form, use getUploadFile instead. Returns none(string) if the form param does not exist

getFormParams handles both form-urlencoded and multipart/form-data.

Source   Edit  
func getPathParams(ctx: Context; key: string): string {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Gets the route parameters(for example, "/hello/{name}"). Returns an empty string if the param does not exist. Source   Edit  
func getPathParams[T: BaseType](ctx: Context; key: string; default: T): T {.
    inline.}
Gets the route parameters(for example, "/hello/{name}"). Returns the default value if the param does not exist. Source   Edit  
func getPathParamsOption(ctx: Context; key: string): Option[string] {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Gets a route parameter(for example, "/hello/{name}"). Returns none(string) if the param does not exist. Source   Edit  
func getPostParams(ctx: Context; key: string; default = ""): string {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}

Gets a param from the HttpPost parameters. Returns the default value (which is an empty string if unspecified) if the param is not in the post parameters.

getPostParams only handles form-urlencoded types.

Source   Edit  
func getPostParamsOption(ctx: Context; key: string): Option[string] {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}

Gets a param from the HttpPost parameters. Returns none(string) if the request method is not HttpPost or the param is not in the post-parameters.

getPostParams only handles form-urlencoded types.

Source   Edit  
func getQueryParams(ctx: Context; key: string; default = ""): string {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Gets a param from the query strings(for example, "www.google.com/hello?name=12", name=12). Returns the default value if the param does not exist. Source   Edit  
func getQueryParamsOption(ctx: Context; key: string): Option[string] {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Gets a param from the query strings. Returns none(string) if the param does not exist. (for example, "www.google.com/hello?name=12", name=12). Source   Edit  
func getSettings(ctx: Context; key: string): JsonNode {.inline, ...raises: [],
    tags: [], forbids: [].}
Get settings from globalSetting. If key doesn't exist, nil will be returned. Source   Edit  
func getUploadFile(ctx: Context; name: string): UploadFile {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Gets the UploadFile from request. Source   Edit  
func gScope(ctx: Context): lent GlobalScope {....raises: [], tags: [], forbids: [].}
Source   Edit  
func incSize(ctx: Context; num = 1) {....raises: [], tags: [], forbids: [].}
Internal function. Do not use. Source   Edit  
proc init(ctx: Context; request: Request; response: Response;
          gScope: GlobalScope) {.inline, ...raises: [], tags: [], forbids: [].}
Source   Edit  
func initEvent(handler: AsyncEvent): Event {....raises: [], tags: [], forbids: [].}
Initializes a new asynchronous event. Source   Edit  
func initEvent(handler: SyncEvent): Event {....raises: [], tags: [], forbids: [].}
Initializes a new synchronous event. Source   Edit  
func initUploadFile(filename, body: string): UploadFile {....raises: [], tags: [],
    forbids: [].}
Initiates a UploadFile. Source   Edit  
func middlewares(ctx: Context): lent seq[HandlerAsync] {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc middlewares=(ctx: Context; middlewares: seq[HandlerAsync]) {....raises: [],
    tags: [], forbids: [].}
Internal function. Do not use. Source   Edit  
func newContextFrom(ctx: Context; src: Context) {....deprecated, raises: [],
    tags: [], forbids: [].}
Deprecated
Creates a new Context by moving object and sharing ref object. Source   Edit  
func newContextTo(ctx: Context; src: Context) {....deprecated, raises: [],
    tags: [], forbids: [].}
Deprecated
Creates a new Context by moving object and copying necessary attributes. Source   Edit  
proc newErrorHandlerTable(initialSize = defaultInitialSize): ErrorHandlerTable {.
    ...raises: [], tags: [], forbids: [].}
Creates a new error handler table. Source   Edit  
proc newErrorHandlerTable(pairs: openArray[(HttpCode, ErrorHandler)]): ErrorHandlerTable {.
    ...raises: [], tags: [], forbids: [].}
Creates a new error handler table. Source   Edit  
func newReversedRouter(): ReversedRouter {....raises: [], tags: [], forbids: [].}
Creates a new reversed router table. Source   Edit  
proc respond(ctx: Context): Future[void] {.inline, ...raises: [Exception],
    tags: [RootEffect], forbids: [].}
Sends response to the client generating from ctx.response. Source   Edit  
proc respond(ctx: Context; code: HttpCode; body: string): Future[void] {.inline,
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Sends response to the client generating from ctx.response. Source   Edit  
proc respond(ctx: Context; code: HttpCode; body: string;
             headers: ResponseHeaders): Future[void] {.inline,
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Sends response to the client generating from code, body and headers. Source   Edit  
proc save(uploadFile: UploadFile; dir: string; filename = "") {.inline,
    ...raises: [OSError, IOError], tags: [ReadDirEffect, WriteIOEffect],
    forbids: [].}
Saves the UploadFile to dir. Source   Edit  
proc send(ctx: Context; content: string): Future[void] {.inline,
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Sends content to the client. Source   Edit  
proc setCookie(ctx: Context; cookie: Cookie) {....raises: [KeyError], tags: [],
    forbids: [].}
Sets Cookie for Response. Source   Edit  
proc setCookie(ctx: Context; key, value: string; expires = "";
               maxAge: Option[int] = none(int); domain = ""; path = "";
               secure = false; httpOnly = false; sameSite = Lax) {.inline,
    ...raises: [KeyError], tags: [], forbids: [].}
Sets Cookie for Response. Source   Edit  
proc setCookie(ctx: Context; key, value: string; expires: DateTime | Time;
               maxAge: Option[int] = none(int); domain = ""; path = "";
               secure = false; httpOnly = false; sameSite = Lax) {.inline.}
Sets Cookie for Response. Source   Edit  
proc setResponse(ctx: Context; code: HttpCode; body = ""; version = HttpVer11) {.
    inline, ...raises: [], tags: [], forbids: [].}
Handy to make the response of ctx. Source   Edit  
proc setResponse(ctx: Context; response: Response) {.inline, ...raises: [],
    tags: [], forbids: [].}
Handy to make the response of ctx. Source   Edit  
func size(ctx: Context): int8 {....raises: [], tags: [], forbids: [].}
Internal function. Do not use. Source   Edit  
proc staticFileResponse(ctx: Context; filename, dir: string; mimetype = "";
                        downloadName = ""; charset = "utf-8"; bufSize = 40960;
                        headers = none(ResponseHeaders)): owned(Future[void]) {.
    ...stackTrace: false, raises: [Exception], tags: [ReadDirEffect, RootEffect],
    forbids: [].}
Returns static files response. The following middlewares processing will be discarded. Source   Edit  
func urlFor(ctx: Context; handler: string;
            parameters: openArray[(string, string)] = @[];
            queryParams: openArray[(string, string)] = @[]; usePlus = true;
            omitEq = true): string {.inline, ...raises: [KeyError, ValueError],
                                     tags: [], forbids: [].}

Returns the corresponding name of the handler.

Limitation: Only supports two forms of Route:

1. "/route/hello"
2. "/route/{parameter}/other
Source   Edit  

Methods

method extend(ctx: Context) {.base, ...gcsafe, inline, ...raises: [], tags: [],
                              forbids: [].}
Source   Edit