From d9db57b4847684957fc3687b361d2a2855d0aa8d Mon Sep 17 00:00:00 2001 From: Haili Zhang Date: Fri, 27 May 2022 21:00:34 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(knative/node):=20add=20http=20?= =?UTF-8?q?trigger=20sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Haili Zhang --- .../http-trigger.yaml | 57 +++++++++++++++++++ .../with-output-binding-node/index.mjs | 12 ++++ .../with-output-binding-node/package.json | 9 +++ 3 files changed, 78 insertions(+) create mode 100644 functions/knative/with-output-binding-node/http-trigger.yaml create mode 100644 functions/knative/with-output-binding-node/index.mjs create mode 100644 functions/knative/with-output-binding-node/package.json diff --git a/functions/knative/with-output-binding-node/http-trigger.yaml b/functions/knative/with-output-binding-node/http-trigger.yaml new file mode 100644 index 0000000..e5aa9e6 --- /dev/null +++ b/functions/knative/with-output-binding-node/http-trigger.yaml @@ -0,0 +1,57 @@ +apiVersion: core.openfunction.io/v1beta1 +kind: Function +metadata: + name: sample-node-http-trigger +spec: + version: v2.0.0 + image: your-docker-registry/image:tag + # imageCredentials: + # name: your-docker-registry-secret + # build: + # builder: openfunction/builder-node:v2-16.13 + # env: + # FUNC_NAME: tryKnative + # srcRepo: + # url: https://github.com/OpenFunction/samples.git + # sourceSubPath: functions/async/mqtt-io-node + # revision: main + # app port default to "8080" + # port: 8080 + serving: + # default to knative + runtime: knative + scaleOptions: + minReplicas: 0 + maxReplicas: 2 + annotations: + # Dapr annotations: https://docs.dapr.io/reference/arguments-annotations-overview/ + # default to "info" + dapr.io/log-level: debug + # default to "true" + dapr.io/log-as-json: 'false' + template: + containers: + - name: function + imagePullPolicy: Always + params: + # default to FUNC_NAME value + FUNCTION_TARGET: tryKnativeAsync + # default to http + FUNCTION_SIGNATURE_TYPE: openfunction + DEBUG: common:*,ofn:* + outputs: + - name: mqtt-output + component: mqtt-out + operation: create + bindings: + # Dapr MQTT Binding: https://docs.dapr.io/reference/components-reference/supported-bindings/mqtt/ + mqtt-out: + type: bindings.mqtt + version: v1 + metadata: + - name: consumerID + value: '{uuid}' + - name: url + value: tcp://admin:public@emqx:1883 + - name: topic + value: out diff --git a/functions/knative/with-output-binding-node/index.mjs b/functions/knative/with-output-binding-node/index.mjs new file mode 100644 index 0000000..71d427e --- /dev/null +++ b/functions/knative/with-output-binding-node/index.mjs @@ -0,0 +1,12 @@ +export const tryKnative = (req, res) => { + res.send(`Hello, ${req.query.u || 'World'}!`); +}; + +export const tryKnativeAsync = async (ctx, data) => { + console.log('Data received: %o', data); + await ctx.send(data); + + // Optional to send ANY data back as HTTP response + // Request data is also accessible via "ctx.req" + // ctx.res.send(ctx.req.query); +}; diff --git a/functions/knative/with-output-binding-node/package.json b/functions/knative/with-output-binding-node/package.json new file mode 100644 index 0000000..1cdbad2 --- /dev/null +++ b/functions/knative/with-output-binding-node/package.json @@ -0,0 +1,9 @@ +{ + "main": "index.mjs", + "scripts": { + "start": "functions-framework --target=tryKnative" + }, + "dependencies": { + "@openfunction/functions-framework": "^0.5.0" + } +}