Last week, I wrote a little hello world app that responds to HTTP requests with a clippy ASCII art. But the docker image for the app was 111MB in size.
So today, I decided to port the app to rust.
If you only want to see the code, here you go. Here’s the PR.
It took me 3 hours to port the app, whereas writing the C# app had barely taken me 5 minutes. I ended up spending most of the time trying to decide which web-framework to use, and the rest of the time trying to put together a Dockerfile.
The rust code in itself was straightforward. I chose to use a web-framework called axum, and they already have a huge list of examples in their GitHub repository.
Here’s my rust code:
1use axum::{response::Result, Router};
2use std::net::SocketAddr;
3use std::env;
4
5const CLIPPY:&str = r#"
6
7 __
8 / \ _____________
9 | | / \
10 @ @ | It looks |
11 || || | like you |
12 || || Result {
13 Ok(CLIPPY)
14}
15
16fn get_port() -> u16 {
17 env::var(PORT_ENV_VAR)
18 .unwrap_or("NOT_SET".to_string())
19 .parse::()
20 .unwrap_or(3000)
21}
It’s not very different from C# if you think about it. It’s almost as if C++ and C# had a baby.
Since the goal of the rewrite was to reduce the docker image size, here are some size stats:
Final ImageLanguageSizeubuntu:lunarC#160MBubuntu/dotnet-deps:7.0_edgeC#111MBalpine:latestRust13.1MBscratchRust6MB
Mission accomplished.