From d321b7e2de6d360e7cbd2a6f4734ddb00f385d6e Mon Sep 17 00:00:00 2001 From: HaQadosch Date: Sun, 19 Nov 2023 18:12:00 +0000 Subject: [PATCH] install rust and template --- README.org | 123 +++++++++++++++++++++++++++++++++++++++++----- qstart/.gitignore | 2 + qstart/Cargo.toml | 16 ++++++ qstart/spin.toml | 18 +++++++ qstart/src/lib.rs | 12 +++++ 5 files changed, 158 insertions(+), 13 deletions(-) create mode 100644 qstart/.gitignore create mode 100644 qstart/Cargo.toml create mode 100644 qstart/spin.toml create mode 100644 qstart/src/lib.rs diff --git a/README.org b/README.org index 2264594..426135b 100644 --- a/README.org +++ b/README.org @@ -22,20 +22,20 @@ Checking if the install went ok #+description: running version and upgrade to confirm install is ok #+name: confirm install #+begin_src shell - rustup --version - rustup upgrade + rustup --version + rustup upgrade #+end_src #+RESULTS: confirm install -| rustup 1.26.0 (5af9b9484 2023-04-05) -| info: This is the version for the rustup toolchain manager, not the rustc compiler. -| info: The currently active `rustc` version is `rustc 1.74.0 (79e9716c9 2023-11-13)` -| info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' -| info: checking for self-update -| -| stable-x86_64-unknown-linux-gnu unchanged - rustc 1.74.0 (79e9716c9 2023-11-13) -| -| info: cleaning up downloads & tmp directories +: rustup 1.26.0 (5af9b9484 2023-04-05) +: info: This is the version for the rustup toolchain manager, not the rustc compiler. +: info: The currently active `rustc` version is `rustc 1.74.0 (79e9716c9 2023-11-13)` +: info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' +: info: checking for self-update +: +: stable-x86_64-unknown-linux-gnu unchanged - rustc 1.74.0 (79e9716c9 2023-11-13) +: +: info: cleaning up downloads & tmp directories Ensure cargo, the package manager is also installed @@ -45,6 +45,103 @@ Ensure cargo, the package manager is also installed #+end_src #+RESULTS: confirm cargo installed -| cargo 1.74.0 (ecb9851af 2023-10-18) -* Rust mode for emacs +: cargo 1.74.0 (ecb9851af 2023-10-18) +** Rust mode for emacs Woohoo https://github.com/rust-lang/rust-mode +* Install Spin +This has been done already in a previous tut. +#+name: check install spin +#+begin_src shell + spin -V +#+end_src + +#+RESULTS: check install spin +: spin 2.0.1 (1d72f1c 2023-11-10) +* Install Templates for Rust +This has also been done in a previous tut. +#+name: check install templates +#+begin_src shell + spin template list +#+end_src + +#+RESULTS: check install templates +: +------------------------------------------------------------------------+ +: | Name Description | +: +========================================================================+ +: | http-c HTTP request handler using C and the Zig toolchain | +: | http-empty HTTP application with no components | +: | http-go HTTP request handler using (Tiny)Go | +: | http-grain HTTP request handler using Grain | +: | http-js HTTP request handler using Javascript | +: | http-php HTTP request handler using PHP | +: | http-rust HTTP request handler using Rust | +: | http-swift HTTP request handler using SwiftWasm | +: | http-ts HTTP request handler using Typescript | +: | http-zig HTTP request handler using Zig | +: | redirect Redirects a HTTP route | +: | redis-go Redis message handler using (Tiny)Go | +: | redis-rust Redis message handler using Rust | +: | static-fileserver Serves static files from an asset directory | +: +------------------------------------------------------------------------+ +* Installing Wasm +We need additional tools to support Wasm. +#+description: adding extra tools +#+name: add wasm +#+begin_src shell + rustup target add wasm32-wasi +#+end_src + +#+RESULTS: add wasm +: info: downloading component 'rust-std' for 'wasm32-wasi' +: info: installing component 'rust-std' for 'wasm32-wasi' +: 17.7 MiB / 17.7 MiB (100 %) 13.8 MiB/s in 1s ETA: 0s +* Create the first application +We use ~spin new~ and select the rust template ~http-rust~ +#+name: spin new http-rust +#+begin_src shell + spin new -t http-rust qstart +#+end_src + +#+RESULTS: spin new http-rust +: Description []: a quick hello world +: Description: a quick hello world +: HTTP path [/...]: +: HTTP path: /... + +#+description: check the folder structure for Rust app +#+name: tree qstart +#+begin_src shell + tree qstart/ +#+end_src + +#+RESULTS: tree qstart +: qstart/ +: ├── Cargo.toml +: ├── spin.toml +: └── src +: └── lib.rs +: +: 2 directories, 3 files + +The manifest file is =spin.toml= +#+begin_src toml + spin_manifest_version = 2 + + [application] + name = "qstart" + version = "0.1.0" + authors = ["HaQadosch "] + description = "a quick hello world" + + [[trigger.http]] + route = "/..." + component = "qstart" + + [component.qstart] + source = "target/wasm32-wasi/release/qstart.wasm" + allowed_outbound_hosts = [] + [component.qstart.build] + command = "cargo build --target wasm32-wasi --release" + watch = ["src/**/*.rs", "Cargo.toml"] +#+end_src + diff --git a/qstart/.gitignore b/qstart/.gitignore new file mode 100644 index 0000000..386474f --- /dev/null +++ b/qstart/.gitignore @@ -0,0 +1,2 @@ +target/ +.spin/ diff --git a/qstart/Cargo.toml b/qstart/Cargo.toml new file mode 100644 index 0000000..5a16181 --- /dev/null +++ b/qstart/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "qstart" +authors = ["HaQadosch "] +description = "a quick hello world" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = [ "cdylib" ] + +[dependencies] +anyhow = "1" +http = "0.2" +spin-sdk = { git = "https://github.com/fermyon/spin", tag = "v2.0.1" } + +[workspace] diff --git a/qstart/spin.toml b/qstart/spin.toml new file mode 100644 index 0000000..35abfe1 --- /dev/null +++ b/qstart/spin.toml @@ -0,0 +1,18 @@ +spin_manifest_version = 2 + +[application] +name = "qstart" +version = "0.1.0" +authors = ["HaQadosch "] +description = "a quick hello world" + +[[trigger.http]] +route = "/..." +component = "qstart" + +[component.qstart] +source = "target/wasm32-wasi/release/qstart.wasm" +allowed_outbound_hosts = [] +[component.qstart.build] +command = "cargo build --target wasm32-wasi --release" +watch = ["src/**/*.rs", "Cargo.toml"] diff --git a/qstart/src/lib.rs b/qstart/src/lib.rs new file mode 100644 index 0000000..f3051ef --- /dev/null +++ b/qstart/src/lib.rs @@ -0,0 +1,12 @@ +use spin_sdk::http::{IntoResponse, Request}; +use spin_sdk::http_component; + +/// A simple Spin HTTP component. +#[http_component] +fn handle_qstart(req: Request) -> anyhow::Result { + println!("Handling request to {:?}", req.header("spin-full-url")); + Ok(http::Response::builder() + .status(200) + .header("content-type", "text/plain") + .body("Hello, Fermyon")?) +}