Skip to content

Instantly share code, notes, and snippets.

@jschatz1
Created January 4, 2021 18:08
Show Gist options
  • Save jschatz1/c608036bb4e25e31f12fc3eb714174a0 to your computer and use it in GitHub Desktop.
Save jschatz1/c608036bb4e25e31f12fc3eb714174a0 to your computer and use it in GitHub Desktop.
use actix_web::{
middleware, web, App, HttpResponse, HttpServer,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct Person {
name: String,
number: i32,
}
// this handler uses json extractor
async fn index(item: web::Json<Person>) -> HttpResponse {
println!("model: {:?}", &item);
HttpResponse::Ok().json(item.0) // <- send response
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.data(web::JsonConfig::default().limit(4096))
.service(web::resource("/").route(web::post().to(index)))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment