1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 18:09:22 +02:00

re-arrange modules and exports

This commit is contained in:
Nikolay Kim
2018-03-30 17:31:18 -07:00
parent b16419348e
commit 9e751de707
49 changed files with 373 additions and 483 deletions

View File

@ -23,7 +23,7 @@ Here is an example of a simple middleware that adds request and response headers
# extern crate http;
# extern crate actix_web;
use http::{header, HttpTryFrom};
use actix_web::*;
use actix_web::{Application, HttpRequest, HttpResponse, Result, httpcodes};
use actix_web::middleware::{Middleware, Started, Response};
struct Headers; // <- Our middleware
@ -135,7 +135,7 @@ the specified header.
```rust
# extern crate actix_web;
use actix_web::*;
use actix_web::{Application, http, httpcodes, middleware};
fn main() {
let app = Application::new()
@ -144,8 +144,8 @@ fn main() {
.header("X-Version", "0.2")
.finish())
.resource("/test", |r| {
r.method(Method::GET).f(|req| httpcodes::HttpOk);
r.method(Method::HEAD).f(|req| httpcodes::HttpMethodNotAllowed);
r.method(http::Method::GET).f(|req| httpcodes::HttpOk);
r.method(http::Method::HEAD).f(|req| httpcodes::HttpMethodNotAllowed);
})
.finish();
}

View File

@ -7,12 +7,12 @@ match path tail we can use `[.*]` regex.
```rust
# extern crate actix_web;
use actix_web::*;
use std::path::PathBuf;
use actix_web::{Application, HttpRequest, Result, http::Method, fs::NamedFile};
fn index(req: HttpRequest) -> Result<fs::NamedFile> {
fn index(req: HttpRequest) -> Result<NamedFile> {
let path: PathBuf = req.match_info().query("tail")?;
Ok(fs::NamedFile::open(path)?)
Ok(NamedFile::open(path)?)
}
fn main() {

View File

@ -80,8 +80,8 @@ in the state:
# extern crate actix;
# extern crate actix_web;
#
use actix_web::*;
use std::cell::Cell;
use actix_web::{Application, HttpRequest, http};
// This struct represents state
struct AppState {
@ -97,7 +97,7 @@ fn index(req: HttpRequest<AppState>) -> String {
fn main() {
Application::with_state(AppState{counter: Cell::new(0)})
.resource("/", |r| r.method(Method::GET).f(index))
.resource("/", |r| r.method(http::Method::GET).f(index))
.finish();
}
```

View File

@ -172,11 +172,11 @@ and is on for *HTTP/1.1* and *HTTP/2.0*.
```rust
# extern crate actix_web;
use actix_web::{header, HttpRequest, HttpResponse, httpcodes::HttpOk};
use actix_web::{HttpRequest, HttpResponse, http, httpcodes::HttpOk};
fn index(req: HttpRequest) -> HttpResponse {
HttpOk.build()
.connection_type(header::ConnectionType::Close) // <- Close connection
.connection_type(http::ConnectionType::Close) // <- Close connection
.force_close() // <- Alternative method
.finish().unwrap()
}

View File

@ -130,7 +130,7 @@ Let's create a response for a custom type that serializes to an `application/jso
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
use actix_web::*;
use actix_web::{Application, HttpServer, HttpRequest, HttpResponse, Error, Responder, http};
#[derive(Serialize)]
struct MyObj {
@ -142,7 +142,7 @@ impl Responder for MyObj {
type Item = HttpResponse;
type Error = Error;
fn respond_to(self, req: HttpRequest) -> Result<HttpResponse> {
fn respond_to(self, req: HttpRequest) -> Result<HttpResponse, Error> {
let body = serde_json::to_string(&self)?;
// Create response and set content type
@ -162,7 +162,7 @@ fn main() {
HttpServer::new(
|| Application::new()
.resource("/", |r| r.method(Method::GET).f(index)))
.resource("/", |r| r.method(http::Method::GET).f(index)))
.bind("127.0.0.1:8088").unwrap()
.start();

View File

@ -70,7 +70,7 @@ to return different responses for different types of errors.
```rust
# extern crate actix_web;
#[macro_use] extern crate failure;
use actix_web::*;
use actix_web::{Application, Body, HttpRequest, HttpResponse, http, error};
#[derive(Fail, Debug)]
enum MyError {
@ -86,11 +86,11 @@ impl error::ResponseError for MyError {
fn error_response(&self) -> HttpResponse {
match *self {
MyError::InternalError => HttpResponse::new(
StatusCode::INTERNAL_SERVER_ERROR, Body::Empty),
http::StatusCode::INTERNAL_SERVER_ERROR, Body::Empty),
MyError::BadClientData => HttpResponse::new(
StatusCode::BAD_REQUEST, Body::Empty),
http::StatusCode::BAD_REQUEST, Body::Empty),
MyError::Timeout => HttpResponse::new(
StatusCode::GATEWAY_TIMEOUT, Body::Empty),
http::StatusCode::GATEWAY_TIMEOUT, Body::Empty),
}
}
}

View File

@ -21,8 +21,8 @@ and a resource configuration function.
```rust
# extern crate actix_web;
# use actix_web::*;
# use actix_web::httpcodes::*;
# use actix_web::{Application, HttpRequest, HttpResponse, http::Method};
# use actix_web::httpcodes::HttpOk;
#
# fn index(req: HttpRequest) -> HttpResponse {
# unimplemented!()
@ -305,8 +305,8 @@ safe to interpolate within, or use as a suffix of, a path without additional che
```rust
# extern crate actix_web;
use actix_web::*;
use std::path::PathBuf;
use actix_web::{Application, HttpRequest, Result, http::Method};
fn index(req: HttpRequest) -> Result<String> {
let path: PathBuf = req.match_info().query("tail")?;
@ -335,7 +335,7 @@ has to implement *serde's *`Deserialize` trait.
# extern crate actix_web;
# extern crate futures;
#[macro_use] extern crate serde_derive;
use actix_web::*;
use actix_web::{Application, Path, Result, http::Method};
#[derive(Deserialize)]
struct Info {
@ -366,8 +366,8 @@ resource with the name "foo" and the pattern "{a}/{b}/{c}", you might do this:
```rust
# extern crate actix_web;
# use actix_web::*;
# use actix_web::httpcodes::*;
# use actix_web::{Application, HttpRequest, HttpResponse, http::Method};
# use actix_web::httpcodes::HttpOk;
#
fn index(req: HttpRequest) -> HttpResponse {
let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource
@ -378,7 +378,7 @@ fn main() {
let app = Application::new()
.resource("/test/{a}/{b}/{c}", |r| {
r.name("foo"); // <- set resource name, then it could be used in `url_for`
r.method(Method::GET).f(|_| httpcodes::HttpOk);
r.method(Method::GET).f(|_| HttpOk);
})
.finish();
}
@ -437,7 +437,7 @@ This handler designed to be use as a handler for application's *default resource
# extern crate actix_web;
# #[macro_use] extern crate serde_derive;
# use actix_web::*;
use actix_web::helpers::NormalizePath;
use actix_web::http::NormalizePath;
#
# fn index(req: HttpRequest) -> httpcodes::StaticResponse {
# httpcodes::HttpOk
@ -462,8 +462,7 @@ It is possible to register path normalization only for *GET* requests only:
```rust
# extern crate actix_web;
# #[macro_use] extern crate serde_derive;
# use actix_web::*;
use actix_web::helpers::NormalizePath;
use actix_web::{Application, HttpRequest, http::Method, http::NormalizePath, httpcodes};
#
# fn index(req: HttpRequest) -> httpcodes::StaticResponse {
# httpcodes::HttpOk
@ -597,9 +596,8 @@ with `Application::resource()` method.
```rust
# extern crate actix_web;
# extern crate http;
use actix_web::*;
use actix_web::httpcodes::*;
use actix_web::{Application, http::Method, pred};
use actix_web::httpcodes::{HttpNotFound, HttpMethodNotAllowed};
fn main() {
Application::new()

View File

@ -12,7 +12,7 @@ builder instance multiple times, the builder will panic.
```rust
# extern crate actix_web;
use actix_web::{HttpRequest, HttpResponse, header::ContentEncoding};
use actix_web::{HttpRequest, HttpResponse, http::ContentEncoding};
fn index(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
@ -45,7 +45,7 @@ to enable `brotli` use `ContentEncoding::Br`:
```rust
# extern crate actix_web;
use actix_web::{HttpRequest, HttpResponse, header::ContentEncoding};
use actix_web::{HttpRequest, HttpResponse, http::ContentEncoding};
fn index(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
@ -135,7 +135,7 @@ type `T` must implement the `Serialize` trait from *serde*.
```rust
# extern crate actix_web;
#[macro_use] extern crate serde_derive;
use actix_web::*;
use actix_web::{Application, HttpRequest, Json, Result, http::Method};
#[derive(Serialize)]
struct MyObj {