Rust implementation of u/d/l for speedtest

Bipul Kuri
2 min readNov 1, 2021

--

Here is a basic rust implementation of speedtest upload download latency

code is here

//to run $cargo run//to test D/L  GET http://localhost:8080/api/downloads http://localhost:8080/api/latencys//to test U POST  curl -v -X POST http://localhost:8080/api/uploadsuse actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};use std::time::UNIX_EPOCH;use std::time::SystemTime;use serde::{Deserialize, Serialize};//use std::fs::File;//use std::io::Read;#[derive(Debug, Serialize, Deserialize)]struct LatencyObj {message: String,time: u64,}#[derive(Debug, Serialize, Deserialize)]struct UploadObj {result: String,message: u64,}//latency apiasync fn latency(_req: HttpRequest) -> HttpResponse {let latency_val = &LatencyObj {message: "pong".to_string(),time: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()};HttpResponse::Ok().json(latency_val) // <- send response}//download apiasync fn download() -> HttpResponse {// Read from req param the size to send back with check//mkfile -n 1g temp_1GB_file on MAC//following needs optimization//let mut f = File::open("./temp_1GB_file").unwrap();let mut buf = vec![0u8; 5000]; // send 5kb//f.read_exact(&mut buf);// 1MB buffer read from req.param.size//let buf: Vec<u8> = Vec::with_capacity(1000000);//let s = String::from_utf8(buf)// need to send buffer to octet-streamHttpResponse::Ok().content_type("application/octet-stream").body(buf)}//upload apiasync fn upload(_req: HttpRequest) -> HttpResponse {let upload_val = &UploadObj {result: "success".to_string(),message: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()};HttpResponse::Ok().json(upload_val) // <- send response}#[actix_web::main]async fn main() -> std::io::Result<()> {std::env::set_var("RUST_LOG", "actix_web=info");env_logger::init();HttpServer::new(|| {App::new()// enable logger.wrap(middleware::Logger::default()).data(web::JsonConfig::default().limit(1024)) // <- limit size of the payload (global configuration) //change it for upload max.service(web::resource("/api/latencys*").to(latency)).service(web::resource("/api/uploads*").route(web::post().to(upload))).service(web::resource("/api/downloads*").to(download))}).bind("127.0.0.1:8080")?.bind("127.0.0.1:8081")?.run().await}

any my Cargo.toml do a `cargo-upgrade`

[package]
name = "web-udl-api"
version = "0.1.0"
edition = "2021"
authors = ["bipul Kuri <bipul.k.kuri@gmail.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
actix-web = "3.3.2"
actix-rt = "2.3.0"
openssl = "0.10.36"
actix-service = "2.0.1"
futures = "0.3.17"
env_logger = "0.9.0"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.68"
json = "0.12.4"
[dev-dependencies]
actix-rt = "2.3.0"

--

--

No responses yet