Aka: 10 minutes to get your Rust code into production
How can I flip my selfie horizontally on IOS so that the logo on my new T-shirt works?
Steve is The author of The Rust Programming Language bible. The book has recently been published in Chinese as Rust’s Authoritative Guide.
After this “help” was issued, in view of Steve’s social status and character, many people began to offer advice. The answers are so varied that one person even made a video showing him/her how to flip an image on an iPhone in case he/she didn’t get it.
Tim McCallum, a programmer based in Australia, woke up seven hours later in the daytime in the United States, saw the answer and decided that manual fixes were not the way programmers should be. So I developed a Rust Web service to solve this problem.
Tim is the core developer of Second State and the author of Second State FaaS – a fast and usable Web services environment for Rust functions.
However, 10 minutes later, Tim came back with a rollover service he had implemented using Rust and told Steve that a Rollover as a service based on Rust had been created! Both mobile and PC terminals can be used to quickly realize image flipping in 1 second.
Here, the renderings!
Well, now everyone knows Steve works at Oxide!
Click on this site to experience image flipping with Rust.
There are many libraries for image processing in Rust, and Tim used the Image library when developing Rust FaaS. Processing images with Rust is not difficult; it takes time to make the program simple and easy to use.
Typically, we write Rust functions on Linux, compile them, and the compiled programs are impossible to run on a phone. This requires us to use the command line on the PC side, copy and paste it several times, and finally send it to the phone.
However, could there be an easy way to put the Rust function directly on the Web service to make it easier to use?
This results in Second State FaaS for Rust, which quickly implements Rust Web services and makes Rust functions available to everyone!
How can I quickly publish a Web service written in Rust
Software to prepare
The simplest and fastest way is to Fork wASM-learning github repo and open github repo using Github codespaces. You can provide a Web service by modifying the Rust code in the SRC folder to suit your application needs.
Of course, older drivers can also use Docker Image or install Rust and SSVMup on Ubuntu 20.04.
$ docker pull secondstate/ssvm-nodejs-starter:v2 $ docker run -p 3000:3000 --rm -it -v $(pwd):/app secondstate/ssvm-nodejs-starter:v2 (docker) # ssvmup build ... .Copy the code
Rust code
use wasm_bindgen::prelude::*; use image::{ImageOutputFormat, GenericImageView, ImageFormat}; #[wasm_bindgen] pub fn flip(img_buf: &[u8]) -> Vec<u8> { println! ("image size is {}", img_buf.len()); let img = image::load_from_memory(img_buf).unwrap(); let (w,h) = img.dimensions(); println! ("Image size {} {}", w, h); println! ("Drawing ..." ); let filtered = img.fliph(); println! ("Returning ..." ); let mut buf = vec! []; let image_format_detected: ImageFormat = image::guess_format(img_buf).unwrap(); match image_format_detected { ImageFormat::Gif => { filtered.write_to(&mut buf, ImageOutputFormat::Gif).unwrap(); }, _ => { filtered.write_to(&mut buf, ImageOutputFormat::Png).unwrap(); }, } return buf; }Copy the code
Compile and deploy the Rust function
Compile Rust functions into WebAssembly functions using the SSvVMup tool. The compiled WASM files will be saved in the PKG directory.
$ ssvmup build
Copy the code
Upload the.wasm file in the PKG directory to the/API/Executables RPC service endpoint in Second State FaaS. Check the.wasm file name before uploading.
$ curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/executables' \
--header 'Content-Type: application/octet-stream' \
--header 'SSVM-Description: flip' \
--data-binary '@pkg/hello_lib_bg.wasm'
Copy the code
. After the WASM file is uploaded, Second State FaaS returns WASm_ID. Wasm_id can access functions in deployed WASM files.
Invoke the FaaS function
Now you can call the Rust function from the Web! The RPC service endpoint is in/API /multipart/run/wasm_id/function_name, where wasm_id is the ID of the Wasm file that was just deployed, Function_name is the name of the function we are going to call in the WASM file, flip.
$ curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/multipart/run/222/flip/bytes' \
--header 'Content-Type: multipart/form-data' \
--form 'input_1=@test/oxide.png' \
--output tmp.png
Copy the code
Web UI
Once the testing is complete, you can publish a website that allows more people to use Rust FaaS quickly. Here is the JavaScript code for the AJAX call to this FaaS function.
$.ajax({ url: "https://rpc.ssvm.secondstate.io:8081/api/run/161/say", type: "post", data : $('#input').val(), contentType: "text/plain", processData: false, success: function (data) { $('#result').html(data); }});Copy the code
Click here to access the Live demo. Rust FaaS brings Rust functions to Web services, making Rust more widely available.
Finally, roll around and get a Github star! The Github repo has many learning resources related to Rust and WebAssembly.
Github.com/second-stat…
🏆 issue 7 | all things can be Serverless technology projects