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