From 3fc532ca2ae3fe0cf063d55abb65f9e0831ec9c8 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Thu, 28 Dec 2023 18:40:31 +0200 Subject: [PATCH] new post: cargo script and cgi --- _posts/2023-12-28-cargo-script-and-cgi.md | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 _posts/2023-12-28-cargo-script-and-cgi.md diff --git a/_posts/2023-12-28-cargo-script-and-cgi.md b/_posts/2023-12-28-cargo-script-and-cgi.md new file mode 100644 index 0000000..d6275fd --- /dev/null +++ b/_posts/2023-12-28-cargo-script-and-cgi.md @@ -0,0 +1,59 @@ +--- +permalink: "/{{ year }}/{{ month }}/{{ day }}/cargo-script-and-cgi" +title: "cargo script and cgi" +published_date: "2023-12-28 18:30:00 +0200" +layout: post.liquid +data: + route: blog +excerpt: | + 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 and that's easily possible with Rust too. +--- + +Everything old is new again. +These days things get deployed as Cloud Functions, Lambdas or whatever [FaaS](https://en.wikipedia.org/wiki/Function_as_a_service) it is today. +But those from the old days [simply stuck with CGI](https://rednafi.com/go/reminiscing_cgi_scripts/). + +Modern[^1] 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][cgi-fork]). +Now all it takes is this little bit of Rust code: + +````rust +#!/usr/bin/env -S cargo +nightly -q -Zscript +```cargo +[dependencies] +cgi = { git = "https://github.com/badboy/rust-cgi" } +``` + +#[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Ă : + + +Need some state? Throw in a database and you can keep track of that too: + +The source code is [available in a Gist][gist]. That's barely 50 lines (plus dependencies). + +Go deploy some cloud functions ... I mean lambdas ... I mean CGI scripts! + +[RFC 3424]: https://github.com/rust-lang/rfcs/blob/798ba4e1ef2175400e4c029cf952aa2d6df96f45/text/3424-cargo-script.md +[fcgiwrap]: https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/ +[cgi]: https://crates.io/crates/cgi +[cgi-fork]: https://github.com/badboy/rust-cgi +[hello-world.rs]: https://fnordig.de/cgi-bin/hello-world.rs +[gist]: https://gist.github.com/badboy/13dae661300e7e09b6f5430297fc341c + +--- + +_Footnotes:_ + +[^1]: Nightly.