Search the Community
Showing results for tags 'rust'.
-
I saw a couple of questions regarding Rust on HelioHost. While it isn't officially supported, I did manage to get a simple Rust application running on Tommy! My method was based on the fact that you can upload Go binaries and run them according to this tutorial. Since a binary is a binary, why not deploy a Rust one the same way? The simple method: At its most basic level, a CGI script just has to return text along with headers. Consider the "Hello world" Perl script: #!/usr/bin/perl print "Content-Type: text/html\n\n"; print "Hello World!"; Based on this, I made the following Rust script, mini.rs: fn main () { print!("Content-type: text/html\n\n"); print!("Hello world!"); } Interlude: Experimentation (you can skip this italicised section unless you want the details) At first, I tried compiling this with a simple rustc mini.rs and uploading the resulting mini file to my cgi-bin. (I use Manjaro, which is Linux, so since HelioHost also uses Linux I didn't have to change the target architecture). However, that was resulting in a 500 Internal Server Error. To debug this, I tried running the compiled binary on a different VPS (running Debian, so it would have slightly older versions of libraries than Manjaro). I realised that the library glibc/libc6 wasn't matching. I found a related comment on this StackOverflow question suggesting using static linking as described in this other StackOverflow answer. I already had the dependencies, so all I had to do was add extra arguments to the compile process to make it: rustc -C target-feature=+crt-static mini.rs. This produced a statically linked binary which I could then run anywhere! To deploy the program to HelioHost, all I had to do was: Run the command to create a statically linked binary, rustc -C target-feature=+crt-static mini.rs (if you're not using Linux you'll have to also set up and specify the Linux architecture as a build target) Copy the resulting mini binary file (the one without the .rs extension) to my website's cgi-bin Set the permissions to 755 (rwxr-xr-x, in other words everyone has read and execute permissions and only the owner has write permissions) Navigate to my website's /cgi-bin/mini and the page rendered! I think a binary may not be necessary if you can compile using the same version of libc6 that's running on Tommy (assuming it has the libc6 library at all) The advanced method: Instead of returning plaintext, you can also use a library like rust-cgi to generate the output for you. I'm not sure how exactly it works since I'm not familiar with CGI but I'm guessing it will help do things like read header data? I followed the docs there to create an empty Cargo project and the following main.rs file: extern crate cgi; cgi::cgi_main! { |_request: cgi::Request| -> cgi::Response { cgi::html_response(200, "<html><title>My Site</title><body>Hello world!</body></html>") } } After this I ran cargo build with the modified flags to get a statically linked binary: RUSTFLAGS="-C target-feature=+crt-static" cargo build Basically, setting the RUSTFLAGS environment variable before running Cargo. If you're doing this often, you can also add the option to Cargo.toml: [build] rustflags = ["-C", "target-feature=+crt-static"] target = "x86_64-unknown-linux-gnu" ...and then simply run cargo build to get the binary. This created a larger binary than with the simple method; mine was around 8.1MB instead of 5.4MB. This is presumably because it has more features (that we aren't using right now) bundled from the library. But if you are using those features I'm guessing it'll be helpful. Uploading the resulting binary, target/debug/[your project name], to cgi-bin will make it get auto-executed whenever you load that URL. WARNING: MAKE SURE YOUR PROCESS ENDS! Most Rust web frameworks like axum, Actix, and Rocket fire up a permanently running web server listening on a given port. That will use up your CPU resources and probably push you over your limit—or, best case, cause your process to get killed after some timeout. Either way, it won't work! So make sure your process runs quickly, prints whatever it needs to, and then exits. It will repeat this for every request, just like the scripts for Perl, PHP, and other languages do. Unfortunately that means we can't use most Rust web frameworks, unless we figure out a way to make them work the CGI way (if you figure that out, please let me know!). Tip: Local testing To test your CGI scripts locally, you can go into any folder, create a folder named cgi-bin inside, and run the following command in the outer folder (i.e. not inside cgi-bin) python -m http.server --cgi This will fire up a local server (usually at localhost:8000), and anything you put in the cgi-bin folder (including Rust binaries) will execute the same way they would on an actual server.