Lua 모듈 작성 및 사용

이 시나리오에서는 Lua 모듈을 작성하고 사용하는 방법을 설명합니다. 시나리오에는 두 개의 Lua 스크립트가 포함되며, 첫 번째는 모듈을 작성하고 두 번째는 작성된 모듈을 사용하는 방법을 보여 줍니다. 두 스크립트 모두 표준 HTTP 변환 규칙의 위치에 추가되어야 합니다. HTTP 변환 규칙을 저장하는 위치가 Lua 검색 경로에 추가되었습니다. 따라서 HTTP 변환 규칙 디렉토리에 포함된 Lua 스크립트는 다른 Lua 스크립트에 의해 찾을 수 있습니다.

모듈 스크립트

-- IBM Confidential 
-- PID 5725-V89 5725-V90 5737-F02
-- 
-- Copyright IBM Corp. 2022, 2022


-- This script is used to test the ability to create a Lua module.  It defines
-- the AddHeaderModule which contains a single function to add a HTTP header to
-- the response.

local AddHeaderModule = {}

function AddHeaderModule.add_header(name, value)
    HTTPResponse.setHeader(name, value)
end

return AddHeaderModule

변환 스크립트

-- IBM Confidential 
-- PID 5725-V89 5725-V90 5737-F02
-- 
-- Copyright IBM Corp. 2022, 2022

-- This script is used to test the ability to use a locally created Lua module.
-- It will 'require' the AddHeaderModule module, and use this module to add the
-- specified header to the response.

local module = require "AddHeaderModule"

module.add_header("test-rsp-header", "header-value")