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:
@ -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"
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user