Skip to content

Commit

Permalink
Add basic http server to Dummy runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Sep 16, 2024
1 parent f9806e1 commit fc63460
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions runtimes/dummy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ name = "dummy"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

build = "../../build.rs"

[dependencies]
actix-web = "4.9"
anyhow = "1.0"
env_logger = "0.11.5"
humantime = "2.1"
humantime-serde = "1.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = "0.4"
tokio = { version = "1.32", features = [] }


[build-dependencies]
static_vcruntime = "2.0"
79 changes: 57 additions & 22 deletions runtimes/dummy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
use std::env;
use std::io;
use std::time::Duration;

fn main() {
let mut input = String::new();
use actix_web::{get, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize};

#[get("/")]
async fn index(_req: HttpRequest) -> impl Responder {
log::info!("Endpoint: index");
"Welcome!"
}

#[get("/healthcheck")]
async fn healthcheck(_req: HttpRequest) -> impl Responder {
log::info!("Endpoint: healthcheck");
HttpResponse::Ok()
}

#[get("/shutdown")]
async fn shutdown(_req: HttpRequest) -> impl Responder {
log::info!("Endpoint: shutdown");
HttpResponse::Ok()
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct GenerateTrafficQuery {
#[serde(with = "humantime_serde")]
pub sleep_time: Duration,
pub response_size: u64,
}

#[get("/generate-traffic")]
async fn generate_traffic(info: web::Query<GenerateTrafficQuery>) -> impl Responder {
log::info!(
"Endpoint: generate-traffic. Sleep time: {}. Response size: {}",
humantime::format_duration(info.sleep_time.clone()),
info.response_size
);

tokio::time::sleep(info.sleep_time).await;
HttpResponse::Ok()
}

#[actix_web::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
let args: Vec<String> = env::args().collect();

println!("Dummy runtime. Args: {args:?}");

loop {
input.clear();

match io::stdin().read_line(&mut input) {
Ok(_) => {
let trimmed_input = input.trim();
if trimmed_input == "stop" {
println!("Stopping.");
break;
}
}
Err(error) => {
eprintln!("Error reading input: {}", error);
// break;
}
}
}
log::info!("Dummy runtime. Args: {args:?}");

Ok(HttpServer::new(|| {
App::new()
.service(index)
.service(healthcheck)
.service(generate_traffic)
.service(shutdown)
})
.bind(("127.0.0.1", 7861))?
.run()
.await?)
}

0 comments on commit fc63460

Please sign in to comment.