11.11. 함수 개발 참조 가이드

OpenShift Serverless Functions는 기본 기능을 생성하는 데 사용할 수 있는 템플릿을 제공합니다. 템플릿은 기능 프로젝트 상용구를 시작하고 kn func 툴과 함께 사용하도록 준비합니다. 각 함수 템플릿은 특정 런타임에 맞게 조정되며 해당 규칙을 따릅니다. 템플릿을 사용하면 함수 프로젝트를 자동으로 시작할 수 있습니다.

다음 런타임에 대한 템플릿을 사용할 수 있습니다.

11.11.1. Node.js 컨텍스트 오브젝트 참조

context 오브젝트에는 함수 개발자가 액세스할 수 있는 여러 속성이 있습니다. 이러한 속성에 액세스하면 HTTP 요청에 대한 정보를 제공하고 클러스터 로그에 출력을 쓸 수 있습니다.

11.11.1.1. log

클러스터 로그에 출력을 작성하는 데 사용할 수 있는 로깅 오브젝트를 제공합니다. 로그는 Pino 로깅 API를 따릅니다.

로그 예

function handle(context) {
  context.log.info(“Processing customer”);
}

kn func invoke 명령을 사용하여 함수에 액세스할 수 있습니다.

명령 예

$ kn func invoke --target 'http://example.function.com'

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}

로그 수준을 fatal,error,warn,info,debug,trace 또는 silent 중 하나로 변경할 수 있습니다. 이렇게 하려면 config 명령을 사용하여 해당 값 중 하나를 환경 변수 FujiNC _LOG_LEVEL에 할당하여 logLevel 값을 변경합니다.

11.11.1.2. query

요청에 대한 쿼리 문자열을 키-값 쌍으로 반환합니다. 이러한 속성은 컨텍스트 오브젝트 자체에서도 확인할 수 있습니다.

예제 쿼리

function handle(context) {
  // Log the 'name' query parameter
  context.log.info(context.query.name);
  // Query parameters are also attached to the context
  context.log.info(context.name);
}

kn func invoke 명령을 사용하여 함수에 액세스할 수 있습니다.

명령 예

$ kn func invoke --target 'http://example.com?name=tiger'

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}

11.11.1.3. body

필요한 경우 요청 본문을 반환합니다. 요청 본문에 JSON 코드가 포함된 경우 속성을 직접 사용할 수 있도록 구문 분석됩니다.

본문의 예

function handle(context) {
  // log the incoming request body's 'hello' parameter
  context.log.info(context.body.hello);
}

curl 명령을 사용하여 이를 호출하여 함수에 액세스할 수 있습니다.

명령 예

$ kn func invoke -d '{"Hello": "world"}'

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}

11.11.1.4. headers

HTTP 요청 헤더를 오브젝트로 반환합니다.

헤더 예

function handle(context) {
  context.log.info(context.headers["custom-header"]);
}

kn func invoke 명령을 사용하여 함수에 액세스할 수 있습니다.

명령 예

$ kn func invoke --target 'http://example.function.com'

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}

11.11.1.5. HTTP 요청

method
HTTP 요청 메서드를 문자열로 반환합니다.
httpVersion
HTTP 버전을 문자열로 반환합니다.
httpVersionMajor
HTTP 주요 버전 번호를 문자열로 반환합니다.
httpVersionMinor
HTTP 마이너 버전 번호를 문자열로 반환합니다.

11.11.2. TypeScript 컨텍스트 오브젝트 참조

context 오브젝트에는 함수 개발자가 액세스할 수 있는 여러 속성이 있습니다. 이러한 속성에 액세스하면 들어오는 HTTP 요청에 대한 정보를 제공하고 클러스터 로그에 출력을 쓸 수 있습니다.

11.11.2.1. log

클러스터 로그에 출력을 작성하는 데 사용할 수 있는 로깅 오브젝트를 제공합니다. 로그는 Pino 로깅 API를 따릅니다.

로그 예

export function handle(context: Context): string {
    // log the incoming request body's 'hello' parameter
    if (context.body) {
      context.log.info((context.body as Record<string, string>).hello);
    } else {
      context.log.info('No data received');
    }
    return 'OK';
}

kn func invoke 명령을 사용하여 함수에 액세스할 수 있습니다.

명령 예

$ kn func invoke --target 'http://example.function.com'

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}

로그 수준을 fatal,error,warn,info,debug,trace 또는 silent 중 하나로 변경할 수 있습니다. 이렇게 하려면 config 명령을 사용하여 해당 값 중 하나를 환경 변수 FujiNC _LOG_LEVEL에 할당하여 logLevel 값을 변경합니다.

11.11.2.2. query

요청에 대한 쿼리 문자열을 키-값 쌍으로 반환합니다. 이러한 속성은 컨텍스트 오브젝트 자체에서도 확인할 수 있습니다.

예제 쿼리

export function handle(context: Context): string {
      // log the 'name' query parameter
    if (context.query) {
      context.log.info((context.query as Record<string, string>).name);
    } else {
      context.log.info('No data received');
    }
    return 'OK';
}

kn func invoke 명령을 사용하여 함수에 액세스할 수 있습니다.

명령 예

$ kn func invoke --target 'http://example.function.com' --data '{"name": "tiger"}'

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}

11.11.2.3. body

요청 본문(있는 경우)을 반환합니다. 요청 본문에 JSON 코드가 포함된 경우 속성을 직접 사용할 수 있도록 구문 분석됩니다.

본문의 예

export function handle(context: Context): string {
    // log the incoming request body's 'hello' parameter
    if (context.body) {
      context.log.info((context.body as Record<string, string>).hello);
    } else {
      context.log.info('No data received');
    }
    return 'OK';
}

kn func invoke 명령을 사용하여 함수에 액세스할 수 있습니다.

명령 예

$ kn func invoke --target 'http://example.function.com' --data '{"hello": "world"}'

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}

11.11.2.4. headers

HTTP 요청 헤더를 오브젝트로 반환합니다.

헤더 예

export function handle(context: Context): string {
    // log the incoming request body's 'hello' parameter
    if (context.body) {
      context.log.info((context.headers as Record<string, string>)['custom-header']);
    } else {
      context.log.info('No data received');
    }
    return 'OK';
}

curl 명령을 사용하여 이를 호출하여 함수에 액세스할 수 있습니다.

명령 예

$ curl -H'x-custom-header: some-value’' http://example.function.com

출력 예

{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}

11.11.2.5. HTTP 요청

method
HTTP 요청 메서드를 문자열로 반환합니다.
httpVersion
HTTP 버전을 문자열로 반환합니다.
httpVersionMajor
HTTP 주요 버전 번호를 문자열로 반환합니다.
httpVersionMinor
HTTP 마이너 버전 번호를 문자열로 반환합니다.