cargo script and cgi

Everything old is new again. These days things get deployed as Cloud Functions, Lambdas or whatever FaaS it is today. But those from the old days simply stuck with CGI.

Modern1 Rust is perfect for that kind of world! Cargo now has support for single-file packages (RFC 3424), which allows you to stuff all your code into a single .rs file and then run that. It even caches the built binary, so it's efficient enough to run on every request.

I went ahead and updated the cgi crate (my fork here). Now all it takes is this little bit of Rust code:

#!/usr/bin/env -S cargo +nightly -q -Zscript
cgi2 = "0.7"

fn main(_request: cgi::Request) -> cgi::Response {
    cgi::text_response(200, "Hello World!")

Install the fcgiwrap tool, follow the instructions to configure Nginx and voilà:

Need some state? Throw in a database and you can keep track of that too: The source code is available in a Gist. That's barely 50 lines (plus dependencies).

Go deploy some cloud functions ... I mean lambdas ... I mean CGI scripts!