NIMのSinatraのようなWebフレームワーク。 Jesterは、NIMでWebアプリケーションを迅速に作成するためのDSLを提供しています。
# example.nim
import htmlgen
import jester
routes:
get " / " :
resp h1 ( " Hello world " )コンパイルして実行します:
cd tests/example
nim c -r example.nim
表示:LocalHost:5000
生産に展開する前に、リバースプロキシの後ろにアプリケーションを実行してください。このライブラリはまだHTTPセキュリティのエクスプロイトに対して強化されていないため、書かれたアプリケーションはパブリックインターネットに公開されるべきではありません。
routes:
get " / " :
# do something here.すべてのルートはroutesブロック内にある必要があります。
ルートは、宣言される順に実行されます。したがって、停止するときは注意してください。
ルートパスには、特別なパターンまたは静的な文字列が含まれる場合があります。特別なパターンは、シナトラのパターンとほぼ同じです。唯一の本当の違いは、 :の代わりに@の使用です。
get " /hello/@name " :
# This matches "/hello/fred" and "/hello/bob".
# In the route ``@"name"`` will be either "fred" or "bob".
# This can of course match any value which does not contain '/'.
resp " Hello " & @ " name "Jesterのパターンは現在もう少し制限されており、ワイルドカードパターンはありません。
「?」を使用できます。オプションのパスパーツを意味する文字。
get " /hello/@name? " :
# This will match what the previous code example matches but will also match
# "/hello/".
if @ " name " == " " :
resp " No name received :( "
else :
resp " Hello " & @ " name "この場合、主要な '/'オプションも作成したい場合があります。パターンを「/hello?@name?」に変更することでこれを行うことができます。 Jesterがオプションにならない場合、Jesterは「/Hello」と一致しないため有用です。
正規表現は、ルートパターンとしても使用できます。サブパターンキャプチャは、ルートが一致するときにrequest.matchesに配置されます。例えば:
get re " ^ /([0-9]{2}) .html$ " :
resp request.matches[ 0 ]これは、フォーム/15.htmlのURLと一致します。この場合request.matches[0] 15なります。
Jesterは条件をサポートしていますが、それらは単純なcondテンプレートに限定されています。
routes:
get " /@name " :
cond @ " name " == " daniel "
# ``cond`` will pass execution to the next matching route if @"name" is not
# "daniel".
resp " Correct, my name is daniel. "
get " /@name " :
# This will be the next route that is matched.
resp " No, that's not my name. " ルートボディにはすべて、暗黙のrequestオブジェクトがあります。このオブジェクトはjester.nimでドキュメントされており、ドキュメントはnim doc jester.nimを実行することで生成できます。
ルートから応答を返すことは、次の機能のいずれかを使用して行う必要があります。
resp機能の1つ。body 、 headers 、および/またはstatusを設定し、呼び出しをreturnことにより。redirectattachment関数もっとあるかもしれません。詳細については、Jester.nimのドキュメントをご覧ください。
routesマクロを使用したり、ルーティングを実行したりすることはできません。
これは、独自のmatch手順を書くことで行うことができます。これを行う方法の例については、example2をご覧ください。
デフォルトでは、Jesterは./publicで静的ファイルを探します。これはsetStaticDir関数を使用してオーバーリデンすることができます。ファイルは次のように提供されます:
./public/css/style.css- -> http://example.com/css/style.css
注:Jesterは、 othersが読みやすいファイルのみを提供します。 Unix/Linuxではchmod o+r ./public/css/style.cssでこれを確保できます。
Cookieは、 setCookie関数を使用して設定できます。
get " / " :
# Set a cookie "test:value" and make it expire in 5 days.
setCookie ( " test " , @ " value " , daysForward ( 5 ))その後、 Table[string, string]を返すrequest.cookies手順を使用してアクセスできます。
リクエストオブジェクトは、現在のリクエストに関するすべての情報を保持します。 request変数を使用してルートからアクセスできます。次のように定義されています。
Request * = ref object
params * : StringTableRef # # Parameters from the pattern, but also the
# # query string.
matches * : array [ MaxSubpatterns , string ] # # Matches if this is a regex
# # pattern.
body * : string # # Body of the request, only for POST.
# # You're probably looking for ``formData``
# # instead.
headers * : StringTableRef # # Headers received with the request.
# # Retrieving these is case insensitive.
formData * : MultiData # # 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.
query * : string # # Query string of request.
cookies * : StringTableRef # # Cookies from the browser.
ip * : string # # IP address of the requesting client.
reqMeth * : HttpMethod # # Request method, eg. HttpGet, HttpPost
settings * : Settings カスタムルーターを使用すると、独自の初期化コードを実行し、Asyncループを開始する前にJesterに動的設定を渡すことができます。
import asyncdispatch, jester, os, strutils
router myrouter:
get " / " :
resp " It's alive! "
proc main () =
let port = paramStr ( 1 ). parseInt (). Port
let settings = newSettings (port = port)
var jester = initJester (myrouter, settings = settings)
jester. serve ()
when isMainModule :
main ()このためのコードは、ここに示されているシナトラのコードにかなり似ています:http://help.github.com/post-receive-hooks/
import jester, json
routes:
post " / " :
var push = parseJson ( @ " payload " )
resp " I got some JSON: " & $ push