mirror of
https://github.com/actix/actix-website
synced 2024-11-23 16:31:08 +01:00
update examples to v4 (#258)
This commit is contained in:
parent
890fea6c23
commit
81dfe300a2
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"actix"
|
||||
]
|
||||
}
|
@ -186,7 +186,7 @@ APPENDIX: How to apply the Apache License to your work.
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
Copyright 2022 Actix Team
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
@ -1,4 +1,4 @@
|
||||
Copyright (c) 2018 Carl Lerche
|
||||
Copyright (c) 2022 Actix Team
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
|
@ -15,7 +15,7 @@ async fn main() -> std::io::Result<()> {
|
||||
App::new()
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -23,4 +23,5 @@ members = [
|
||||
"url-dispatch",
|
||||
"websockets",
|
||||
]
|
||||
exclude = ["databases", "sentry"]
|
||||
# TODO: update databases
|
||||
exclude = ["databases"]
|
||||
|
@ -5,5 +5,4 @@ edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-service = "1"
|
||||
actix-web = "4"
|
||||
|
@ -17,7 +17,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/index.html", web::get().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -12,16 +12,16 @@ async fn main() -> std::io::Result<()> {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1")
|
||||
.data(State1)
|
||||
.route("/", web::to(|| HttpResponse::Ok())),
|
||||
.app_data(web::Data::new(State1))
|
||||
.route("/", web::to(HttpResponse::Ok)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/app2")
|
||||
.data(State2)
|
||||
.route("/", web::to(|| HttpResponse::Ok())),
|
||||
.app_data(web::Data::new(State2))
|
||||
.route("/", web::to(HttpResponse::Ok)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
fn scoped_config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/test")
|
||||
.route(web::get().to(|| HttpResponse::Ok().body("test")))
|
||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||
.route(web::get().to(|| async { HttpResponse::Ok().body("test") }))
|
||||
.route(web::head().to(HttpResponse::MethodNotAllowed)),
|
||||
);
|
||||
}
|
||||
|
||||
@ -16,8 +16,8 @@ fn scoped_config(cfg: &mut web::ServiceConfig) {
|
||||
fn config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/app")
|
||||
.route(web::get().to(|| HttpResponse::Ok().body("app")))
|
||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||
.route(web::get().to(|| async { HttpResponse::Ok().body("app") }))
|
||||
.route(web::head().to(HttpResponse::MethodNotAllowed)),
|
||||
);
|
||||
}
|
||||
|
||||
@ -27,9 +27,12 @@ async fn main() -> std::io::Result<()> {
|
||||
App::new()
|
||||
.configure(config)
|
||||
.service(web::scope("/api").configure(scoped_config))
|
||||
.route("/", web::get().to(|| HttpResponse::Ok().body("/")))
|
||||
.route(
|
||||
"/",
|
||||
web::get().to(|| async { HttpResponse::Ok().body("/") }),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -12,15 +12,11 @@ pub mod vh;
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1").route("/", web::to(|| HttpResponse::Ok())),
|
||||
)
|
||||
.service(
|
||||
web::scope("/app2").route("/", web::to(|| HttpResponse::Ok())),
|
||||
)
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
.service(web::scope("/app1").route("/", web::to(HttpResponse::Ok)))
|
||||
.service(web::scope("/app2").route("/", web::to(HttpResponse::Ok)))
|
||||
.route("/", web::to(HttpResponse::Ok))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.app_data(counter.clone()) // <- register the created data
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ async fn index(data: web::Data<AppState>) -> String {
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.data(AppState {
|
||||
.app_data(web::Data::new(AppState {
|
||||
app_name: String::from("Actix-web"),
|
||||
})
|
||||
}))
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -8,16 +8,16 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(
|
||||
web::scope("/")
|
||||
.guard(guard::Header("Host", "www.rust-lang.org"))
|
||||
.route("", web::to(|| HttpResponse::Ok().body("www"))),
|
||||
.route("", web::to(|| async { HttpResponse::Ok().body("www") })),
|
||||
)
|
||||
.service(
|
||||
web::scope("/")
|
||||
.guard(guard::Header("Host", "users.rust-lang.org"))
|
||||
.route("", web::to(|| HttpResponse::Ok().body("user"))),
|
||||
.route("", web::to(|| async { HttpResponse::Ok().body("user") })),
|
||||
)
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
.route("/", web::to(HttpResponse::Ok))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,6 +4,6 @@ version = "2.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
futures = "0.3.1"
|
||||
bytes = "0.5"
|
||||
|
@ -14,7 +14,7 @@ async fn stream() -> HttpResponse {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(stream))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,4 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
|
@ -39,7 +39,7 @@ async fn main() -> io::Result<()> {
|
||||
App::new().data(pool.clone())
|
||||
.resource("/{name}", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,5 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
serde = "1.0"
|
||||
|
@ -25,7 +25,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/", web::get().to(index))
|
||||
.route("/register", web::post().to(register))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,4 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
|
@ -5,11 +5,11 @@ type RegisterResult = Either<HttpResponse, Result<&'static str, Error>>;
|
||||
|
||||
async fn index() -> RegisterResult {
|
||||
if is_a_variant() {
|
||||
// <- choose variant A
|
||||
Either::A(HttpResponse::BadRequest().body("Bad data"))
|
||||
// choose Left variant
|
||||
Either::Left(HttpResponse::BadRequest().body("Bad data"))
|
||||
} else {
|
||||
// <- variant B
|
||||
Either::B(Ok("Hello!"))
|
||||
// choose Right variant
|
||||
Either::Right(Ok("Hello!"))
|
||||
}
|
||||
}
|
||||
// </either>
|
||||
@ -19,7 +19,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
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -4,6 +4,6 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
@ -19,7 +19,7 @@ async fn index(form: web::Form<FormData>) -> Result<String> {
|
||||
#[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
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ async fn index(info: web::Json<Info>) -> Result<String> {
|
||||
#[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
|
||||
}
|
||||
|
@ -21,7 +21,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.limit(4096)
|
||||
.error_handler(|err, _req| {
|
||||
// create custom error response
|
||||
error::InternalError::from_response(err, HttpResponse::Conflict().finish()).into()
|
||||
error::InternalError::from_response(err, HttpResponse::Conflict().finish())
|
||||
.into()
|
||||
});
|
||||
|
||||
App::new().service(
|
||||
@ -31,7 +32,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route(web::post().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/{name}/{id}", web::post().to(index))
|
||||
.route("/{name}/{id}/extract", web::post().to(extract))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -8,10 +8,8 @@ struct Info {
|
||||
}
|
||||
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(
|
||||
web::Path((user_id, friend)): web::Path<(u32, String)>,
|
||||
query: web::Query<Info>,
|
||||
) -> String {
|
||||
async fn index(path: web::Path<(u32, String)>, query: web::Query<Info>) -> String {
|
||||
let (user_id, friend) = path.into_inner();
|
||||
format!(
|
||||
"Welcome {}, friend {}, user_id {}!",
|
||||
query.username, friend, user_id
|
||||
@ -23,7 +21,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ use actix_web::{get, web, Result};
|
||||
/// {user_id} - deserializes to a u32
|
||||
/// {friend} - deserializes to a String
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(web::Path((user_id, friend)): web::Path<(u32, String)>) -> Result<String> {
|
||||
async fn index(path: web::Path<(u32, String)>) -> Result<String> {
|
||||
let (user_id, friend) = path.into_inner();
|
||||
Ok(format!("Welcome {}, user_id {}!", friend, user_id))
|
||||
}
|
||||
|
||||
@ -14,7 +15,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ async fn index(info: web::Query<Info>) -> String {
|
||||
#[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
|
||||
}
|
||||
|
@ -4,5 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
serde = "1.0"
|
||||
|
@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(web::resource("/").to(hello_world))
|
||||
.service(web::resource("/temp").to(current_temperature))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -5,4 +5,4 @@ edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
|
@ -25,7 +25,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(echo)
|
||||
.route("/hey", web::get().to(manual_hello))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,4 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
|
@ -13,7 +13,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/", web::get().to(greet))
|
||||
.route("/{name}", web::get().to(greet))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,8 +4,7 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-service = "1"
|
||||
actix-session = "0.4"
|
||||
futures = "0.3"
|
||||
actix-web = "4"
|
||||
actix-session = "0.5.0-beta.8"
|
||||
futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
|
||||
env_logger = "0.7"
|
||||
|
@ -1,23 +1,18 @@
|
||||
// <default-headers>
|
||||
use actix_web::{http, middleware, HttpResponse};
|
||||
use actix_web::{http::Method, middleware, web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||
.wrap(middleware::DefaultHeaders::new().add(("X-Version", "0.2")))
|
||||
.service(
|
||||
web::resource("/test")
|
||||
.route(web::get().to(|| HttpResponse::Ok()))
|
||||
.route(
|
||||
web::method(http::Method::HEAD)
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
),
|
||||
.route(web::get().to(HttpResponse::Ok))
|
||||
.route(web::method(Method::HEAD).to(HttpResponse::MethodNotAllowed)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,31 +1,33 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <error-handler>
|
||||
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{dev, http, web, App, HttpResponse, HttpServer, Result};
|
||||
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
|
||||
use actix_web::{
|
||||
dev,
|
||||
http::{header, StatusCode},
|
||||
web, App, HttpResponse, HttpServer, Result,
|
||||
};
|
||||
|
||||
fn add_error_header<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
|
||||
res.response_mut().headers_mut().insert(
|
||||
http::header::CONTENT_TYPE,
|
||||
http::HeaderValue::from_static("Error"),
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("Error"),
|
||||
);
|
||||
Ok(ErrorHandlerResponse::Response(res))
|
||||
|
||||
Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(ErrorHandlers::new().handler(
|
||||
http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
add_error_header,
|
||||
))
|
||||
.service(
|
||||
web::resource("/")
|
||||
.route(web::get().to(HttpResponse::InternalServerError)),
|
||||
.wrap(
|
||||
ErrorHandlers::new()
|
||||
.handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header),
|
||||
)
|
||||
.service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(Logger::default())
|
||||
.wrap(Logger::new("%a %{User-Agent}i"))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -5,13 +5,13 @@ pub mod user_sessions;
|
||||
pub mod wrap_fn;
|
||||
|
||||
// <simple>
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::future::{ready, Ready};
|
||||
|
||||
use actix_service::{Service, Transform};
|
||||
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
||||
use futures::future::{ok, Ready};
|
||||
use futures::Future;
|
||||
use actix_web::{
|
||||
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
|
||||
Error,
|
||||
};
|
||||
use futures_util::future::LocalBoxFuture;
|
||||
|
||||
// There are two steps in middleware processing.
|
||||
// 1. Middleware initialization, middleware factory gets called with
|
||||
@ -19,16 +19,15 @@ use futures::Future;
|
||||
// 2. Middleware's call method gets called with normal request.
|
||||
pub struct SayHi;
|
||||
|
||||
// Middleware factory is `Transform` trait from actix-service crate
|
||||
// Middleware factory is `Transform` trait
|
||||
// `S` - type of the next service
|
||||
// `B` - type of response's body
|
||||
impl<S, B> Transform<S> for SayHi
|
||||
impl<S, B> Transform<S, ServiceRequest> for SayHi
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
@ -36,7 +35,7 @@ where
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(SayHiMiddleware { service })
|
||||
ready(Ok(SayHiMiddleware { service }))
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,22 +43,19 @@ pub struct SayHiMiddleware<S> {
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl<S, B> Service for SayHiMiddleware<S>
|
||||
impl<S, B> Service<ServiceRequest> for SayHiMiddleware<S>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.poll_ready(cx)
|
||||
}
|
||||
forward_ready!(service);
|
||||
|
||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
println!("Hi from start. You requested: {}", req.path());
|
||||
|
||||
let fut = self.service.call(req);
|
||||
@ -80,13 +76,12 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().wrap(SayHi).service(
|
||||
web::resource("/")
|
||||
.to(|| async {
|
||||
"Hello, middleware! Check the console where the server is run."
|
||||
}),
|
||||
web::resource("/").to(|| async {
|
||||
"Hello, middleware! Check the console where the server is run."
|
||||
}),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||
async fn index(session: Session) -> Result<HttpResponse, Error> {
|
||||
// access session data
|
||||
if let Some(count) = session.get::<i32>("counter")? {
|
||||
session.set("counter", count + 1)?;
|
||||
session.insert("counter", count + 1)?;
|
||||
} else {
|
||||
session.set("counter", 1)?;
|
||||
session.insert("counter", 1)?;
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().body(format!(
|
||||
@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
||||
)
|
||||
.service(web::resource("/").to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,9 +1,8 @@
|
||||
#![allow(dead_code, unused_variables)]
|
||||
|
||||
// <wrap-fn>
|
||||
use actix_web::dev::Service;
|
||||
use actix_web::{web, App};
|
||||
use futures::future::FutureExt;
|
||||
use actix_web::{dev::Service as _, web, App};
|
||||
use futures_util::future::FutureExt;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
@ -17,9 +16,7 @@ async fn main() {
|
||||
})
|
||||
.route(
|
||||
"/index.html",
|
||||
web::get().to(|| async {
|
||||
"Hello, middleware!"
|
||||
}),
|
||||
web::get().to(|| async { "Hello, middleware!" }),
|
||||
);
|
||||
}
|
||||
// </wrap-fn>
|
||||
|
@ -4,5 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
serde = "1.0"
|
||||
|
@ -38,7 +38,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/", web::get().to(index))
|
||||
.route("/event", web::post().to(capture_event))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,4 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
|
@ -42,11 +42,11 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(data.clone())
|
||||
.app_data(web::Data::new(data.clone()))
|
||||
.service(show_count)
|
||||
.service(add_one)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -29,11 +29,11 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(data.clone())
|
||||
.app_data(web::Data::new(data.clone()))
|
||||
.route("/", web::to(show_count))
|
||||
.route("/add", web::to(add_one))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,4 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
|
@ -16,7 +16,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(web::resource("/").to(index))
|
||||
.service(web::resource("/{name}").to(hello))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -6,6 +6,6 @@ edition = "2018"
|
||||
[dependencies]
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
futures = "0.3.1"
|
||||
actix-multipart = "0.3"
|
||||
|
@ -20,7 +20,7 @@ async fn index(info: web::Json<Info>) -> Result<String> {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| App::new().service(index_manual))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
serde = "1.0"
|
||||
futures = "0.3.1"
|
||||
bytes = "0.5"
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <auto>
|
||||
use actix_web::{get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer};
|
||||
use actix_web::{get, middleware, App, HttpResponse, HttpServer};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
@ -10,10 +10,10 @@ async fn index() -> HttpResponse {
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||
.wrap(middleware::Compress::default())
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,13 +1,9 @@
|
||||
// <brotli>
|
||||
use actix_web::{
|
||||
dev::BodyEncoding, get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
};
|
||||
use actix_web::{get, middleware, App, HttpResponse, HttpServer};
|
||||
|
||||
#[get("/")]
|
||||
async fn index_br() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Br)
|
||||
.body("data")
|
||||
HttpResponse::Ok().body("data")
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
@ -17,7 +13,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::Compress::default())
|
||||
.service(index_br)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::Compress::new(ContentEncoding::Br))
|
||||
.route("/", web::get().to(index_br))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::io;
|
||||
|
||||
// <chunked>
|
||||
use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use futures::future::ok;
|
||||
@ -10,9 +12,10 @@ async fn index(_req: HttpRequest) -> HttpResponse {
|
||||
// </chunked>
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
async fn main() -> io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")
|
||||
.bind(("127.0.0.1", 8080))
|
||||
.unwrap()
|
||||
.run();
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::Compress::default())
|
||||
.service(index_br)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
// <identity>
|
||||
use actix_web::{
|
||||
dev::BodyEncoding, get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
get, http::header::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
// v- disable compression
|
||||
.encoding(ContentEncoding::Identity)
|
||||
.insert_header(ContentEncoding::Identity)
|
||||
.body("data")
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::Compress::default())
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
// <identity-two>
|
||||
use actix_web::{
|
||||
dev::BodyEncoding, get, http::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
get, http::header::ContentEncoding, middleware, App, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
static HELLO_WORLD: &[u8] = &[
|
||||
0x1f, 0x8b, 0x08, 0x00, 0xa2, 0x30, 0x10, 0x5c, 0x00, 0x03, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0x57,
|
||||
0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00, 0x00, 0x00,
|
||||
0x1f, 0x8b, 0x08, 0x00, 0xa2, 0x30, 0x10, 0x5c, 0x00, 0x03, 0xcb, 0x48, 0xcd, 0xc9, 0xc9,
|
||||
0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
|
||||
0x00, 0x00,
|
||||
];
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.encoding(ContentEncoding::Identity)
|
||||
.header("content-encoding", "gzip")
|
||||
.insert_header(ContentEncoding::Gzip)
|
||||
.body(HELLO_WORLD)
|
||||
}
|
||||
// </identity-two>
|
||||
@ -24,7 +24,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::Compress::default())
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -7,12 +7,12 @@ pub mod identity_two;
|
||||
pub mod json_resp;
|
||||
|
||||
// <builder>
|
||||
use actix_web::HttpResponse;
|
||||
use actix_web::{http::header::ContentType, HttpResponse};
|
||||
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/plain")
|
||||
.header("X-Hdr", "sample")
|
||||
.content_type(ContentType::plaintext())
|
||||
.insert_header(("X-Hdr", "sample"))
|
||||
.body("data")
|
||||
}
|
||||
// </builder>
|
||||
@ -22,7 +22,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
|
||||
}
|
||||
|
@ -1,3 +1,2 @@
|
||||
max_width = 79
|
||||
max_width = 96
|
||||
reorder_imports = true
|
||||
fn_args_density = "Compressed"
|
||||
|
@ -1 +0,0 @@
|
||||
max_width = 80
|
@ -3,21 +3,17 @@ use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let one = HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.keep_alive(75); // <- Set keep-alive to 75 seconds
|
||||
let one = HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
|
||||
.keep_alive(75); // <- Set keep-alive to 75 seconds
|
||||
|
||||
// let _two = HttpServer::new(|| {
|
||||
// App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
// })
|
||||
// .keep_alive(); // <- Use `SO_KEEPALIVE` socket option.
|
||||
|
||||
let _three = HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.keep_alive(None); // <- Disable keep-alive
|
||||
let _three = HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
|
||||
.keep_alive(None); // <- Disable keep-alive
|
||||
|
||||
one.bind("127.0.0.1:8080")?.run().await
|
||||
one.bind(("127.0.0.1", 8080))?.run().await
|
||||
}
|
||||
// </keep-alive>
|
||||
|
@ -9,11 +9,9 @@ use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </main>
|
||||
|
@ -10,12 +10,10 @@ async fn main() {
|
||||
thread::spawn(move || {
|
||||
let sys = System::new("http-server");
|
||||
|
||||
let srv = HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
||||
.run();
|
||||
let srv = HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok)))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
||||
.run();
|
||||
|
||||
let _ = tx.send(srv);
|
||||
sys.run()
|
||||
|
@ -14,8 +14,7 @@ async fn main() -> std::io::Result<()> {
|
||||
// load ssl keys
|
||||
// to create a self-signed temporary cert for testing:
|
||||
// `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
|
||||
let mut builder =
|
||||
SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
builder
|
||||
.set_private_key_file("key.pem", SslFiletype::PEM)
|
||||
.unwrap();
|
||||
|
@ -3,9 +3,7 @@ use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new().route("/", web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.workers(4); // <- Start 4 workers
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(HttpResponse::Ok))).workers(4);
|
||||
// <- Start 4 workers
|
||||
}
|
||||
// </workers>
|
||||
|
@ -4,6 +4,6 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-files = "0.3"
|
||||
actix-web = "4"
|
||||
actix-files = "0.6"
|
||||
mime = "0.3"
|
||||
|
@ -18,7 +18,7 @@ async fn index(req: HttpRequest) -> Result<fs::NamedFile, 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
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.use_last_modified(true),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,11 +4,9 @@ use actix_web::{App, HttpServer};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().service(fs::Files::new("/static", ".").show_files_listing())
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(fs::Files::new("/static", ".").show_files_listing()))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </directory>
|
||||
|
@ -17,7 +17,7 @@ async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index)))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -4,12 +4,9 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
futures = "0.3"
|
||||
futures-util = "0.3"
|
||||
bytes = "0.5"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "1"
|
||||
|
@ -8,22 +8,25 @@ async fn index(_req: HttpRequest) -> impl Responder {
|
||||
// <integration-one>
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use actix_web::{test, web, App};
|
||||
use actix_web::{http::header::ContentType, test, web, App};
|
||||
|
||||
#[actix_rt::test]
|
||||
use super::*;
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_index_get() {
|
||||
let mut app = test::init_service(App::new().route("/", web::get().to(index))).await;
|
||||
let req = test::TestRequest::with_header("content-type", "text/plain").to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
let app = test::init_service(App::new().route("/", web::get().to(index))).await;
|
||||
let req = test::TestRequest::default()
|
||||
.insert_header(ContentType::plaintext())
|
||||
.to_request();
|
||||
let resp = test::call_service(&app, req).await;
|
||||
assert!(resp.status().is_success());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
#[actix_web::test]
|
||||
async fn test_index_post() {
|
||||
let mut app = test::init_service(App::new().route("/", web::get().to(index))).await;
|
||||
let app = test::init_service(App::new().route("/", web::get().to(index))).await;
|
||||
let req = test::TestRequest::post().uri("/").to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
let resp = test::call_service(&app, req).await;
|
||||
assert!(resp.status().is_client_error());
|
||||
}
|
||||
}
|
||||
|
@ -17,16 +17,16 @@ mod tests {
|
||||
use super::*;
|
||||
use actix_web::{test, web, App};
|
||||
|
||||
#[actix_rt::test]
|
||||
#[actix_web::test]
|
||||
async fn test_index_get() {
|
||||
let mut app = test::init_service(
|
||||
let app = test::init_service(
|
||||
App::new()
|
||||
.data(AppState { count: 4 })
|
||||
.app_data(web::Data::new(AppState { count: 4 }))
|
||||
.route("/", web::get().to(index)),
|
||||
)
|
||||
.await;
|
||||
let req = test::TestRequest::get().uri("/").to_request();
|
||||
let resp: AppState = test::read_response_json(&mut app, req).await;
|
||||
let resp: AppState = test::call_and_read_body_json(&app, req).await;
|
||||
|
||||
assert_eq!(resp.count, 4);
|
||||
}
|
||||
|
@ -20,16 +20,21 @@ fn main() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use actix_web::{http, test};
|
||||
use actix_web::{
|
||||
http::{self, header::ContentType},
|
||||
test,
|
||||
};
|
||||
|
||||
#[actix_rt::test]
|
||||
#[actix_web::test]
|
||||
async fn test_index_ok() {
|
||||
let req = test::TestRequest::with_header("content-type", "text/plain").to_http_request();
|
||||
let req = test::TestRequest::default()
|
||||
.insert_header(ContentType::plaintext())
|
||||
.to_http_request();
|
||||
let resp = index(req).await;
|
||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
#[actix_web::test]
|
||||
async fn test_index_not_ok() {
|
||||
let req = test::TestRequest::default().to_http_request();
|
||||
let resp = index(req).await;
|
||||
|
@ -1,29 +1,29 @@
|
||||
// <stream-response>
|
||||
use futures::stream::poll_fn;
|
||||
use std::task::Poll;
|
||||
|
||||
use actix_web::http::{ContentEncoding, StatusCode};
|
||||
use actix_web::{http, web, App, Error, HttpRequest, HttpResponse};
|
||||
use actix_web::{
|
||||
http::{self, header::ContentEncoding, StatusCode},
|
||||
web, App, Error, HttpRequest, HttpResponse,
|
||||
};
|
||||
use futures::stream;
|
||||
|
||||
async fn sse(_req: HttpRequest) -> HttpResponse {
|
||||
let mut counter: usize = 5;
|
||||
|
||||
// yields `data: N` where N in [5; 1]
|
||||
let server_events = poll_fn(move |_cx| -> Poll<Option<Result<web::Bytes, Error>>> {
|
||||
if counter == 0 {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
let payload = format!("data: {}\n\n", counter);
|
||||
counter -= 1;
|
||||
Poll::Ready(Some(Ok(web::Bytes::from(payload))))
|
||||
});
|
||||
let server_events =
|
||||
stream::poll_fn(move |_cx| -> Poll<Option<Result<web::Bytes, Error>>> {
|
||||
if counter == 0 {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
let payload = format!("data: {}\n\n", counter);
|
||||
counter -= 1;
|
||||
Poll::Ready(Some(Ok(web::Bytes::from(payload))))
|
||||
});
|
||||
|
||||
HttpResponse::build(StatusCode::OK)
|
||||
.set_header(http::header::CONTENT_TYPE, "text/event-stream")
|
||||
.set_header(
|
||||
http::header::CONTENT_ENCODING,
|
||||
ContentEncoding::Identity.as_str(),
|
||||
)
|
||||
.insert_header((http::header::CONTENT_TYPE, "text/event-stream"))
|
||||
.insert_header(ContentEncoding::Identity)
|
||||
.streaming(server_events)
|
||||
}
|
||||
|
||||
@ -35,39 +35,41 @@ pub fn main() {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use futures_util::stream::StreamExt;
|
||||
use futures_util::stream::TryStreamExt;
|
||||
use actix_web::{body::MessageBody as _, rt::pin, test, web, App};
|
||||
use futures::future;
|
||||
|
||||
use actix_web::{test, web, App};
|
||||
|
||||
#[actix_rt::test]
|
||||
#[actix_web::test]
|
||||
async fn test_stream() {
|
||||
let mut app = test::init_service(App::new().route("/", web::get().to(sse))).await;
|
||||
let app = test::init_service(App::new().route("/", web::get().to(sse))).await;
|
||||
let req = test::TestRequest::get().to_request();
|
||||
|
||||
let mut resp = test::call_service(&mut app, req).await;
|
||||
let resp = test::call_service(&app, req).await;
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
let body = resp.into_body();
|
||||
pin!(body);
|
||||
|
||||
// first chunk
|
||||
let (bytes, mut resp) = resp.take_body().into_future().await;
|
||||
let bytes = future::poll_fn(|cx| body.as_mut().poll_next(cx)).await;
|
||||
assert_eq!(
|
||||
bytes.unwrap().unwrap(),
|
||||
web::Bytes::from_static(b"data: 5\n\n")
|
||||
);
|
||||
|
||||
// second chunk
|
||||
let (bytes, mut resp) = resp.take_body().into_future().await;
|
||||
let bytes = future::poll_fn(|cx| body.as_mut().poll_next(cx)).await;
|
||||
assert_eq!(
|
||||
bytes.unwrap().unwrap(),
|
||||
web::Bytes::from_static(b"data: 4\n\n")
|
||||
);
|
||||
|
||||
// remaining part
|
||||
let bytes = test::load_stream(resp.take_body().into_stream()).await;
|
||||
assert_eq!(
|
||||
bytes.unwrap(),
|
||||
web::Bytes::from_static(b"data: 3\n\ndata: 2\n\ndata: 1\n\n")
|
||||
);
|
||||
// TODO: fix this example
|
||||
// // remaining part
|
||||
// let bytes = body::to_bytes(body).await;
|
||||
// assert_eq!(
|
||||
// bytes.unwrap(),
|
||||
// web::Bytes::from_static(b"data: 3\n\ndata: 2\n\ndata: 1\n\n")
|
||||
// );
|
||||
}
|
||||
}
|
||||
// </stream-response>
|
||||
|
@ -6,7 +6,7 @@ workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix = "0.10"
|
||||
actix-web = "3"
|
||||
actix-web = "4"
|
||||
futures = "0.3.1"
|
||||
openssl = "0.10"
|
||||
serde = "1.0"
|
||||
|
@ -12,12 +12,12 @@ App::new().service(
|
||||
web::route()
|
||||
.guard(guard::Get())
|
||||
.guard(guard::Header("content-type", "text/plain"))
|
||||
.to(|| HttpResponse::Ok()),
|
||||
.to(HttpResponse::Ok),
|
||||
),
|
||||
)
|
||||
// </cfg>
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -14,10 +14,10 @@ async fn main() -> std::io::Result<()> {
|
||||
.default_service(
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
.to(HttpResponse::MethodNotAllowed),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,11 +1,16 @@
|
||||
// <guard>
|
||||
use actix_web::{dev::RequestHead, guard::Guard, http, HttpResponse};
|
||||
use actix_web::{
|
||||
guard::{Guard, GuardContext},
|
||||
http, HttpResponse,
|
||||
};
|
||||
|
||||
struct ContentTypeHeader;
|
||||
|
||||
impl Guard for ContentTypeHeader {
|
||||
fn check(&self, req: &RequestHead) -> bool {
|
||||
req.headers().contains_key(http::header::CONTENT_TYPE)
|
||||
fn check(&self, req: &GuardContext) -> bool {
|
||||
req.head()
|
||||
.headers()
|
||||
.contains_key(http::header::CONTENT_TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,12 +21,10 @@ async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/",
|
||||
web::route()
|
||||
.guard(ContentTypeHeader)
|
||||
.to(|| HttpResponse::Ok()),
|
||||
web::route().guard(ContentTypeHeader).to(HttpResponse::Ok),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ async fn main() -> std::io::Result<()> {
|
||||
"/",
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
.to(HttpResponse::MethodNotAllowed),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/", web::get().to(index))
|
||||
.route("/user", web::post().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ async fn index(req: HttpRequest) -> Result<String> {
|
||||
#[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
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::NormalizePath::default())
|
||||
.route("/resource/", web::to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user