Serving a Web Page
Serves the gcd()
function from GCD.
use actix_web::{web, App, HttpResponse, HttpServer}; use serde::Deserialize; #[derive(Deserialize)] struct GcdParameters { n: u64, m: u64, } fn main() { let server = HttpServer::new(|| { App::new() .route("/", web::get().to(get_index)) }); println!("Serving on http://localhost:3000...") server .bind("0.0.0.0:3000").expect("error binding to the server address") .run().expect("error running server"); } fn get_index() -> HttpResponse { HttpResponse::Ok() .content_type("text/html") .body( r#" <title>GCD Calculator</title> <form action="/gcd" method="post"> <input type="text" name="n"/> <input type="text" name="m"/> <button type="submit">Compute GCD</button> </form> "#, ) } fn gcd(mut n: u64, mut m: u64) -> u64 { // Make sure that either number isn't 0 assert!(n != 0 && m != 0); // Run a loop while the remainder `m` is not 0 while m != 0 { // Make sure we are always trying to get the divsor of // the smaller number if m < n { // initialize a temp variable let temp = m; m = n; n = temp; } // Get the divisor m = m % n; } // return n aka the greatest common denominator n } fn post_gcd(form: web::Form<GcdParameters>) -> HttpResponse { if form.n ==0 || form.m == 0 { return HttpResponse::BadRequest() .content_type("text/html") .body("Computing the GCD with 0 is boring."); } let response = format!("The greatest common divisor of the numbers {} and ) }