How to convert any rust project to be compatible with spin deploy? #2534
-
If I were to convert any rust app to use spin i would be at least required to
Below are some of the steps i believe would be done... add the following in my Cargo.toml [package]
name = "your-app"
version = "0.1.0"
edition = "2021"
[lib]
# Required to have a `cdylib` (dynamic library) to produce a Wasm module.
crate-type = [ "cdylib" ]
[dependencies]
# Useful crate to handle errors.
anyhow = "1"
# The Spin SDK.
spin-sdk = { git = "https://github.com/fermyon/spin" } create a spin_manifest_version = 2
[application]
name = "app"
version = "0.1.0"
authors = ["Uriah <codeitlikemiley@gmail.com>"]
description = "" add a rust template using:
prompt you with the following:
the spin_manifest_version = 2
[application]
name = "app"
version = "0.1.0"
authors = ["Uriah <codeitlikemiley@gmail.com>"]
description = ""
[[trigger.http]]
route = "/api"
component = "api"
[component.api]
source = "ruber/target/wasm32-wasi/release/api.wasm"
allowed_outbound_hosts = []
[component.ruber.build]
command = "cargo build --target wasm32-wasi --release"
workdir = "api"
watch = ["src/**/*.rs", "Cargo.toml"] lib.rs having now used the
So to conclude this, this is whats need to be done from scratch if we you wanna use spin but like i am asking this doesnt convert any existing project to be compatible with deploy as an example a leptos app can be build to wasm but still i know there is a package built as wrapper on top of it
While on other example I saw Dioxus dont have any wrapper and uses which spark my attention to , that might be able to give me more clarity on my problem.
see here on this Cargo.tom being the stuff i mentioned required above is also being required here
BUT on their example code on their any import to spin-sdk and usage of the macro the that was added now to my mental clarity is adding see their lib.rs also their spin.toml is quite interesting
spin_manifest_version = 2
[application]
name = "fermyon-spin-dioxus-csr"
version = "0.1.0"
authors = ["ZombieKing <zombiekingcrypto@proton.me>"]
description = "Fermyon Spin + Dioxus - Client Side Rendering (CSR) template with Tailwind"
[[trigger.http]]
route = "/..."
component = "spin-dioxus-csr" # same as the name of the project name on Cargo.toml
[component.spin-dioxus-csr]
source = { url = "https://github.com/fermyon/spin-fileserver/releases/download/v0.2.1/spin_static_fs.wasm", digest = "sha256:5f05b15f0f7cd353d390bc5ebffec7fe25c6a6d7a05b9366c86dcb1a346e9f0f" }
files = [{ source = "pkg", destination = "/" }]
# set the fallback path so maybe Spin works well with Dioxus like it does with the Leptos Router
environment = { FALLBACK_PATH = "index.html" }
[component.spin-dioxus-csr.build]
command = "wasm-pack build --release --target web && cp static/index.html pkg && npx tailwindcss -i static/input.css -o pkg/tailwind.css"
watch = ["src/**/*.rs", "Cargo.toml"] here they havent used any LOCAL BUILD that has e.g. below
They used a certain build from this repo: the build link is which after some time learned this was generated using a command
I have tested spin deploy on this project and it deploy on fermyon cloud and show a static page well by the looks of that I think that even if we are not using I also find now another interesting example that uses https://github.com/fermyon/spin-fileserver/tree/main/examples/rust see the Cargo.toml [package]
name = "spin-fileserver-example"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[package.metadata.component]
package = "component:spin-fileserver-example"
adapter = "../../adapters/fd1e948d/wasi_snapshot_preview1.reactor.wasm"
[dependencies]
cargo-component-bindings = "0.4.0"
wit-bindgen = "0.13.0"
spin-sdk = { git = "https://github.com/fermyon/spin" }
futures = "0.3.28"
[workspace] This is quite interesting as they are using and a new search-paths:
- ../../target/wasm32-wasi/release
instantiations:
$input:
arguments:
wasi:http/incoming-handler@0.2.0-rc-2023-10-18: spin_static_fs and this block of code
Im wondering what does it do i read the wit-bindgen that using that , allows use to use any e.g. // wit/host.wit package example:host;
world host {
import print: func(msg: string);
export run: func();
} I have dugged so much info, but the end conclusion i have is One there is no way to deploy a rust project if it is not compiled to wasm and dont have any spin.toml and spin-sdk usage so the example on dioxus is not quite complete , and missing usage of spin-sdk , since what just happened is it compiles the code to as an asset that is serve by the file-server.... What I am searching actually is , or have been lurking on my head to ask is e.g. I have a rust image e.g. a grpc server or actix web ... that i have build on docker is there a way using spin cli to deploy as such? If there is please let me know |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Thanks for digging so deeply into the Spin and Wasm rabbit hole! There are a lot of overlapping threads here and I'm not quite sure how to address all of them, but I'll do my best! Spin is a server-side Web assembly runtime. That is, an application (or component) to run in Spin must indeed be compiled to Wasm. You can't use it to deploy arbitrary containers, whether the application in those containers is Rust-based or not. The file server is an example of a Spin WebAssembly component. It illustrates that you don't necessarily need to compile your own code to Wasm. For example, you can use the fileserver to serve a site generated using a SSG such as Zola (https://github.com/karthik2804/spin-zola), or to serve Leptos or Dioxus CSR application, because there is no server-side logic in any of those except for serving the static assets (browser Wasm, JavaScript, CSS, etc.). (Yes, the CSR examples involve building your own code to Wasm... but it is client-side Wasm, which from Spin's point of view is a static asset. It doesn't run in Spin: it just gets handed off to the browser.) Another example might be the Bartholomew CMS: you can use the Bartholomew Wasm file (linked from https://github.com/fermyon/bartholomew/releases/tag/v0.10.0) to serve templated Markdown files without ever writing a line of Rust code. If you do build server code to Wasm to run in Spin, then you need the resulting Wasm to conform to the Spin and WASI interfaces. The SDK (and the Rust The So, the key things for your topics and questions are, I think:
I hope that gives a bit more of a picture. Happy to clarify further. |
Beta Was this translation helpful? Give feedback.
Thanks for digging so deeply into the Spin and Wasm rabbit hole! There are a lot of overlapping threads here and I'm not quite sure how to address all of them, but I'll do my best!
Spin is a server-side Web assembly runtime. That is, an application (or component) to run in Spin must indeed be compiled to Wasm. You can't use it to deploy arbitrary containers, whether the application in those containers is Rust-based or not.
The file server is an example of a Spin WebAssembly component. It illustrates that you don't necessarily need to compile your own code to Wasm. For example, you can use the fileserver to serve a site generated using a SSG such as Zola (https://github.com/karthik2804/spi…