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
```cargo
[dependencies]
cgi2 = "0.7"
```

#[cgi::main]
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à: https://fnordig.de/cgi-bin/hello-world.rs

Need some state? Throw in a database and you can keep track of that too: https://fnordig.de/cgi-bin/counter.rs 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!


Footnotes:

1

Nightly.