yzeng25 commented on code in PR #1340: URL: https://github.com/apache/apisix-website/pull/1340#discussion_r979413034
########## blog/en/blog/2022/09/28/rust-loves-apisix.md: ########## @@ -0,0 +1,198 @@ +--- +title: "Apache APISIX loves Rust!" +authors: + - name: Nicolas Fränkel + title: Author + url: https://github.com/nfrankel + image_url: https://avatars.githubusercontent.com/u/752258 +keywords: +- API gateway +- Apache APISIX +- Rust +- WebAssembly +description: This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. +tags: [Case Studies] +--- + +> This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. + +<!--truncate--> + +<head> + <link rel="canonical" href="https://blog.frankel.ch/rust-apisix/1/" /> +</head> + +Apache APISIX is built upon the shoulders of two giants: + +* [NGINX](https://www.nginx.com/), a widespread Open Source reverse-proxy +* [OpenResty](https://openresty.org/en/), a platform that allows scripting NGINX with the [Lua](https://www.lua.org/) programming language via [LuaJIT](https://luajit.org/) + +This approach allows APISIX to provide out-of-the-box Lua plugins that should fit most business requirements. But it always comes a time when generic plugins don't fit your requirements. In this case, you can write your own Lua plugin. + +However, if Lua is not part of your tech stack, [diving into a new ecosystem is a considerable investment](https://blog.frankel.ch/on-learning-new-programming-language/). Therefore, Apache APISIX offers developers to write plugins in several other languages. In this post, I'd like to highlight how to write such a plugin with Rust. + +## A bit of context + +Before I dive into the "how", let me first describe a bit of context surrounding the Rust integration in Apache APISIX. I believe it's a good story because it highlights the power of Open Source. + +It starts with the Envoy proxy. + +>Envoy is an open source edge and service proxy, designed for cloud-native applications +> +> -- https://www.envoyproxy.io/ + +Around 2019, Envoy's developers realized a simple truth. Since Envoy is a statically compiled binary, integrators who need to extend it must compile it from the modified source instead of using the official binary version. Issues range from supply chains more vulnerable to attacks to a longer drift when a new version is released. For end-users, whose core business is much further, it means having to hire specialized skills for this reason only. + +The team considered to solve the issue with C++ extensions, but discarded this approach as neither <abbr title="Application Programmer Interface">API</abbr>s nor <abbr title="Application Binary Interface">ABI</abbr>s were stable. Instead, they chose to provide a stable WebAssembly-based ABI. If you're interested in a more detailed background, you can read the whole piece [on GitHub](https://github.com/proxy-wasm/spec/blob/master/docs/WebAssembly-in-Envoy.md). + +The specification is available on [GitHub](https://github.com/proxy-wasm/spec). + +* Developers can create SDK for their tech stack +* Proxy and API Gateway providers can integrate `proxy-wasm` in their product + +## Apache APISIX and proxy-wasm + +The Apache APISIX project decided to integrate `proxy-wasm` into the product to benefit from the standardization effort. It also allows end-users to start with Envoy, or any other `proxy-wasm`-compatible reverse proxy, to migrate to Apache APISIX when necessary. + +APISIX doesn't implement `proxy-wasm` but integrates [wasm-nginx-module](https://github.com/api7/wasm-nginx-module). It's an Apache v2-licensed project provided by [api7.ai](https://api7.ai/), one of the main contributors to Apache APISIX. As its name implies, integration is done at the NGINX level. + + + +## Let's code! + +Now that we have explained how everything fits together, it's time to code. + +### Preparing Rust for WebAssembly + +Before developing the first line of code, we need to give Rust <abbr title="WebAssembly">WASM</abbr> compilation capabilities. + +```bash +rustup target add wasm32-wasi +``` + +It allows the Rust compiler to output WASM code: + +```bash +cargo build --target wasm32-wasi +``` + +The WASM code is found in: + +* `target/wasm32-wasi/debug/sample.wasm` +* `target/wasm32-wasi/release/sample.wasm` (when compiled with the `--release` flag) + +### Setting up the project + +The setup of the project is pretty straightforward: + +```bash +cargo new sample --lib +``` + +The command creates a `lib` project with the expected structure. + +### The code itself + +Let me first say that the available documentation is pretty sparse. For example, `proxy-wasm`'s is limited to the [methods' signature](https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT) (think JavaDocs). Rust SDK is sample-based. However, one can get some information from the [C++ SDK](https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/blob/master/docs/wasm_filter.md). + +>WASM module is running in a stack-based virtual machine and its memory is isolated from the host environment. All interactions between host and WASM module are through functions and callbacks wrapped by context object. +> +>At bootstrap time, a root context is created. The root context has the same lifetime as the VM/runtime instance and acts as a target for any interactions which happen at initial setup. It is also used for interactions that outlive a request. +> +>At request time, a context with incremental is created for each stream. Stream context has the same lifetime as the stream itself and acts as a target for interactions that are local to that stream. + +The Rust code maps to the same abstractions. + + Review Comment: ditto, Tencent CDN? cc @juzhiyuan ```suggestion  ``` ########## blog/en/blog/2022/09/28/rust-loves-apisix.md: ########## @@ -0,0 +1,198 @@ +--- +title: "Apache APISIX loves Rust!" +authors: + - name: Nicolas Fränkel + title: Author + url: https://github.com/nfrankel + image_url: https://avatars.githubusercontent.com/u/752258 +keywords: +- API gateway +- Apache APISIX +- Rust +- WebAssembly +description: This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. +tags: [Case Studies] +--- + +> This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. + +<!--truncate--> + +<head> + <link rel="canonical" href="https://blog.frankel.ch/rust-apisix/1/" /> +</head> + +Apache APISIX is built upon the shoulders of two giants: + +* [NGINX](https://www.nginx.com/), a widespread Open Source reverse-proxy +* [OpenResty](https://openresty.org/en/), a platform that allows scripting NGINX with the [Lua](https://www.lua.org/) programming language via [LuaJIT](https://luajit.org/) + +This approach allows APISIX to provide out-of-the-box Lua plugins that should fit most business requirements. But it always comes a time when generic plugins don't fit your requirements. In this case, you can write your own Lua plugin. + +However, if Lua is not part of your tech stack, [diving into a new ecosystem is a considerable investment](https://blog.frankel.ch/on-learning-new-programming-language/). Therefore, Apache APISIX offers developers to write plugins in several other languages. In this post, I'd like to highlight how to write such a plugin with Rust. + +## A bit of context + +Before I dive into the "how", let me first describe a bit of context surrounding the Rust integration in Apache APISIX. I believe it's a good story because it highlights the power of Open Source. + +It starts with the Envoy proxy. + +>Envoy is an open source edge and service proxy, designed for cloud-native applications Review Comment: I think we need a space here to show properly. ```suggestion > Envoy is an open source edge and service proxy, designed for cloud-native applications ``` ########## blog/en/blog/2022/09/28/rust-loves-apisix.md: ########## @@ -0,0 +1,198 @@ +--- +title: "Apache APISIX loves Rust!" +authors: + - name: Nicolas Fränkel + title: Author + url: https://github.com/nfrankel + image_url: https://avatars.githubusercontent.com/u/752258 +keywords: +- API gateway +- Apache APISIX +- Rust +- WebAssembly +description: This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. +tags: [Case Studies] +--- + +> This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. + +<!--truncate--> + +<head> + <link rel="canonical" href="https://blog.frankel.ch/rust-apisix/1/" /> +</head> + +Apache APISIX is built upon the shoulders of two giants: + +* [NGINX](https://www.nginx.com/), a widespread Open Source reverse-proxy +* [OpenResty](https://openresty.org/en/), a platform that allows scripting NGINX with the [Lua](https://www.lua.org/) programming language via [LuaJIT](https://luajit.org/) + +This approach allows APISIX to provide out-of-the-box Lua plugins that should fit most business requirements. But it always comes a time when generic plugins don't fit your requirements. In this case, you can write your own Lua plugin. + +However, if Lua is not part of your tech stack, [diving into a new ecosystem is a considerable investment](https://blog.frankel.ch/on-learning-new-programming-language/). Therefore, Apache APISIX offers developers to write plugins in several other languages. In this post, I'd like to highlight how to write such a plugin with Rust. + +## A bit of context + +Before I dive into the "how", let me first describe a bit of context surrounding the Rust integration in Apache APISIX. I believe it's a good story because it highlights the power of Open Source. + +It starts with the Envoy proxy. + +>Envoy is an open source edge and service proxy, designed for cloud-native applications +> +> -- https://www.envoyproxy.io/ + +Around 2019, Envoy's developers realized a simple truth. Since Envoy is a statically compiled binary, integrators who need to extend it must compile it from the modified source instead of using the official binary version. Issues range from supply chains more vulnerable to attacks to a longer drift when a new version is released. For end-users, whose core business is much further, it means having to hire specialized skills for this reason only. + +The team considered to solve the issue with C++ extensions, but discarded this approach as neither <abbr title="Application Programmer Interface">API</abbr>s nor <abbr title="Application Binary Interface">ABI</abbr>s were stable. Instead, they chose to provide a stable WebAssembly-based ABI. If you're interested in a more detailed background, you can read the whole piece [on GitHub](https://github.com/proxy-wasm/spec/blob/master/docs/WebAssembly-in-Envoy.md). + +The specification is available on [GitHub](https://github.com/proxy-wasm/spec). + +* Developers can create SDK for their tech stack +* Proxy and API Gateway providers can integrate `proxy-wasm` in their product + +## Apache APISIX and proxy-wasm + +The Apache APISIX project decided to integrate `proxy-wasm` into the product to benefit from the standardization effort. It also allows end-users to start with Envoy, or any other `proxy-wasm`-compatible reverse proxy, to migrate to Apache APISIX when necessary. + +APISIX doesn't implement `proxy-wasm` but integrates [wasm-nginx-module](https://github.com/api7/wasm-nginx-module). It's an Apache v2-licensed project provided by [api7.ai](https://api7.ai/), one of the main contributors to Apache APISIX. As its name implies, integration is done at the NGINX level. + + Review Comment: Probably need to covert to Tencent CDN, cc @juzhiyuan ########## blog/en/blog/2022/09/28/rust-loves-apisix.md: ########## @@ -0,0 +1,198 @@ +--- +title: "Apache APISIX loves Rust!" +authors: + - name: Nicolas Fränkel + title: Author + url: https://github.com/nfrankel + image_url: https://avatars.githubusercontent.com/u/752258 +keywords: +- API gateway +- Apache APISIX +- Rust +- WebAssembly +description: This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. +tags: [Case Studies] +--- + +> This article shows the steps to perform to develop and deploy WebAssembly plugins from Rust. + +<!--truncate--> + +<head> + <link rel="canonical" href="https://blog.frankel.ch/rust-apisix/1/" /> +</head> + +Apache APISIX is built upon the shoulders of two giants: + +* [NGINX](https://www.nginx.com/), a widespread Open Source reverse-proxy +* [OpenResty](https://openresty.org/en/), a platform that allows scripting NGINX with the [Lua](https://www.lua.org/) programming language via [LuaJIT](https://luajit.org/) + +This approach allows APISIX to provide out-of-the-box Lua plugins that should fit most business requirements. But it always comes a time when generic plugins don't fit your requirements. In this case, you can write your own Lua plugin. + +However, if Lua is not part of your tech stack, [diving into a new ecosystem is a considerable investment](https://blog.frankel.ch/on-learning-new-programming-language/). Therefore, Apache APISIX offers developers to write plugins in several other languages. In this post, I'd like to highlight how to write such a plugin with Rust. + +## A bit of context + +Before I dive into the "how", let me first describe a bit of context surrounding the Rust integration in Apache APISIX. I believe it's a good story because it highlights the power of Open Source. + +It starts with the Envoy proxy. + +>Envoy is an open source edge and service proxy, designed for cloud-native applications +> +> -- https://www.envoyproxy.io/ + +Around 2019, Envoy's developers realized a simple truth. Since Envoy is a statically compiled binary, integrators who need to extend it must compile it from the modified source instead of using the official binary version. Issues range from supply chains more vulnerable to attacks to a longer drift when a new version is released. For end-users, whose core business is much further, it means having to hire specialized skills for this reason only. + +The team considered to solve the issue with C++ extensions, but discarded this approach as neither <abbr title="Application Programmer Interface">API</abbr>s nor <abbr title="Application Binary Interface">ABI</abbr>s were stable. Instead, they chose to provide a stable WebAssembly-based ABI. If you're interested in a more detailed background, you can read the whole piece [on GitHub](https://github.com/proxy-wasm/spec/blob/master/docs/WebAssembly-in-Envoy.md). + +The specification is available on [GitHub](https://github.com/proxy-wasm/spec). + +* Developers can create SDK for their tech stack +* Proxy and API Gateway providers can integrate `proxy-wasm` in their product + +## Apache APISIX and proxy-wasm + +The Apache APISIX project decided to integrate `proxy-wasm` into the product to benefit from the standardization effort. It also allows end-users to start with Envoy, or any other `proxy-wasm`-compatible reverse proxy, to migrate to Apache APISIX when necessary. + +APISIX doesn't implement `proxy-wasm` but integrates [wasm-nginx-module](https://github.com/api7/wasm-nginx-module). It's an Apache v2-licensed project provided by [api7.ai](https://api7.ai/), one of the main contributors to Apache APISIX. As its name implies, integration is done at the NGINX level. + + + +## Let's code! + +Now that we have explained how everything fits together, it's time to code. + +### Preparing Rust for WebAssembly + +Before developing the first line of code, we need to give Rust <abbr title="WebAssembly">WASM</abbr> compilation capabilities. + +```bash +rustup target add wasm32-wasi +``` + +It allows the Rust compiler to output WASM code: + +```bash +cargo build --target wasm32-wasi +``` + +The WASM code is found in: + +* `target/wasm32-wasi/debug/sample.wasm` +* `target/wasm32-wasi/release/sample.wasm` (when compiled with the `--release` flag) + +### Setting up the project + +The setup of the project is pretty straightforward: + +```bash +cargo new sample --lib +``` + +The command creates a `lib` project with the expected structure. + +### The code itself + +Let me first say that the available documentation is pretty sparse. For example, `proxy-wasm`'s is limited to the [methods' signature](https://github.com/proxy-wasm/spec/tree/master/abi-versions/vNEXT) (think JavaDocs). Rust SDK is sample-based. However, one can get some information from the [C++ SDK](https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/blob/master/docs/wasm_filter.md). + +>WASM module is running in a stack-based virtual machine and its memory is isolated from the host environment. All interactions between host and WASM module are through functions and callbacks wrapped by context object. +> +>At bootstrap time, a root context is created. The root context has the same lifetime as the VM/runtime instance and acts as a target for any interactions which happen at initial setup. It is also used for interactions that outlive a request. +> +>At request time, a context with incremental is created for each stream. Stream context has the same lifetime as the stream itself and acts as a target for interactions that are local to that stream. Review Comment: ditto ```suggestion > WASM module is running in a stack-based virtual machine and its memory is isolated from the host environment. All interactions between host and WASM module are through functions and callbacks wrapped by context object. > > At bootstrap time, a root context is created. The root context has the same lifetime as the VM/runtime instance and acts as a target for any interactions which happen at initial setup. It is also used for interactions that outlive a request. > > At request time, a context with incremental is created for each stream. Stream context has the same lifetime as the stream itself and acts as a target for interactions that are local to that stream. ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
