mirror of
https://github.com/Noratrieb/karlheinz.git
synced 2026-01-16 23:45:00 +01:00
heinz
This commit is contained in:
parent
2e19f2888d
commit
cffd2de050
3 changed files with 88 additions and 13 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -806,6 +806,8 @@ name = "karlheinz"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,5 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3"
|
actix-web = "3"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0.64"
|
||||||
95
src/main.rs
95
src/main.rs
|
|
@ -1,48 +1,119 @@
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use actix_web::{App, get, HttpResponse, HttpServer, post, Responder, web};
|
use actix_web::{App, Either, get, HttpResponse, HttpServer, post, Responder, web};
|
||||||
|
use actix_web::http::{HeaderName, HeaderValue};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
macro_rules! impl_responder {
|
||||||
|
(for $name: ident) => {
|
||||||
|
impl actix_web::Responder for $name {
|
||||||
|
type Error = actix_web::Error;
|
||||||
|
type Future = std::future::Ready<Result<actix_web::HttpResponse, actix_web::Error>>;
|
||||||
|
|
||||||
|
fn respond_to(self, _req: &actix_web::HttpRequest) -> Self::Future {
|
||||||
|
let body = serde_json::to_string(&self).unwrap();
|
||||||
|
|
||||||
|
std::future::ready(Ok(actix_web::HttpResponse::Ok()
|
||||||
|
.content_type("application/json")
|
||||||
|
.body(body)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
impl_responder!(for Person);
|
||||||
|
impl_responder!(for Post);
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
struct Person {
|
struct Person {
|
||||||
name: String,
|
name: String,
|
||||||
age: i32,
|
age: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
struct Post {
|
||||||
|
#[serde(default)]
|
||||||
|
id: usize,
|
||||||
|
author: String,
|
||||||
|
title: String,
|
||||||
|
content: String,
|
||||||
|
}
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
hugos_name: Mutex<String>,
|
posts: Mutex<Vec<Post>>,
|
||||||
|
hugo: Mutex<Person>,
|
||||||
request_amount: Mutex<i32>,
|
request_amount: Mutex<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/post/{id}")]
|
||||||
|
async fn get_post(
|
||||||
|
web::Path(id): web::Path<usize>,
|
||||||
|
data: web::Data<AppState>,
|
||||||
|
) -> Either<impl Responder, impl Responder> {
|
||||||
|
let posts = data.posts.lock().unwrap();
|
||||||
|
|
||||||
|
match posts.get(id) {
|
||||||
|
None => Either::A(HttpResponse::NotFound()),
|
||||||
|
Some(post) => Either::B(post.clone()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/post")]
|
||||||
|
async fn post_post(mut post: web::Json<Post>, data: web::Data<AppState>) -> impl Responder {
|
||||||
|
let mut posts = data.posts.lock().unwrap();
|
||||||
|
post.id = posts.len();
|
||||||
|
posts.push(post.clone());
|
||||||
|
post
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/hugo")]
|
#[get("/hugo")]
|
||||||
async fn hugo(data: web::Data<AppState>) -> impl Responder {
|
async fn hugo(data: web::Data<AppState>) -> impl Responder {
|
||||||
let hugo_name = &data.hugos_name.lock().unwrap();
|
let hugo_person = data.hugo.lock().unwrap();
|
||||||
let mut counter = data.request_amount.lock().unwrap();
|
let mut counter = data.request_amount.lock().unwrap();
|
||||||
*counter += 1;
|
*counter += 1;
|
||||||
|
|
||||||
HttpResponse::Ok().body(format!("{} as been requested {} times", hugo_name, counter))
|
HttpResponse::Ok()
|
||||||
|
.header(
|
||||||
|
HeaderName::from_static("request-amount"),
|
||||||
|
HeaderValue::from(*counter),
|
||||||
|
)
|
||||||
|
.content_type("application/json")
|
||||||
|
.body(serde_json::to_string(&*hugo_person).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/hugo")]
|
#[post("/hugo")]
|
||||||
async fn hugo_post(req_body: String, data: web::Data<AppState>) -> impl Responder {
|
async fn hugo_post(new_hugo: web::Json<Person>, data: web::Data<AppState>) -> impl Responder {
|
||||||
let mut hugo_name = data.hugos_name.lock().unwrap();
|
let mut hugo_person = data.hugo.lock().unwrap();
|
||||||
*hugo_name = req_body;
|
|
||||||
|
|
||||||
HttpResponse::Ok().body(&*hugo_name)
|
*hugo_person = new_hugo.clone();
|
||||||
|
new_hugo
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
let posts = vec![Post {
|
||||||
|
id: 0,
|
||||||
|
author: "Hugo Boss".to_string(),
|
||||||
|
title: "I like winning".to_string(),
|
||||||
|
content: "I really like winning. That's why I always win at everything".to_string(),
|
||||||
|
}];
|
||||||
|
|
||||||
let data = web::Data::new(AppState {
|
let data = web::Data::new(AppState {
|
||||||
|
posts: Mutex::new(posts),
|
||||||
request_amount: Mutex::new(0),
|
request_amount: Mutex::new(0),
|
||||||
hugos_name: Mutex::new("Hugo Boss".to_string()),
|
hugo: Mutex::new(Person {
|
||||||
|
name: "Hugo Boss".to_string(),
|
||||||
|
age: 40,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
HttpServer::new(move ||
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(data.clone())
|
.app_data(data.clone())
|
||||||
.service(hugo_post)
|
.service(hugo_post)
|
||||||
.service(hugo)
|
.service(hugo)
|
||||||
)
|
.service(get_post)
|
||||||
|
.service(post_post)
|
||||||
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue