Base application

2020-05-17

This is our base application, rapu. It is a simple web server with a couple of endpoints. The / endpoint serves a hello text, the /status endpoint serves a 200 OK status code, and the /uuid endpoint serves a randomly-generated UUID.

// main.rs
use std::env;
use uuid::Uuid;

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
    let mut app = tide::new();
    let port = env::var("PORT").expect("PORT variable missing");

    app.at("/")
        .get(|_| async move { "Hello from Rust! 🦀\n" });

    app.at("/status")
        .get(|_| async move { tide::Response::new(200) });

    app.at("/uuid").get(|_| async move { generate_uuid() });

    app.listen(format!("0.0.0.0:{}", port)).await?;
    Ok(())
}

fn generate_uuid() -> String {
    Uuid::new_v4().to_hyphenated().to_string()
}


# Cargo.toml
[package]
name = "rapu"
version = "0.1.0"
authors = ["Walther <veeti.haapsamo@gmail.com>"]
edition = "2018"
publish = false

[dependencies]
tide = "0.6.0"
async-std = { version = "1.5.0", features = ["attributes"] }
uuid = { version = "0.8.1", features = ["v4"] }

You can run the application locally with

# debug mode, by default
PORT=8080 cargo run
# release mode, slower to build but faster binary
PORT=8080 cargo run --release