1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 15:39:02 +02:00

update examples to v4 (#258)

This commit is contained in:
Rob Ede
2022-02-26 03:56:24 +00:00
committed by GitHub
parent 890fea6c23
commit 81dfe300a2
110 changed files with 321 additions and 334 deletions

View File

@ -4,7 +4,7 @@ version = "1.0.0"
edition = "2018"
[dependencies]
actix-web = "3"
actix-web = "4"
futures = "0.3.1"
serde = "1.0"
serde_json = "1.0"

View File

@ -1,6 +1,7 @@
// <responder-trait>
use actix_web::{Error, HttpRequest, HttpResponse, Responder};
use futures::future::{ready, Ready};
use actix_web::{
body::BoxBody, http::header::ContentType, HttpRequest, HttpResponse, Responder,
};
use serde::Serialize;
#[derive(Serialize)]
@ -10,16 +11,15 @@ struct MyObj {
// Responder
impl Responder for MyObj {
type Error = Error;
type Future = Ready<Result<HttpResponse, Error>>;
type Body = BoxBody;
fn respond_to(self, _req: &HttpRequest) -> Self::Future {
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> {
let body = serde_json::to_string(&self).unwrap();
// Create response and set content type
ready(Ok(HttpResponse::Ok()
.content_type("application/json")
.body(body)))
HttpResponse::Ok()
.content_type(ContentType::json())
.body(body)
}
}
@ -33,7 +33,7 @@ async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8080")?
.bind(("127.0.0.1", 8080))?
.run()
.await
}