-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic http server to Dummy runtime
- Loading branch information
1 parent
f9806e1
commit fc63460
Showing
3 changed files
with
78 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) | ||
} |