Dart OpenWhisk 函数模板
本仓库包含用于使用 Dart 编程语言编写无服务器函数的 OpenWhisk 函数模板。
用法
-
请确保 OpenWhisk 已部署到您的 Kubernetes 集群并已安装 OpenWhisk CLI 工具。有关如何执行此操作的简要介绍,请参阅 此处 和 此处。
-
从本仓库下载 Dart 函数模板并进入主目录
git clone https://github.com/nicklasxyz/dart_openwhisk && \
cd dart_openwhisk
- 为将要由 OpenWhisk 部署和管理的函数添加新功能
code template/function/lib/src/function_base.dart
# ... Inside this file extend or add whatever you want to below the 'handleRequest' function
注意:dart_openwhisk/template 目录中的文件和目录是普通 Dart 项目的一部分,但经过专门结构化以供 OpenWhisk 使用。所有新功能都应主要作为位于 dart_openwhisk/template/function/ 目录中的 function 包的一部分来实现。额外的依赖项应添加到 template 或 template/function 目录根目录中的相应 pubspec.yaml 文件中。项目可以像往常一样在本地编译和测试。有关更多信息,请参阅以下 README.md。
- 构建并推送函数
# Define your docker image name and function name below
export FUNC_NAME= # For example: test-function
export IMAGE_NAME= # For example: username/test-function:latest
# Then build and push the docker image to Docker Hub:
docker build template --tag $IMAGE_NAME && docker push $IMAGE_NAME
- 创建包并部署无服务器函数
wsk -i package create demo && \
wsk -i action create /guest/demo/$FUNC_NAME --docker $IMAGE_NAME --web true
# To remove function deployments run:
wsk -i action delete /guest/demo/$FUNC_NAME
- 等待几秒钟,然后我们可以通过发送 curl 请求来调用该函数
### Retrieve function invocation URL
export FUNC_URL=$(wsk -i action get /guest/demo/$FUNC_NAME --url | tail -1)
### Example GET request
curl -k \
-X GET $FUNC_URL; \
echo
# If nothing was changed in the 'dart_openwhisk/template/function'
# directory before deployment then we should just see the default response:
>> Hello from OpenWhisk & Dart!
### Example POST request:
curl -k \
-d "{\"name\": \"Peter\", \"age\": \"42\", \"height\": \"180.5\"}" \
-H "Content-Type: application/json" \
-X POST $FUNC_URL; \
echo
# If nothing was changed in the 'dart_openwhisk/template/function'
# directory before deployment then we should just see the default response:
>> [{"string_field":"Peter"}, {"int_field":42}, {"double_field":180.5}]
有关其他请求方法如何工作的更多信息,请参阅 OpenWhisk API 网关文档