#+title: Rust Quickstart #+date:[2023-11-19 Sun] #+author: HaQadosch #+startup: indent #+note: based on https://developer.fermyon.com/spin/v2/quickstart #+link: article https://developer.fermyon.com/spin/v2/quickstart * Org shortcuts ** Feetnotes[fn:1] - create footnote :: ~C-c C-x f~ - jump to the footnote ref :: ~C-c C-c~ * Instaling Rust Looking at the [[https://www.rust-lang.org/learn/get-started][get-started doc]], let's use ~rustup~ #+description: installing rust and libs using rustup #+name: install rustup #+begin_src shell curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh #+end_src 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 #+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 Ensure cargo, the package manager is also installed #+name: confirm cargo installed #+begin_src shell cargo --version #+end_src #+RESULTS: confirm cargo installed : 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 * Modifying the code We change the =qstar/lib.rs= file to display the =hello world= message. #+description: line change for message #+name: lib.rs diff #+begin_src shell git diff #+end_src #+RESULTS: lib.rs diff : diff --git a/qstart/src/lib.rs b/qstart/src/lib.rs : index f3051ef..a51e027 100644 : --- a/qstart/src/lib.rs : +++ b/qstart/src/lib.rs : @@ -8,5 +8,5 @@ fn handle_qstart(req: Request) -> anyhow::Result { : Ok(http::Response::builder() : .status(200) : .header("content-type", "text/plain") : - .body("Hello, Fermyon")?) : + .body("Hello, World!")?) : } * Building the app In the ~qstart/~ folder #+begin_src shell cd qstart/ spin build #+end_src #+RESULTS: : Building component qstart with `cargo build --target wasm32-wasi --release` : Finished release [optimized] target(s) in 0.17s : Finished building all Spin components * Running the app We could have done ~spin build --up~ to autostart the app once the build has been done. #+description: running the app once build is finished #+name: spin up the app #+begin_src shell cd qstart/ spin up #+end_src #+RESULTS: spin up the app : Logging component stdio to ".spin/logs/" : : Serving http://127.0.0.1:3000 : Available Routes: : qstart: http://127.0.0.1:3000 (wildcard) : 2023-11-19T18:38:54.648503Z ERROR spin_trigger::cli: Trigger executor failed : Error: Unable to listen on 127.0.0.1:3000 : : Caused by: : Address already in use (os error 98) In this case we have another app running in the same port. Let's see which one. #+name: lsof #+begin_src shell lsof -i tcp:3000 #+end_src #+RESULTS: lsof : COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME : forgejo 1531870 haqadosch 7u IPv6 8599019 0t0 TCP *:3000 (LISTEN) So we kill that one and restart the ~spin up~. #+name: spin up again #+begin_src shell kill -9 1531870 cd qstart/ spin up #+end_src #+RESULTS: spin up again : Logging component stdio to ".spin/logs/" : : Serving http://127.0.0.1:3000 : Available Routes: : qstart: http://127.0.0.1:3000 (wildcard) * Try the app We can display the page in your favourite browser or confirm with ~curl~ #+name: curl #+begin_src shell curl -i localhost:3000 #+end_src #+RESULTS: curl : HTTP/1.1 200 OK : content-type: text/plain : transfer-encoding: chunked : date: Sun, 19 Nov 2023 19:01:02 GMT : : Hello, World!