インストール

pip install nexom
前提: Nexom は WSGI ベースです。WSGI サーバ(例: gunicorn)と組み合わせて動かす想定です。

最短スタート

まずは「HTML を返すルート」を 1 本作るのが最速です。

ルート定義(例)

from nexom.web.path import Path, Pathlib
from nexom.web.response import HtmlResponse

def index(request, args):
    return HtmlResponse("<h1>Hello Nexom</h1>")

routing = Pathlib(
    Path("", index, "index"),
)

ポイント: Nexom の handler は (request, args) を受け取って Response(または HtmlResponse/JsonResponse)を返すのが基本です。

ルーティング

基本

from nexom.web.path import Path, Pathlib

routing = Pathlib(
    Path("", index, "index"),
    Path("doc/", docs, "docs"),
)

パスパラメータ({id})

user/{id} のように書くと、URL の該当部分が args に入ります。

from nexom.web.response import HtmlResponse

def user(request, args):
    return HtmlResponse(f"<p>User ID: {args['id']}</p>")

Path("user/{id}", user, "user")

静的ファイル(Static)

Static を使うと、ディレクトリ配下のファイルを配信できます。

from nexom.web.path import Static

Static("static/", "./static", "static_files")
  • /static/xxx./static/xxx にマップして配信します。
  • ディレクトリトラバーサル対策(../)はフレームワーク側でブロックする設計です。

レスポンス

HtmlResponse

from nexom.web.response import HtmlResponse

return HtmlResponse("<h1>OK</h1>")

JsonResponse

from nexom.web.response import JsonResponse

return JsonResponse({"ok": True, "message": "hello"})

低レベル Response(自由度重視)

高度なことをしたい場合は Response を直接使います。

from nexom.web.response import Response

# バイナリや任意 Content-Type を返す
return Response(
    b"binary",
    content_type="application/octet-stream",
)

リダイレクト

from nexom.web.response import Redirect

return Redirect("/to")

エラーレスポンス

標準のエラーページテンプレートから HTML を生成します。

from nexom.web.response import ErrorResponse

return ErrorResponse(404, "Not Found")
charset について: Nexom は str の body を返すときに charsetencode します。
HtmlResponse / JsonResponse は “短く書ける” 方向で、UTF-8 を前提に使える設計です。

テンプレート

Nexom のテンプレートは「最小機能で予測可能」に寄せた独自構文です。

変数展開

<h1>{{ title }}</h1>

継承(Extends)と差し込み(Insert)

<Extends base />
<Insert main>
  <h1>Hello</h1>
</Insert>

部品読み込み(Import)

<Import header />

Python 側から呼ぶ

from nexom.web.template import Templates

templates = Templates("./templates", "default", "document")

html = templates.default(title="Nexom")
return HtmlResponse(html)
設計方針: テンプレートにロジックを入れず、「HTML と差し込み」に寄せることで、コード側をシンプルに保つ思想です。

CLI

動作確認

python -m nexom test

サーバープロジェクト生成

python -m nexom build-server myapp

生成されるプロジェクトには以下が含まれます(構成はバージョンにより変わる場合があります)。

  • 最小のルーティング定義
  • テンプレート一式
  • 静的ファイル用ディレクトリ
  • gunicorn / config の雛形

運用のコツ

「短く書く層」と「脱出口」を分ける

  • 普段: HtmlResponse / JsonResponse を使う
  • 詰めたい: Response で headers / content-type を直接制御

ルーティングは “薄く”

handler 内で複雑化しそうなら、関数を分けて「処理の塊」を小さくするのが Nexom と相性良いです。

テストのおすすめ

公開運用を想定するなら、最低限この3つのテストを用意すると安心です。

  • Path の引数抽出
  • Static の traversal ブロック
  • Response の Content-Type / charset の期待値