コンテンツにスキップ

HTTPエンドポイント

エンドポイント(http.endpoint)はLua関数を実行するHTTPルートハンドラを定義します。

- name: get_user
kind: http.endpoint
meta:
router: app:api_router
method: GET
path: /users/{id}
func: app.users:get_user
フィールド必須説明
meta.routerregistry.IDいいえ親ルーター(ルーターが1つだけ登録されている場合はそれがデフォルト)
methodstringはいHTTPメソッド
pathstringはいURLパスパターン
funcregistry.IDはい実行する関数

サポートされるメソッド:

メソッドユースケース
GETリソースの取得
POSTリソースの作成
PUTリソースの置換
PATCH部分更新
DELETEリソースの削除
HEADヘッダーのみ
OPTIONSCORSプリフライト(自動処理)
TRACE診断ループバック

URLパラメータには{param}構文を使用:

- name: get_user
kind: http.endpoint
method: GET
path: /users/{id}
func: get_user
- name: get_user_post
kind: http.endpoint
method: GET
path: /users/{user_id}/posts/{post_id}
func: get_user_post

ハンドラでのアクセス:

local http = require("http")
local function handler()
local req = http.request()
local user_id = req:param("id")
local post_id = req:param("post_id")
end

{path...}で残りのパスをキャプチャ:

- name: file_handler
kind: http.endpoint
method: GET
path: /files/{path...}
func: serve_file
local function handler()
local req = http.request()
local file_path = req:param("path")
-- /files/docs/readme.md -> path = "docs/readme.md"
end

エンドポイント関数はhttpモジュールからリクエストとレスポンスオブジェクトを取得します:

local http = require("http")
local json = require("json")
local function handler()
local req = http.request()
local res = http.response()
-- リクエストを読み取り
local body = req:body()
local user_id = req:param("id")
local page = req:query("page")
local auth = req:header("Authorization")
-- 処理
local user = get_user(user_id)
-- レスポンスを書き込み
res:set_content_type(http.CONTENT.JSON)
res:set_status(http.STATUS.OK)
res:write_json(user)
end
return { handler = handler }
メソッド戻り値説明
req:method()stringHTTPメソッド
req:path()stringリクエストパス
req:param(name)stringURLパラメータ
req:params()tableすべてのパスパラメータ
req:query(name)stringクエリパラメータ
req:query_params()tableすべてのクエリパラメータ
req:header(name)stringリクエストヘッダー
req:body()stringリクエストボディ
req:body_json()table, errorJSONボディをパース
req:has_body()booleanボディの有無を確認
req:content_type()stringコンテンツタイプ
req:content_length()numberボディサイズ(バイト)
req:host()stringホスト名
req:remote_addr()stringクライアントIPアドレス
req:accepts(type)booleanコンテンツネゴシエーション
req:is_content_type(type)booleanコンテンツタイプを確認
req:stream()Stream大きなファイル用のストリームとしてボディを取得
req:parse_multipart(max?)table, errorマルチパートフォームをパース
メソッド説明
res:set_status(code)HTTPステータスコードを設定
res:set_header(name, value)レスポンスヘッダーを設定
res:set_content_type(type)コンテンツタイプを設定
res:write(data)生のボディを書き込み
res:write_json(data)JSONレスポンスを書き込み
res:write_event(data)SSEイベントを送信
res:set_transfer(encoding)転送モードを設定(SSE、chunked)
res:flush()レスポンスをクライアントにフラッシュ

JSON APIの一般的なパターン:

local http = require("http")
local function handler()
local req = http.request()
local res = http.response()
local data, err = req:body_json()
if err then
res:set_status(http.STATUS.BAD_REQUEST)
res:write_json({error = "Invalid JSON"})
return
end
local result = process(data)
res:set_status(http.STATUS.OK)
res:write_json(result)
end
return { handler = handler }
local http = require("http")
local function api_error(res, status, code, message)
res:set_status(status)
res:write_json({
error = {
code = code,
message = message
}
})
end
local function handler()
local req = http.request()
local res = http.response()
local user_id = req:param("id")
local user, err = db.get_user(user_id)
if err then
if errors.is(err, errors.NOT_FOUND) then
return api_error(res, http.STATUS.NOT_FOUND, "USER_NOT_FOUND", "User not found")
end
return api_error(res, http.STATUS.INTERNAL_ERROR, "INTERNAL_ERROR", "Server error")
end
res:set_status(http.STATUS.OK)
res:write_json(user)
end
return { handler = handler }
entries:
- name: users_router
kind: http.router
prefix: /api/users
middleware:
- cors
- compress
- name: list_users
kind: http.endpoint
meta:
router: users_router
method: GET
path: /
func: app.users:list
- name: get_user
kind: http.endpoint
meta:
router: users_router
method: GET
path: /{id}
func: app.users:get
- name: create_user
kind: http.endpoint
meta:
router: users_router
method: POST
path: /
func: app.users:create
- name: update_user
kind: http.endpoint
meta:
router: users_router
method: PUT
path: /{id}
func: app.users:update
- name: delete_user
kind: http.endpoint
meta:
router: users_router
method: DELETE
path: /{id}
func: app.users:delete
- name: admin_endpoint
kind: http.endpoint
meta:
router: admin_router
method: POST
path: /settings
func: app.admin:update_settings
post_middleware:
- endpoint_firewall
post_options:
endpoint_firewall.action: "admin"