Initial commit
This commit is contained in:
commit
ec2977022d
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
.idea
|
1515
Cargo.lock
generated
Normal file
1515
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "hello_world_http"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = {version = "4.5.9", features = ["derive", "env"]}
|
||||||
|
actix-web = "4"
|
||||||
|
env_logger = "0.11.5"
|
45
src/main.rs
Normal file
45
src/main.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
use actix_web::middleware::Logger;
|
||||||
|
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
|
use clap::Parser;
|
||||||
|
use env_logger::Env;
|
||||||
|
|
||||||
|
/// Simple hello world HTTP server
|
||||||
|
#[derive(clap::Parser, Debug)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
/// Name of the person to greet
|
||||||
|
#[arg(env, short, long, default_value = "0.0.0.0:8000")]
|
||||||
|
listen_url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
async fn hello() -> impl Responder {
|
||||||
|
HttpResponse::Ok().body("Hello world!")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/echo")]
|
||||||
|
async fn echo(req_body: String) -> impl Responder {
|
||||||
|
HttpResponse::Ok().body(req_body)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn manual_hello() -> impl Responder {
|
||||||
|
HttpResponse::Ok().body("Hey there!")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
|
env_logger::init_from_env(Env::default().default_filter_or("info"));
|
||||||
|
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
|
HttpServer::new(|| {
|
||||||
|
App::new()
|
||||||
|
.wrap(Logger::default())
|
||||||
|
.service(hello)
|
||||||
|
.service(echo)
|
||||||
|
.route("/hey", web::get().to(manual_hello))
|
||||||
|
})
|
||||||
|
.bind(args.listen_url)?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user