This commit is contained in:
nora 2021-06-03 22:04:25 +02:00
parent 5159de5269
commit e9cb4fca7e
3 changed files with 1783 additions and 2 deletions

1734
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "3"

View file

@ -1,3 +1,49 @@
fn main() {
println!("Hello, world!");
use std::sync::Mutex;
use actix_web::{App, get, HttpResponse, HttpServer, post, Responder, web};
#[derive(Serialize, Deserialize)]
struct Person {
name: String,
age: i32,
}
struct AppState {
hugos_name: Mutex<String>,
request_amount: Mutex<i32>,
}
#[get("/hugo")]
async fn hugo(data: web::Data<AppState>) -> impl Responder {
let hugo_name = &data.hugos_name.lock().unwrap();
let mut counter = data.request_amount.lock().unwrap();
*counter += 1;
HttpResponse::Ok().body(format!("{} as been requested {} times", hugo_name, counter))
}
#[post("/hugo")]
async fn hugo_post(req_body: String, data: web::Data<AppState>) -> impl Responder {
let mut hugo_name = data.hugos_name.lock().unwrap();
*hugo_name = req_body;
HttpResponse::Ok().body(&*hugo_name)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let data = web::Data::new(AppState {
request_amount: Mutex::new(0),
hugos_name: Mutex::new("Hugo Boss".to_string()),
});
HttpServer::new(move ||
App::new()
.app_data(data.clone())
.service(hugo_post)
.service(hugo)
)
.bind("127.0.0.1:8080")?
.run()
.await
}