1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29: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"
derive_more = "0.99"
env_logger = "0.7"
log = "0.4"

View File

@ -17,7 +17,7 @@ async fn index() -> Result<&'static str> {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.bind(("127.0.0.1", 8080))?
.run()
.await
}

View File

@ -33,7 +33,7 @@ async fn main() -> std::io::Result<()> {
.wrap(logger)
.service(index)
})
.bind("127.0.0.1:8080")?
.bind(("127.0.0.1", 8080))?
.run()
.await
}

View File

@ -27,7 +27,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
}

View File

@ -1,6 +1,8 @@
// <override>
use actix_web::{
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
error, get,
http::{header::ContentType, StatusCode},
App, HttpResponse,
};
use derive_more::{Display, Error};
@ -18,8 +20,8 @@ enum MyError {
impl error::ResponseError for MyError {
fn error_response(&self) -> HttpResponse {
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
HttpResponse::build(self.status_code())
.insert_header(ContentType::html())
.body(self.to_string())
}
@ -53,7 +55,7 @@ async fn main() -> std::io::Result<()> {
use actix_web::HttpServer;
HttpServer::new(|| App::new().service(index).service(error2).service(error3))
.bind("127.0.0.1:8080")?
.bind(("127.0.0.1", 8080))?
.run()
.await
}

View File

@ -1,7 +1,8 @@
// <recommend-one>
use actix_web::{
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
HttpServer,
error, get,
http::{header::ContentType, StatusCode},
App, HttpResponse, HttpServer,
};
use derive_more::{Display, Error};
@ -13,8 +14,8 @@ enum UserError {
impl error::ResponseError for UserError {
fn error_response(&self) -> HttpResponse {
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
HttpResponse::build(self.status_code())
.insert_header(ContentType::html())
.body(self.to_string())
}
fn status_code(&self) -> StatusCode {
@ -35,7 +36,7 @@ async fn index() -> Result<&'static str, UserError> {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.bind(("127.0.0.1", 8080))?
.run()
.await
}

View File

@ -1,7 +1,8 @@
// <recommend-two>
use actix_web::{
dev::HttpResponseBuilder, error, get, http::header, http::StatusCode, App, HttpResponse,
HttpServer,
error, get,
http::{header::ContentType, StatusCode},
App, HttpResponse, HttpServer,
};
use derive_more::{Display, Error};
@ -13,10 +14,11 @@ enum UserError {
impl error::ResponseError for UserError {
fn error_response(&self) -> HttpResponse {
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
HttpResponse::build(self.status_code())
.insert_header(ContentType::html())
.body(self.to_string())
}
fn status_code(&self) -> StatusCode {
match *self {
UserError::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
@ -38,7 +40,7 @@ fn do_thing_that_fails() -> Result<(), std::io::Error> {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.bind(("127.0.0.1", 8080))?
.run()
.await
}