mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
rename Application
This commit is contained in:
@ -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::{Application, HttpRequest, HttpResponse, Result};
|
||||
use actix_web::{App, HttpRequest, HttpResponse, Result};
|
||||
use actix_web::middleware::{Middleware, Started, Response};
|
||||
|
||||
struct Headers; // <- Our middleware
|
||||
@ -51,7 +51,7 @@ impl<S> Middleware<S> for Headers {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.middleware(Headers) // <- Register middleware, this method can be called multiple times
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok()));
|
||||
}
|
||||
@ -79,14 +79,14 @@ Default `Logger` can be created with `default` method, it uses the default forma
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
extern crate env_logger;
|
||||
use actix_web::Application;
|
||||
use actix_web::App;
|
||||
use actix_web::middleware::Logger;
|
||||
|
||||
fn main() {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
Application::new()
|
||||
App::new()
|
||||
.middleware(Logger::default())
|
||||
.middleware(Logger::new("%a %{User-Agent}i"))
|
||||
.finish();
|
||||
@ -135,10 +135,10 @@ the specified header.
|
||||
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use actix_web::{http, middleware, Application, HttpResponse};
|
||||
use actix_web::{http, middleware, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
let app = Application::new()
|
||||
let app = App::new()
|
||||
.middleware(
|
||||
middleware::DefaultHeaders::build()
|
||||
.header("X-Version", "0.2")
|
||||
@ -198,7 +198,7 @@ fn index(mut req: HttpRequest) -> Result<&'static str> {
|
||||
fn main() {
|
||||
# let sys = actix::System::new("basic-example");
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.middleware(SessionStorage::new( // <- create session middleware
|
||||
CookieSessionBackend::build(&[0; 32]) // <- create cookie session backend
|
||||
.secure(false)
|
||||
|
@ -8,7 +8,7 @@ match path tail we can use `[.*]` regex.
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use std::path::PathBuf;
|
||||
use actix_web::{Application, HttpRequest, Result, http::Method, fs::NamedFile};
|
||||
use actix_web::{App, HttpRequest, Result, http::Method, fs::NamedFile};
|
||||
|
||||
fn index(req: HttpRequest) -> Result<NamedFile> {
|
||||
let path: PathBuf = req.match_info().query("tail")?;
|
||||
@ -16,7 +16,7 @@ fn index(req: HttpRequest) -> Result<NamedFile> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -25,7 +25,7 @@ fn main() {
|
||||
## Directory
|
||||
|
||||
To serve files from specific directory and sub-directories `StaticFiles` could be used.
|
||||
`StaticFiles` must be registered with `Application::handler()` method otherwise
|
||||
`StaticFiles` must be registered with `App::handler()` method otherwise
|
||||
it won't be able to serve sub-paths.
|
||||
|
||||
```rust
|
||||
@ -33,7 +33,7 @@ it won't be able to serve sub-paths.
|
||||
use actix_web::*;
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.handler("/static", fs::StaticFiles::new(".", true))
|
||||
.finish();
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ fn main() {
|
||||
builder.set_certificate_chain_file("cert.pem").unwrap();
|
||||
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/index.html", |r| r.f(index)))
|
||||
.bind("127.0.0.1:8080").unwrap();
|
||||
.serve_ssl(builder).unwrap();
|
||||
|
@ -87,7 +87,7 @@ fn main() {
|
||||
|
||||
// Start http server
|
||||
HttpServer::new(move || {
|
||||
Application::with_state(State{db: addr.clone()})
|
||||
App::with_state(State{db: addr.clone()})
|
||||
.resource("/{name}", |r| r.method(Method::GET).a(index))})
|
||||
.bind("127.0.0.1:8080").unwrap()
|
||||
.start().unwrap();
|
||||
|
@ -48,7 +48,7 @@ request handler with the application's `resource` on a particular *HTTP method*
|
||||
# "Hello world!"
|
||||
# }
|
||||
# fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/", |r| r.f(index));
|
||||
# }
|
||||
```
|
||||
@ -58,7 +58,7 @@ connections. The server accepts a function that should return an `HttpHandler` i
|
||||
|
||||
```rust,ignore
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/", |r| r.f(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run();
|
||||
@ -72,7 +72,7 @@ Here is full source of main.rs file:
|
||||
```rust
|
||||
# use std::thread;
|
||||
extern crate actix_web;
|
||||
use actix_web::{Application, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_web::{App, HttpRequest, HttpResponse, HttpServer};
|
||||
|
||||
fn index(req: HttpRequest) -> &'static str {
|
||||
"Hello world!"
|
||||
@ -84,7 +84,7 @@ fn main() {
|
||||
# // call.
|
||||
# thread::spawn(|| {
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/", |r| r.f(index)))
|
||||
.bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088")
|
||||
.run();
|
||||
|
@ -4,7 +4,7 @@ Actix web provides some primitives to build web servers and applications with Ru
|
||||
It provides routing, middlewares, pre-processing of requests, and post-processing of responses,
|
||||
websocket protocol handling, multipart streams, etc.
|
||||
|
||||
All actix web servers are built around the `Application` instance.
|
||||
All actix web servers are built around the `App` instance.
|
||||
It is used for registering routes for resources, and middlewares.
|
||||
It also stores application specific state that is shared across all handlers
|
||||
within same application.
|
||||
@ -24,7 +24,7 @@ but path `/application` would not match.
|
||||
# "Hello world!"
|
||||
# }
|
||||
# fn main() {
|
||||
let app = Application::new()
|
||||
let app = App::new()
|
||||
.prefix("/app")
|
||||
.resource("/index.html", |r| r.method(Method::GET).f(index))
|
||||
.finish()
|
||||
@ -43,17 +43,17 @@ Multiple applications can be served with one server:
|
||||
# extern crate tokio_core;
|
||||
# use tokio_core::net::TcpStream;
|
||||
# use std::net::SocketAddr;
|
||||
use actix_web::{Application, HttpResponse, HttpServer};
|
||||
use actix_web::{App, HttpResponse, HttpServer};
|
||||
|
||||
fn main() {
|
||||
HttpServer::new(|| vec![
|
||||
Application::new()
|
||||
App::new()
|
||||
.prefix("/app1")
|
||||
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
||||
Application::new()
|
||||
App::new()
|
||||
.prefix("/app2")
|
||||
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/", |r| r.f(|r| HttpResponse::Ok())),
|
||||
]);
|
||||
}
|
||||
@ -81,7 +81,7 @@ in the state:
|
||||
# extern crate actix_web;
|
||||
#
|
||||
use std::cell::Cell;
|
||||
use actix_web::{Application, HttpRequest, http};
|
||||
use actix_web::{App, HttpRequest, http};
|
||||
|
||||
// This struct represents state
|
||||
struct AppState {
|
||||
@ -96,7 +96,7 @@ fn index(req: HttpRequest<AppState>) -> String {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::with_state(AppState{counter: Cell::new(0)})
|
||||
App::with_state(AppState{counter: Cell::new(0)})
|
||||
.resource("/", |r| r.method(http::Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
|
@ -13,13 +13,13 @@ within a properly configured actix system:
|
||||
# extern crate actix;
|
||||
# extern crate actix_web;
|
||||
use actix::*;
|
||||
use actix_web::{server, Application, HttpResponse};
|
||||
use actix_web::{server, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
let sys = actix::System::new("guide");
|
||||
|
||||
server::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.bind("127.0.0.1:59080").unwrap()
|
||||
.start();
|
||||
@ -49,7 +49,7 @@ address of the started http server. Actix http server accepts several messages:
|
||||
use std::thread;
|
||||
use std::sync::mpsc;
|
||||
use actix::*;
|
||||
use actix_web::{server, Application, HttpResponse, HttpServer};
|
||||
use actix_web::{server, App, HttpResponse, HttpServer};
|
||||
|
||||
fn main() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
@ -57,7 +57,7 @@ fn main() {
|
||||
thread::spawn(move || {
|
||||
let sys = actix::System::new("http-server");
|
||||
let addr = server::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0")
|
||||
.shutdown_timeout(60) // <- Set shutdown timeout to 60 seconds
|
||||
@ -81,11 +81,11 @@ can be overridden with the `HttpServer::threads()` method.
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
# extern crate tokio_core;
|
||||
use actix_web::{Application, HttpServer, HttpResponse};
|
||||
use actix_web::{App, HttpServer, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.threads(4); // <- Start 4 workers
|
||||
}
|
||||
@ -116,7 +116,7 @@ fn main() {
|
||||
builder.set_certificate_chain_file("cert.pem").unwrap();
|
||||
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/index.html", |r| r.f(index)))
|
||||
.bind("127.0.0.1:8080").unwrap()
|
||||
.serve_ssl(builder).unwrap();
|
||||
@ -143,21 +143,21 @@ connection behavior is defined by server settings.
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
# extern crate tokio_core;
|
||||
use actix_web::{server, Application, HttpResponse};
|
||||
use actix_web::{server, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
server::new(||
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.keep_alive(75); // <- Set keep-alive to 75 seconds
|
||||
|
||||
server::new(||
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.keep_alive(server::KeepAlive::Tcp(75)); // <- Use `SO_KEEPALIVE` socket option.
|
||||
|
||||
server::new(||
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok())))
|
||||
.keep_alive(None); // <- Disable keep-alive
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ Here is an example of a handler that stores the number of processed requests:
|
||||
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use actix_web::{Application, HttpRequest, HttpResponse, dev::Handler};
|
||||
use actix_web::{App, HttpRequest, HttpResponse, dev::Handler};
|
||||
|
||||
struct MyHandler(usize);
|
||||
|
||||
@ -75,7 +75,7 @@ number of requests processed per thread. A proper implementation would use `Arc`
|
||||
```rust
|
||||
# extern crate actix;
|
||||
# extern crate actix_web;
|
||||
use actix_web::{server, Application, HttpRequest, HttpResponse, dev::Handler};
|
||||
use actix_web::{server, App, HttpRequest, HttpResponse, dev::Handler};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
@ -99,7 +99,7 @@ fn main() {
|
||||
server::new(
|
||||
move || {
|
||||
let cloned = inc.clone();
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/", move |r| r.h(MyHandler(cloned)))
|
||||
})
|
||||
.bind("127.0.0.1:8088").unwrap()
|
||||
@ -127,7 +127,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::{Application, HttpServer, HttpRequest, HttpResponse, Error, Responder, http};
|
||||
use actix_web::{App, HttpServer, HttpRequest, HttpResponse, Error, Responder, http};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct MyObj {
|
||||
@ -158,7 +158,7 @@ fn main() {
|
||||
let sys = actix::System::new("example");
|
||||
|
||||
HttpServer::new(
|
||||
|| Application::new()
|
||||
|| App::new()
|
||||
.resource("/", |r| r.method(http::Method::GET).f(index)))
|
||||
.bind("127.0.0.1:8088").unwrap()
|
||||
.start();
|
||||
@ -198,7 +198,7 @@ fn index2(req: HttpRequest) -> Box<Future<Item=&'static str, Error=Error>> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/async", |r| r.route().a(index))
|
||||
.resource("/", |r| r.route().a(index2))
|
||||
.finish();
|
||||
@ -224,7 +224,7 @@ fn index(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/async", |r| r.f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -257,7 +257,7 @@ fn index(req: HttpRequest) -> Result<Box<Future<Item=HttpResponse, Error=Error>>
|
||||
#
|
||||
# fn is_error() -> bool { true }
|
||||
# fn main() {
|
||||
# Application::new()
|
||||
# App::new()
|
||||
# .resource("/async", |r| r.route().f(index))
|
||||
# .finish();
|
||||
# }
|
||||
@ -294,7 +294,7 @@ fn index(req: HttpRequest) -> RegisterResult {
|
||||
}
|
||||
# fn is_a_variant() -> bool { true }
|
||||
# fn main() {
|
||||
# Application::new()
|
||||
# App::new()
|
||||
# .resource("/register", |r| r.f(index))
|
||||
# .finish();
|
||||
# }
|
||||
|
@ -27,7 +27,7 @@ fn index(req: HttpRequest) -> io::Result<fs::NamedFile> {
|
||||
}
|
||||
#
|
||||
# fn main() {
|
||||
# Application::new()
|
||||
# App::new()
|
||||
# .resource(r"/a/index.html", |r| r.f(index))
|
||||
# .finish();
|
||||
# }
|
||||
@ -58,7 +58,7 @@ fn index(req: HttpRequest) -> Result<&'static str, MyError> {
|
||||
}
|
||||
#
|
||||
# fn main() {
|
||||
# Application::new()
|
||||
# App::new()
|
||||
# .resource(r"/a/index.html", |r| r.f(index))
|
||||
# .finish();
|
||||
# }
|
||||
@ -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::{Application, HttpRequest, HttpResponse, http, error};
|
||||
use actix_web::{App, HttpRequest, HttpResponse, http, error};
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
enum MyError {
|
||||
@ -100,7 +100,7 @@ fn index(req: HttpRequest) -> Result<&'static str, MyError> {
|
||||
}
|
||||
#
|
||||
# fn main() {
|
||||
# Application::new()
|
||||
# App::new()
|
||||
# .resource(r"/a/index.html", |r| r.f(index))
|
||||
# .finish();
|
||||
# }
|
||||
@ -127,7 +127,7 @@ fn index(req: HttpRequest) -> Result<&'static str> {
|
||||
Ok(result.map_err(|e| error::ErrorBadRequest(e))?)
|
||||
}
|
||||
# fn main() {
|
||||
# Application::new()
|
||||
# App::new()
|
||||
# .resource(r"/a/index.html", |r| r.f(index))
|
||||
# .finish();
|
||||
# }
|
||||
|
@ -15,20 +15,20 @@ A resource also has a pattern, meant to match against the *PATH* portion of a *U
|
||||
it does not match against the *QUERY* portion (the portion following the scheme and
|
||||
port, e.g., */foo/bar* in the *URL* *http://localhost:8080/foo/bar?q=value*).
|
||||
|
||||
The [Application::resource](../actix_web/struct.Application.html#method.resource) methods
|
||||
The [App::resource](../actix_web/struct.App.html#method.resource) methods
|
||||
add a single resource to application routing table. This method accepts a *path pattern*
|
||||
and a resource configuration function.
|
||||
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
# use actix_web::{Application, HttpRequest, HttpResponse, http::Method};
|
||||
# use actix_web::{App, HttpRequest, HttpResponse, http::Method};
|
||||
#
|
||||
# fn index(req: HttpRequest) -> HttpResponse {
|
||||
# unimplemented!()
|
||||
# }
|
||||
#
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/prefix", |r| r.f(index))
|
||||
.resource("/user/{name}",
|
||||
|r| r.method(Method::GET).f(|req| HttpResponse::Ok()))
|
||||
@ -63,7 +63,7 @@ any number of *predicates* but only one handler.
|
||||
# use actix_web::*;
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/path", |resource|
|
||||
resource.route()
|
||||
.filter(pred::Get())
|
||||
@ -108,7 +108,7 @@ against a URL path pattern. `path` represents the path portion of the URL that w
|
||||
The way that *actix* does this is very simple. When a request enters the system,
|
||||
for each resource configuration declaration present in the system, actix checks
|
||||
the request's path against the pattern declared. This checking happens in the order that
|
||||
the routes were declared via `Application::resource()` method. If resource can not be found,
|
||||
the routes were declared via `App::resource()` method. If resource can not be found,
|
||||
the *default resource* is used as the matched resource.
|
||||
|
||||
When a route configuration is declared, it may contain route predicate arguments. All route
|
||||
@ -277,7 +277,7 @@ fn index(req: HttpRequest) -> Result<String> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource(r"/a/{v1}/{v2}/", |r| r.f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -304,7 +304,7 @@ safe to interpolate within, or use as a suffix of, a path without additional che
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use std::path::PathBuf;
|
||||
use actix_web::{Application, HttpRequest, Result, http::Method};
|
||||
use actix_web::{App, HttpRequest, Result, http::Method};
|
||||
|
||||
fn index(req: HttpRequest) -> Result<String> {
|
||||
let path: PathBuf = req.match_info().query("tail")?;
|
||||
@ -312,7 +312,7 @@ fn index(req: HttpRequest) -> Result<String> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -333,7 +333,7 @@ has to implement *serde's *`Deserialize` trait.
|
||||
# extern crate actix_web;
|
||||
# extern crate futures;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
use actix_web::{Application, Path, Result, http::Method};
|
||||
use actix_web::{App, Path, Result, http::Method};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Info {
|
||||
@ -346,7 +346,7 @@ fn index(info: Path<Info>) -> Result<String> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = Application::new()
|
||||
let app = App::new()
|
||||
.resource("/{username}/index.html", // <- define path parameters
|
||||
|r| r.method(Method::GET).with(index));
|
||||
}
|
||||
@ -364,7 +364,7 @@ resource with the name "foo" and the pattern "{a}/{b}/{c}", you might do this:
|
||||
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
# use actix_web::{Application, HttpRequest, HttpResponse, http::Method};
|
||||
# use actix_web::{App, HttpRequest, HttpResponse, http::Method};
|
||||
#
|
||||
fn index(req: HttpRequest) -> HttpResponse {
|
||||
let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource
|
||||
@ -372,7 +372,7 @@ fn index(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = Application::new()
|
||||
let app = App::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(|_| HttpResponse::Ok());
|
||||
@ -394,7 +394,7 @@ for URL generation purposes only and are never considered for matching at reques
|
||||
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use actix_web::{Application, HttpRequest, HttpResponse, Error};
|
||||
use actix_web::{App, HttpRequest, HttpResponse, Error};
|
||||
|
||||
fn index(mut req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||
let url = req.url_for("youtube", &["oHg5SJYRHA0"])?;
|
||||
@ -403,7 +403,7 @@ fn index(mut req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = Application::new()
|
||||
let app = App::new()
|
||||
.resource("/index.html", |r| r.f(index))
|
||||
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
||||
.finish();
|
||||
@ -440,7 +440,7 @@ use actix_web::http::NormalizePath;
|
||||
# HttpResponse::Ok().into()
|
||||
# }
|
||||
fn main() {
|
||||
let app = Application::new()
|
||||
let app = App::new()
|
||||
.resource("/resource/", |r| r.f(index))
|
||||
.default_resource(|r| r.h(NormalizePath::default()))
|
||||
.finish();
|
||||
@ -459,13 +459,13 @@ 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::{Application, HttpRequest, http::Method, http::NormalizePath};
|
||||
use actix_web::{App, HttpRequest, http::Method, http::NormalizePath};
|
||||
#
|
||||
# fn index(req: HttpRequest) -> &'static str {
|
||||
# "test"
|
||||
# }
|
||||
fn main() {
|
||||
let app = Application::new()
|
||||
let app = App::new()
|
||||
.resource("/resource/", |r| r.f(index))
|
||||
.default_resource(|r| r.method(Method::GET).h(NormalizePath::default()))
|
||||
.finish();
|
||||
@ -474,7 +474,7 @@ fn main() {
|
||||
|
||||
## Using an Application Prefix to Compose Applications
|
||||
|
||||
The `Application::prefix()`" method allows to set a specific application prefix.
|
||||
The `App::prefix()`" method allows to set a specific application prefix.
|
||||
This prefix represents a resource prefix that will be prepended to all resource patterns added
|
||||
by the resource configuration. This can be used to help mount a set of routes at a different
|
||||
location than the included callable's author intended while still maintaining the same
|
||||
@ -491,7 +491,7 @@ fn show_users(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.prefix("/users")
|
||||
.resource("/show", |r| r.f(show_users))
|
||||
.finish();
|
||||
@ -517,7 +517,7 @@ Here is a simple predicate that check that a request contains a specific *header
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
# use actix_web::*;
|
||||
use actix_web::{http, pred::Predicate, Application, HttpRequest};
|
||||
use actix_web::{http, pred::Predicate, App, HttpRequest};
|
||||
|
||||
struct ContentTypeHeader;
|
||||
|
||||
@ -529,7 +529,7 @@ impl<S: 'static> Predicate<S> for ContentTypeHeader {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/index.html", |r|
|
||||
r.route()
|
||||
.filter(ContentTypeHeader)
|
||||
@ -553,10 +553,10 @@ except "GET":
|
||||
# extern crate actix_web;
|
||||
# extern crate http;
|
||||
# use actix_web::*;
|
||||
use actix_web::{pred, Application, HttpResponse};
|
||||
use actix_web::{pred, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/index.html", |r|
|
||||
r.route()
|
||||
.filter(pred::Not(pred::Get()))
|
||||
@ -583,16 +583,16 @@ predicates match. i.e:
|
||||
|
||||
If the path pattern can not be found in the routing table or a resource can not find matching
|
||||
route, the default resource is used. The default response is *NOT FOUND*.
|
||||
It is possible to override the *NOT FOUND* response with `Application::default_resource()`.
|
||||
It is possible to override the *NOT FOUND* response with `App::default_resource()`.
|
||||
This method accepts a *configuration function* same as normal resource configuration
|
||||
with `Application::resource()` method.
|
||||
with `App::resource()` method.
|
||||
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use actix_web::{Application, HttpResponse, http::Method, pred};
|
||||
use actix_web::{App, HttpResponse, http::Method, pred};
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.default_resource(|r| {
|
||||
r.method(Method::GET).f(|req| HttpResponse::NotFound());
|
||||
r.route().filter(pred::Not(pred::Get()))
|
||||
|
@ -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::{Application, HttpRequest, Json, Result, http::Method};
|
||||
use actix_web::{App, HttpRequest, Json, Result, http::Method};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct MyObj {
|
||||
@ -147,7 +147,7 @@ fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource(r"/a/{name}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
|
@ -75,15 +75,15 @@ function same way as you would for real http server configuration.
|
||||
|
||||
```rust
|
||||
# extern crate actix_web;
|
||||
use actix_web::{http, test, Application, HttpRequest, HttpResponse};
|
||||
use actix_web::{http, test, App, HttpRequest, HttpResponse};
|
||||
|
||||
fn index(req: HttpRequest) -> HttpResponse {
|
||||
HttpResponse::Ok().into()
|
||||
}
|
||||
|
||||
/// This function get called by http server.
|
||||
fn create_app() -> Application {
|
||||
Application::new()
|
||||
fn create_app() -> App {
|
||||
App::new()
|
||||
.resource("/test", |r| r.h(index))
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::new()
|
||||
App::new()
|
||||
.resource("/ws/", |r| r.f(|req| ws::start(req, Ws))) // <- register websocket route
|
||||
.finish();
|
||||
}
|
||||
|
Reference in New Issue
Block a user