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

Final fixes before requesting review.

This commit is contained in:
Cameron Dershem
2019-06-28 13:31:30 -04:00
parent cbf046b1f0
commit 5133ab874d
40 changed files with 116 additions and 81 deletions

View File

@ -1,7 +1,7 @@
// <setup>
use actix_web::{web, App, HttpRequest, Responder};
use actix_web::{web, App, Responder};
fn index(_req: HttpRequest) -> impl Responder {
fn index() -> impl Responder {
"Hello world!"
}

View File

@ -3,12 +3,10 @@ fn is_error() -> bool {
}
// <async-stream>
use actix_web::{error, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use actix_web::{error, Error, HttpResponse};
use futures::future::{result, Future};
fn index(
_req: HttpRequest,
) -> Result<Box<Future<Item = HttpResponse, Error = Error>>, Error> {
fn index() -> Result<Box<Future<Item = HttpResponse, Error = Error>>, Error> {
if is_error() {
Err(error::ErrorBadRequest("bad request"))
} else {
@ -20,6 +18,8 @@ fn index(
// </async-stream>
pub fn main() {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::to_async(index)))
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,22 +1,30 @@
pub mod async_stream;
pub mod stream;
// <async-responder>
use actix_web::{web, App, Error, HttpRequest, HttpResponse};
use actix_web::{Error, HttpResponse};
use futures::future::{ok, Future};
fn index(_req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
fn index() -> Box<Future<Item = HttpResponse, Error = Error>> {
Box::new(ok::<_, Error>(
HttpResponse::Ok().content_type("text/html").body("Hello!"),
))
}
fn index2(_req: HttpRequest) -> Box<Future<Item = &'static str, Error = Error>> {
fn index2() -> Box<Future<Item = &'static str, Error = Error>> {
Box::new(ok::<_, Error>("Welcome!"))
}
fn main() {
App::new()
.route("/async", web::to_async(index))
.route("/", web::to_async(index2));
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()
.route("/async", web::to_async(index))
.route("/", web::to_async(index2))
})
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}
// </async-responder>

View File

@ -1,9 +1,9 @@
// <stream>
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer};
use actix_web::{Error, HttpResponse};
use bytes::Bytes;
use futures::stream::once;
fn index(_req: HttpRequest) -> HttpResponse {
fn index() -> HttpResponse {
let body = once::<Bytes, Error>(Ok(Bytes::from_static(b"test")));
HttpResponse::Ok()
@ -12,6 +12,8 @@ fn index(_req: HttpRequest) -> HttpResponse {
}
pub fn main() {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/async", web::to_async(index)))
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,11 +1,11 @@
// <either>
use actix_web::{web, App, Either, Error, HttpRequest, HttpResponse};
use actix_web::{Either, Error, HttpResponse};
use futures::future::{ok, Future};
type RegisterResult =
Either<HttpResponse, Box<Future<Item = HttpResponse, Error = Error>>>;
fn index(_req: HttpRequest) -> RegisterResult {
fn index() -> RegisterResult {
if is_a_variant() {
// <- choose variant A
Either::A(HttpResponse::BadRequest().body("Bad data"))
@ -20,7 +20,13 @@ fn index(_req: HttpRequest) -> RegisterResult {
}
fn main() {
App::new().route("/", web::get().to(index));
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}
// </either>

View File

@ -1,13 +1,13 @@
use actix_web::{web, App};
// <helpers>
use actix_web::{error, HttpRequest, Result};
use actix_web::{error, Result};
#[derive(Debug)]
struct MyError {
name: &'static str,
}
pub fn index(_req: HttpRequest) -> Result<&'static str> {
pub fn index() -> Result<&'static str> {
let result: Result<&'static str, MyError> = Err(MyError { name: "test error" });
Ok(result.map_err(|e| error::ErrorBadRequest(e.name))?)

View File

@ -16,7 +16,7 @@ pub struct MyError {
// Use default implementation for `error_response()` method
impl error::ResponseError for MyError {}
fn index(_req: HttpRequest) -> Result<&'static str, MyError> {
fn index() -> Result<&'static str, MyError> {
Err(MyError { name: "test" })
}
// </response-error>

View File

@ -1,6 +1,6 @@
use actix_web::{web, App};
// <override>
use actix_web::{error, http, HttpRequest, HttpResponse};
use actix_web::{error, http, HttpResponse};
use failure::Fail;
#[derive(Fail, Debug)]
@ -25,16 +25,16 @@ impl error::ResponseError for MyError {
}
}
fn index(_req: HttpRequest) -> Result<&'static str, MyError> {
fn index() -> Result<&'static str, MyError> {
Err(MyError::BadClientData)
}
// </override>
fn error2(_req: HttpRequest) -> Result<&'static str, MyError> {
fn error2() -> Result<&'static str, MyError> {
Err(MyError::InternalError)
}
fn error3(_req: HttpRequest) -> Result<&'static str, MyError> {
fn error3() -> Result<&'static str, MyError> {
Err(MyError::Timeout)
}

View File

@ -1,5 +1,5 @@
// <form>
use actix_web::{web, App, HttpServer, Result};
use actix_web::{web, Result};
use serde::Deserialize;
#[derive(Deserialize)]
@ -16,6 +16,8 @@ fn index(form: web::Form<FormData>) -> Result<String> {
// </form>
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,5 +1,5 @@
// <json-one>
use actix_web::{web, App, HttpServer, Result};
use actix_web::{web, Result};
use serde::Deserialize;
#[derive(Deserialize)]
@ -14,6 +14,8 @@ fn index(info: web::Json<Info>) -> Result<String> {
// </json-one>
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,5 +1,5 @@
// <json-two>
use actix_web::{error, web, App, FromRequest, HttpResponse, HttpServer, Responder};
use actix_web::{error, web, FromRequest, HttpResponse, Responder};
use serde::Deserialize;
#[derive(Deserialize)]
@ -13,6 +13,8 @@ fn index(info: web::Json<Info>) -> impl Responder {
}
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().service(
web::resource("/")

View File

@ -1,5 +1,5 @@
// <multi>
use actix_web::{web, App, HttpServer};
use actix_web::web;
use serde::Deserialize;
#[derive(Deserialize)]
@ -15,6 +15,8 @@ fn index((path, query): (web::Path<(u32, String)>, web::Query<Info>)) -> String
}
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(
"/users/{userid}/{friend}", // <- define path parameters

View File

@ -1,5 +1,5 @@
// <path-one>
use actix_web::{web, App, HttpServer, Result};
use actix_web::{web, Result};
/// extract path info from "/users/{userid}/{friend}" url
/// {userid} - - deserializes to a u32
@ -9,6 +9,8 @@ fn index(info: web::Path<(u32, String)>) -> Result<String> {
}
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(
"/users/{userid}/{friend}", // <- define path parameters

View File

@ -1,4 +1,4 @@
use actix_web::{web, App, HttpRequest, HttpServer, Result};
use actix_web::{web, HttpRequest, Result};
// <path-three>
fn index(req: HttpRequest) -> Result<String> {
@ -9,6 +9,8 @@ fn index(req: HttpRequest) -> Result<String> {
}
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(
"/users/{userid}/{friend}", // <- define path parameters

View File

@ -1,5 +1,5 @@
// <path-two>
use actix_web::{web, App, HttpServer, Result};
use actix_web::{web, Result};
use serde::Deserialize;
#[derive(Deserialize)]
@ -14,6 +14,8 @@ fn index(info: web::Path<Info>) -> Result<String> {
}
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(
"/users/{userid}/{friend}", // <- define path parameters

View File

@ -1,5 +1,5 @@
// <query>
use actix_web::{web, App, HttpServer};
use actix_web::web;
use serde::Deserialize;
#[derive(Deserialize)]
@ -14,6 +14,8 @@ fn index(info: web::Query<Info>) -> String {
// </query>
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,11 +1,11 @@
// <setup>
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
fn index() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
fn index2(_req: HttpRequest) -> impl Responder {
fn index2() -> impl Responder {
HttpResponse::Ok().body("Hello world again!")
}
// </setup>

View File

@ -1,5 +1,5 @@
// <arc>
use actix_web::{web, App, HttpServer, Responder};
use actix_web::{web, Responder};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
@ -19,6 +19,8 @@ fn add_one(data: web::Data<AppState>) -> impl Responder {
}
pub fn main() {
use actix_web::{App, HttpServer};
let data = AppState {
count: Arc::new(AtomicUsize::new(0)),
};

View File

@ -1,6 +1,6 @@
pub mod handlers_arc;
// <data>
use actix_web::{web, App, HttpServer, Responder};
use actix_web::{web, Responder};
use std::cell::Cell;
#[derive(Clone)]
@ -20,6 +20,8 @@ fn add_one(data: web::Data<AppState>) -> impl Responder {
}
fn main() {
use actix_web::{App, HttpServer};
let data = AppState {
count: Cell::new(0),
};

View File

@ -1,5 +1,5 @@
// <responder-trait>
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::{Error, HttpRequest, HttpResponse, Responder};
use serde::Serialize;
#[derive(Serialize)]
@ -28,6 +28,8 @@ fn index() -> impl Responder {
// </responder-trait>
fn main() {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,4 +1,4 @@
// <chunked>
// // <chunked>
// use actix_web::{web, HttpRequest, HttpResponse};
// use bytes::Bytes;
// use futures::stream::once;
@ -10,7 +10,7 @@
// b"data",
// ))))))
// }
// </chunked>
// // </chunked>
// pub fn main() {
// use actix_web::{web, App, HttpServer};

View File

@ -1,7 +1,6 @@
// <identity-two>
use actix_web::{
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpRequest,
HttpResponse,
http::ContentEncoding, middleware, middleware::BodyEncoding, HttpResponse,
};
static HELLO_WORLD: &[u8] = &[
@ -10,7 +9,7 @@ static HELLO_WORLD: &[u8] = &[
0x0c, 0x00, 0x00, 0x00,
];
pub fn index(_req: HttpRequest) -> HttpResponse {
pub fn index() -> HttpResponse {
HttpResponse::Ok()
.encoding(ContentEncoding::Identity)
.header("content-encoding", "gzip")

View File

@ -7,11 +7,10 @@ pub mod identity_two;
pub mod json_resp;
// <builder>
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
use actix_web::HttpResponse;
fn index() -> HttpResponse {
HttpResponse::Ok()
.encoding(ContentEncoding::Br)
.content_type("plain/text")
.header("X-Hdr", "sample")
.body("data")

View File

@ -4,7 +4,7 @@ pub mod directory;
// <individual-file>
use actix_files::NamedFile;
use actix_web::{web, App, HttpRequest, HttpServer, Result};
use actix_web::{HttpRequest, Result};
use std::path::PathBuf;
fn index(req: HttpRequest) -> Result<NamedFile> {
@ -13,6 +13,8 @@ fn index(req: HttpRequest) -> Result<NamedFile> {
}
fn main() {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index)))
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,7 +1,5 @@
// <guard>
use actix_web::{
dev::RequestHead, guard::Guard, http, web, App, HttpResponse, HttpServer,
};
use actix_web::{dev::RequestHead, guard::Guard, http, HttpResponse};
struct ContentTypeHeader;
@ -12,6 +10,8 @@ impl Guard for ContentTypeHeader {
}
pub fn main() {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new().route(
"/",

View File

@ -1,5 +1,5 @@
// <minfo>
use actix_web::{web, App, HttpRequest, Result};
use actix_web::{HttpRequest, HttpResponse, Result};
fn index(req: HttpRequest) -> Result<String> {
let v1: u8 = req.match_info().get("v1").unwrap().parse().unwrap();
@ -9,12 +9,12 @@ fn index(req: HttpRequest) -> Result<String> {
}
pub fn main() {
use actix_web::HttpServer;
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()
.route("/a/{v1}/{v2}/", web::get().to(index))
.route("", web::get().to(|| actix_web::HttpResponse::Ok()))
.route("", web::get().to(|| HttpResponse::Ok()))
})
.bind("127.0.0.1:8088")
.unwrap()

View File

@ -1,12 +1,12 @@
// <norm>
use actix_web::{middleware, web, App, HttpResponse};
use actix_web::{middleware, HttpResponse};
fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}
pub fn main() {
use actix_web::HttpServer;
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()

View File

@ -1,3 +1,9 @@
use actix_web::HttpResponse;
fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}
// <norm>
use actix_web::{http::Method, middleware, web, App, HttpServer};
@ -14,9 +20,3 @@ pub fn main() {
.unwrap();
}
// </norm>
use actix_web::HttpResponse;
fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}

View File

@ -1,12 +1,12 @@
// <path>
use actix_web::{web, App, Result};
use actix_web::{web, Result};
fn index(info: web::Path<(String, u32)>) -> Result<String> {
Ok(format!("Welcome {}! id: {}", info.0, info.1))
}
pub fn main() {
use actix_web::HttpServer;
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(

View File

@ -1,5 +1,5 @@
// <path>
use actix_web::{web, App, Result};
use actix_web::{web, Result};
use serde::Deserialize;
#[derive(Deserialize)]
@ -13,7 +13,7 @@ fn index(info: web::Path<Info>) -> Result<String> {
}
pub fn main() {
use actix_web::HttpServer;
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(

View File

@ -1,5 +1,5 @@
// <pbuf>
use actix_web::{web, App, HttpRequest, Result};
use actix_web::{HttpRequest, Result};
use std::path::PathBuf;
fn index(req: HttpRequest) -> Result<String> {
@ -8,7 +8,7 @@ fn index(req: HttpRequest) -> Result<String> {
}
pub fn main() {
use actix_web::HttpServer;
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route(r"/a/{tail:.*}", web::get().to(index)))
.bind("127.0.0.1:8088")

View File

@ -5,8 +5,8 @@ fn show_users() -> HttpResponse {
HttpResponse::Ok().body("Show users")
}
fn user_detail(_path: web::Path<(u32,)>) -> HttpResponse {
HttpResponse::Ok().body("User detail")
fn user_detail(path: web::Path<(u32,)>) -> HttpResponse {
HttpResponse::Ok().body(format!("User detail: {}", path.0))
}
pub fn main() {

View File

@ -1,5 +1,5 @@
// <ext>
use actix_web::{web, App, HttpRequest, Responder};
use actix_web::{HttpRequest, Responder};
fn index(req: HttpRequest) -> impl Responder {
let url = req.url_for("youtube", &["oHg5SJYRHA0"]).unwrap();
@ -9,7 +9,7 @@ fn index(req: HttpRequest) -> impl Responder {
}
pub fn main() {
use actix_web::HttpServer;
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()

View File

@ -1,5 +1,5 @@
// <url>
use actix_web::{guard, http::header, web, App, HttpRequest, HttpResponse, Result};
use actix_web::{guard, http::header, HttpRequest, HttpResponse, Result};
fn index(req: HttpRequest) -> Result<HttpResponse> {
let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource
@ -10,7 +10,7 @@ fn index(req: HttpRequest) -> Result<HttpResponse> {
}
pub fn main() {
use actix_web::HttpServer;
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()