15个版本 (9个破坏性版本)
| 0.11.0 | 2019年11月12日 |
|---|---|
| 0.10.0 | 2019年7月15日 |
| 0.9.0 | 2019年6月11日 |
| 0.6.0 | 2019年3月19日 |
| 0.3.0 | 2018年11月27日 |
#66 in #developer
每月52次下载
61KB
1.5K SLoC
注意
⚠️
此项目不再由原作者积极维护。
鼓励进行分支或衍生作品!
Azure Functions for Rust
用于在Rust中实现Azure Functions的框架。
🚩 免责声明
此项目不是官方认可的Microsoft产品,也不是对Microsoft未来产品提供的任何支持的认可。Microsoft和Azure是Microsoft公司的注册商标。
如果您希望Azure Functions团队考虑支持Rust,请对此反馈项进行投票。
示例
一个简单的HTTP触发的Azure Function
use azure_functions::{
bindings::{HttpRequest, HttpResponse},
func,
};
#[func]
pub fn greet(req: HttpRequest) -> HttpResponse {
format!(
"Hello from Rust, {}!\n",
req.query_params().get("name").map_or("stranger", |x| x)
)
.into()
}
Azure Functions for Rust还支持异步函数。
use azure_functions::{
bindings::{HttpRequest, HttpResponse},
func,
};
use futures::future::ready;
#[func]
pub async fn greet_async(req: HttpRequest) -> HttpResponse {
// Use ready().await to simply demonstrate the async/await feature
ready(format!(
"Hello from Rust, {}!\n",
req.query_params().get("name").map_or("stranger", |x| x)
))
.await
.into()
}
有关更多信息,请参阅构建异步Azure Functions应用程序。
开始使用
目录
安装
需求
.NET Core SDK
需要.NET Core SDK以同步Azure Functions Host绑定扩展。
例如,使用Cosmos DB绑定需要为Azure Functions Host安装Microsoft.Azure.WebJobs.Extensions.CosmosDB扩展。
当应用程序初始化时,这会由Azure Functions for Rust自动完成。
按照 2.2 .NET Core SDK 的下载说明 安装适用于 Windows、macOS 或您使用的 Linux 发行版。
Azure Functions Core Tools
安装 Azure Functions Core Tools 的 2.0 或更高版本。(请参阅安装指南)
如果您使用的是 Windows,必须将 %ProgramFiles%\nodejs\node_modules\azure-functions-core-tools\bin(其中 func.exe 所在位置)添加到 PATH 环境变量中。
安装Azure Functions for Rust SDK
使用 cargo install 安装 Azure Functions for Rust SDK。
cargo install azure-functions-sdk
这将安装一个名为 func 的新 cargo 命令,可用于创建和运行新的 Azure Functions 应用程序。
创建新的Azure Functions应用程序
使用 cargo func new-app 命令创建新的 Azure Functions 应用程序。
cargo func new-app hello
这将在 ./hello 目录中创建一个新应用,并包含一个名为 functions 的模块,预期在其中放置导出的 Azure Functions。
添加简单的HTTP触发的应用程序
使用 cargo func new 命令创建一个名为 hello 的新 HTTP 触发 Azure Function。
cargo func new http -n hello
函数的源代码位于 src/functions/hello.rs。
构建Azure Functions应用程序
要构建您的 Azure Functions 应用程序,只需使用 cargo build。
cargo build
如果您使用的是 nightly 编译器,您可以在编译期间启用改进的错误消息。
将以下内容添加到您的 Cargo.toml 文件中。
[features]
unstable = ["azure-functions/unstable"]
构建您的应用程序
cargo build --features unstable
这使 Azure Functions for Rust 能够发出包含属性中错误位置的诊断消息。
构建异步Azure Functions应用程序
要支持异步 Azure Functions,请将以下内容添加到您的 Cargo.toml 文件中。
[dependencies]
futures-preview = "0.3.0-alpha.19"
然后构建
cargo build
运行Azure Functions应用程序
要构建和运行您的 Azure Functions 应用程序,请使用 cargo func run。
cargo func run
如果您需要启用 unstable 功能,请向 cargo 传递 --features 选项。
cargo func run -- --features unstable
cargo func run 命令将使用由 Azure Functions Core Tools 安装的 Azure Function Host 在本地构建和运行您的应用程序。
默认情况下,宿主将配置为监听 0.0.0.0:8080。
对于之前添加的 hello 函数,可以通过 https://:8080/api/hello 来调用。
调试Azure Functions应用程序
使用 Visual Studio Code 和 CodeLLDB 扩展是最容易调试 Azure Functions 应用程序的方法。
默认情况下,Azure Functions for Rust SDK 将在您运行 cargo func new-app 时创建一个 Visual Studio Code 启动配置。
这将为调试配置启用一个 Debug 启动配置,该配置将在将 lldb 调试器附加到 Rust 工作进程之前本地构建和运行您的应用程序。
部署Azure Functions应用程序
未来,将有一个 cargo func deploy 命令,可以直接将 Azure Functions 应用程序部署到 Azure。
在此期间,您必须手动构建并将 Docker 镜像推送到 Azure 可以访问的存储库。
注意:这需要至少 18.06 的 Docker,以便支持实验性的 BuildKit 支持。Docker
要启用 BuildKit 支持,请在运行 docker build 之前将 DOCKER_BUILDKIT 环境变量设置为 1。
首先使用 docker build -t $TAG_NAME .
docker build -t $TAG_NAME .
其中 $TAG_NAME 是要使用的标签名(例如,username/my-functions-app)。
使用 docker push 将镜像推送到 Azure Functions 可以访问的存储库。
docker push $TAG_NAME
在 Azure 中使用 "Linux (Preview)" 操作系统创建 "Function App (Classic)"。在 "发布" 设置下,选择 "Docker Image"。
在 "配置容器" 部分中,选择存储库并输入您推送的镜像。
就是这样!一旦 Azure 中的函数应用启动,您应该能够查看并运行这些函数。
Azure Functions绑定
Azure Functions 支持许多输入和输出绑定,这些绑定可以用在函数中。
在 C# 这样的语言中,参数可以用描述参数如何绑定的属性进行注释。
Rust 不支持参数上的属性,因此 #[binding] 属性应用于具有与参数标识符匹配名称的函数。属性的参数取决于绑定类型。
#[func] 属性用于将普通 Rust 函数转换为 Azure Function。它通过检查函数的参数和返回类型,并将它们自动映射到相应的绑定来实现。
当前支持的绑定列表
| Rust 类型 | Azure Functions 绑定 | 方向 | Vec<T> |
|---|---|---|---|
| Blob | 输入和输出 Blob | in, inout, out | No |
| BlobTrigger | Blob 触发器 | in, inout | No |
| CosmosDbDocument | 输入和输出 Cosmos DB 文档 | in, out | Yes |
| CosmosDbTrigger | Cosmos DB 触发器 | in | No |
| DurableActivityContext | 持久活动触发器 | in | No |
| DurableOrchestrationClient | 持久编排客户端 | in | No |
| DurableOrchestrationContext | 持久编排触发器 | in | No |
| EventGridEvent | 事件网格触发器 | in | No |
| EventHubMessage | 事件中心输出消息 | out | Yes |
| EventHubTrigger | 事件中心触发器 | in | No |
| GenericInput | 通用输入 | in | No |
| GenericOutput | 通用输出 | out | No |
| GenericTrigger | 通用触发器 | in | No |
| HttpRequest | HTTP 触发器 | in | No |
| HttpResponse | 输出 HTTP 响应 | out | No |
| QueueMessage | 输出队列消息 | out | Yes |
| QueueTrigger | 队列触发器 | in | No |
| SendGridMessage | SendGrid电子邮件消息 | out | Yes |
| ServiceBusMessage | 服务总线输出消息 | out | Yes |
| ServiceBusTrigger | 服务总线触发器 | in | No |
| SignalRConnectionInfo | SignalR连接信息 | in | No |
| SignalRGroupAction | SignalR群组操作 | out | Yes |
| SignalRMessage | SignalR消息 | out | Yes |
| 表 | 输入和输出表 | in, out | No |
| TimerInfo | 计时器触发器 | in | No |
| TwilioSmsMessage | Twilio短信消息输出 | out | Yes |
未来将实现更多绑定,包括支持从自定义绑定检索数据。
Rust中的绑定
Azure Functions for Rust会根据绑定在函数声明中的使用方式自动推断绑定的方向。
输入绑定
类型为 T 或 &T 的参数,其中 T 是触发器或输入绑定类型,被推断为具有 in 方向的绑定。
#[func]
...
pub fn example(..., blob: Blob) {
...
}
#[func]
...
pub fn example(..., blob: &Blob) {
...
}
此外,一些输入绑定类型支持类型为 Vec<T> 和 &Vec<T> 的参数,其中 T 是输入绑定类型
#[func]
...
pub fn example(..., documents: Vec<CosmosDbDocument>) {
...
}
以下输入绑定支持类型为 Vec<T>
CosmosDbDocument
输入-输出(inout)绑定
类型为 &mut T 的参数,其中 T 是支持 inout 方向的触发器或输入绑定类型,被推断为具有 inout 方向的绑定。
#[func]
...
pub fn example(..., blob: &mut Blob) {
...
}
注意:目前除了C#之外的语言,没有实现 inout 方向绑定。
有关Azure Functions Host的此问题的更多信息,请参阅此问题。
输出绑定
返回类型为 T 的函数,其中 T 是输出绑定类型,或输出绑定类型的元组,被推断为具有 out 方向的绑定。
#[func]
...
pub fn example(...) -> Blob {
...
}
函数还可以返回任何输出绑定类型 T 的 Option<T>;None 值将跳过输出值。
#[func]
...
pub fn example(...) -> Option<Blob> {
...
}
#[func]
...
pub fn example(...) -> (HttpResponse, Option<Blob>) {
...
}
此外,一些输出绑定类型支持返回 Vec<T>,其中 T 是输出绑定类型
#[func]
...
pub fn example(...) -> Vec<CosmosDbDocument>) {
...
}
以下输出绑定支持返回类型 Vec<T>
CosmosDbDocumentEventHubMessageQueueMessageSendGridMessageServiceBusMessageSignalRGroupActionSignalRMessageTwilioSmsMessage
对于返回单个输出绑定类型的函数,绑定具有特殊的名称 $return,并被视为函数的返回值。
对于返回输出绑定类型元组的函数,元组中的第一个值具有绑定名称 $return,并被视为函数的返回值。其余值具有绑定名称 output1、output2、...、output(N-1),其中 N 是元组中的类型数量,并仅被视为输出绑定。
单元元组 () 可用于元组中以“跳过”一个绑定
#[func]
...
pub fn example(...) -> ((), Blob) {
...
}
对于上述示例,没有 $return 绑定,Azure Function 不返回任何值。相反,使用一个名为 output1 的单个输出绑定。
贡献者
感谢这些优秀的人(emoji key)
彼得·胡恩 💻 🐛 📖 🤔 🚇 🚧 📦 👀 ⚠️ ✅ |
瑞安·莱维克 💻 🤔 🚧 👀 🚇 |
托马斯·埃克特 💻 🖋 🎨 📖 🤔 🚇 🚧 👀 ✅ |
R. 泰勒·克罗伊 💻 📖 👀 🤔 |
德尼斯·莫洛坎诺夫 💻 ⚠️ 🎨 🤔 |
斯科特·莱昂斯 💻 ⚠️ 🎨 🤔 |
Rohan Prinja 💻 |
Christian Brevik 🐛 💻 ✅ |
Danny Browning 💻 🚧 |
本项目遵循all-contributors规范。欢迎任何形式的贡献!
依赖项
~8–18MB
~233K SLoC