1 个不稳定版本
| 0.1.0 | 2022年7月7日 |
|---|
#17 in #fastly
32KB
327 行
SpecTrust Fastly Worker 库
SpecTrust Fastly Compute@Edge 集成库。
此库提供了一个简单的接口,用于集成和配置 Spec Proxy 实例,并通过 Fastly Compute@Edge 工作员与其通信。
请参阅文档以获取示例和更多信息。
lib.rs:
SpecTrust Fastly Compute@Edge 集成库。
此库提供了一个简单的接口,用于集成和配置 Spec Proxy 实例,并通过 Fastly Compute@Edge 工作员与其通信。
在开始使用此库之前,了解 Fastly 工作员系统的一些组件将很有用。
使用我们的库被设计得相对简单。以下是我们库的集成示例。
use fastly::{Error, Request, Response};
use spectrust_fastly_worker::{spec_proxy_process, SpecConfiguration, SpecProxyMode};
#[fastly::main]
fn main(mut request: Request) -> Result<Response, Error> {
let config = SpecConfiguration::builder(
[
// you will only need a backend for your server if you're running in Listening mode
(
"www.example.com",
"example_origin",
),
// and a backend for the spec proxy server
(
"www.example.com.specprotected.com",
"example_spec_proxy_origin",
),
]
.into(),
)
.with_operating_mode(SpecProxyMode::Listening)
.build();
spec_proxy_process(request, &config)
}
此示例将 Spec Proxy 库设置为 SpecProxyMode::Listening 模式,这将向 Spec Proxy 发送每个传入请求的副本。此配置不会提供 Spec Proxy 的全部功能。如果您想以 SpecProxyMode::Inline 模式运行,请联系您的 SpecTrust 代表关于在 Spec Proxy 中启用此功能。
请注意,在撰写本文时,Fastly 后端不支持通配符,因此每个子域都需要自己的后端和 HashMap 中的条目。
如果您有任何疑问或评论,请联系您的 SpecTrust 代表。
运行时配置
请注意,在撰写本文时,目前没有足够的信息来确切说明 Fastly 字典是如何实现的。使用这些字典进行运行时配置可能会产生潜在的性能影响。 此文档 声称使用这些字典的读取时间为微秒级。
通过使用Fastly 字典,我们可以集成一个系统,允许在运行时动态配置 Spec Proxy 库!这是一个强大的功能,可以避免仅仅为了更改 Spec Proxy 库的设置而部署新的边缘工作版本。设置这个过程相对直接。您需要遵循这里的说明,这将为您创建一个 Fastly 服务字典,然后使用以下代码示例将字典与SpecConfigurationBuilder集成。
- Fastly Rust 文档
- 更多关于Fastly 字典的信息
use fastly::{ConfigStore, Error, Request, Response};
use spectrust_fastly_worker::{spec_proxy_process, SpecConfiguration, SpecProxyMode};
#[fastly::main]
fn main(mut request: Request) -> Result<Response, Error> {
let mut builder = SpecConfiguration::builder(
[
// you will only need a backend for your server if you're running in Listening mode
(
"www.example.com",
"example_origin",
),
// and a backend for the spec proxy server
(
"www.example.com.specprotected.com",
"example_spec_proxy_origin",
),
]
.into(),
);
// Attempt to load a Dictionary to configure runtime values, we don't like panicking
// so use the Result version, `try_open`
if let Ok(spec_proxy_config) = ConfigStore::try_open("SpecProxy") {
builder = match spec_proxy_config.try_get("disable_spec_proxy") {
// if our dictionary value is present and set to "true", disable Spec Proxy
Ok(Some(s)) => builder.with_disable_spec_proxy(s == "true"),
// otherwise, default to enable Spec Proxy
_ => builder.with_disable_spec_proxy(false),
};
builder = match spec_proxy_config.try_get("spec_proxy_mode") {
// if our dictionary value is present and set to "inline", use Inline mode
Ok(Some(s)) if s == "inline" => builder.with_operating_mode(SpecProxyMode::Inline),
// otherwise, default to Listening mode
_ => builder.with_operating_mode(SpecProxyMode::Listening),
};
builder = match spec_proxy_config.try_get("percentage_of_ips") {
// if our dictionary value is present and set to a valid number, set the percentage
// of IP traffic that's routed to Spec Proxy
Ok(Some(s)) => builder.with_percentage_of_ips(s.parse().unwrap_or(100)),
// otherwise, default to routing 100% of traffic to Spec Proxy
_ => builder.with_percentage_of_ips(100),
};
}
let config = builder.build();
spec_proxy_process(request, &config)
}
在这个例子中,我们在字典中使用三个键。使用disable_spec_proxy来配置是否完全禁用库,使用spec_proxy_mode来配置 Spec Proxy 的操作模式,以及使用percentage_of_ips来配置通过 Spec Proxy 库路由的 IP 百分比。
现在我们可以通过 Fastly 网络界面在运行时修改 Spec Proxy 库了!
请注意,这不需要重新启动 Spec Proxy、边缘工作或任何其他东西。更改通过在运行时更改字典中的运行时值来生效。
依赖项
~6MB
~142K SLoC