akane

Types

ServerRef = ref object
  port*: uint16
  address*: string
  server*: AsyncHttpServer

Vars

AKANE_DEBUG_MODE: bool = false
change it with newServer proc

Procs

proc newServer(address: string = "127.0.0.1"; port: uint16 = 5000; debug: bool = false): ServerRef {...}{.
    raises: [OSError, IOError], tags: [ReadDirEffect, WriteDirEffect].}

Creates a new ServerRef object.

Arguments:

  • address - server address, e.g. "127.0.0.1"
  • port - server port, e.g. 5000
  • debug - debug mode
proc loadtemplate(name: string; json: JsonNode = %*{}): Future[string] {...}{.inline,
    raises: [Exception, ValueError, FutureError], tags: [RootEffect].}

Loads HTML template from templates folder.

Arguments:

  • name - template's name, e.g. "index", "api", etc.
  • json - Json data, which replaces in the template.

Replaces:

  • $(key) -> value
  • if $(key) { ... } -> ... (if value is true)
proc parseQuery(request: Request): Future[JsonNode] {...}{.
    raises: [Exception, ValueError, FutureError], tags: [TimeEffect, RootEffect].}
Decodes query.
e.g.:
"a=5&b=10" -> {"a": "5", "b": "10"}

This also have debug output, if AKANE_DEBUG_MODE is true.

Macros

macro pages(server: ServerRef; body: untyped): untyped

This macro provides convenient page adding.

body should be StmtList. page type can be:

  • equals
  • startswith
  • endswith
  • regex
  • notfound - this page uses without URL argument.

server.pages:
equals("/home"):
echo url echo urlParams

macro answer(request, message: untyped; http_code = Http200): untyped
Responds from server with utf-8.
Translates to:
await request.respond(Http200, "<head><meta charset='utf-8'></head>" & message)
macro error(request, message: untyped; http_code = Http404): untyped
Responds from server with utf-8.
Translates to:
await request.respond(Http404, "<head><meta charset='utf-8'></head>" & message)
macro start(server: ServerRef): untyped
Starts server.