mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 01:51:23 +02:00
simplify application creation
This commit is contained in:
@ -15,12 +15,12 @@ Default `Logger` could be created with `default` method, it uses the default for
|
||||
%a %t "%r" %s %b "%{Referrer}i" "%{User-Agent}i" %T
|
||||
```
|
||||
```rust
|
||||
extern crate actix_web;
|
||||
# extern crate actix_web;
|
||||
use actix_web::Application;
|
||||
use actix_web::middlewares::Logger;
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.middleware(Logger::default())
|
||||
.middleware(Logger::new("%a %{User-Agent}i"))
|
||||
.finish();
|
||||
@ -67,11 +67,11 @@ Tto set default response headers `DefaultHeaders` middleware could be used.
|
||||
*DefaultHeaders* middleware does not set header if response headers already contains it.
|
||||
|
||||
```rust
|
||||
extern crate actix_web;
|
||||
# extern crate actix_web;
|
||||
use actix_web::*;
|
||||
|
||||
fn main() {
|
||||
let app = Application::default("/")
|
||||
let app = Application::new("/")
|
||||
.middleware(
|
||||
middlewares::DefaultHeaders::build()
|
||||
.header("X-Version", "0.2")
|
||||
|
@ -16,7 +16,7 @@ fn index(req: HttpRequest) -> Result<fs::NamedFile> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -32,7 +32,7 @@ To serve files from specific directory and sub-directories `StaticFiles` could b
|
||||
use actix_web::*;
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/static", |r| r.h(fs::StaticFiles::new(".", true)))
|
||||
.finish();
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ fn main() {
|
||||
let pkcs12 = Pkcs12::from_der(&pkcs12).unwrap().parse("12345").unwrap();
|
||||
|
||||
HttpServer::new(
|
||||
Application::default("/")
|
||||
.route("/index.html", |r| r.f(index))
|
||||
Application::new("/")
|
||||
.resource("/index.html", |r| r.f(index))
|
||||
.serve_tls::<_, ()>("127.0.0.1:8080", pkcs12).unwrap();
|
||||
}
|
||||
```
|
||||
|
@ -48,7 +48,7 @@ request handler with the application's `resource` on a particular *HTTP method*
|
||||
# "Hello world!"
|
||||
# }
|
||||
# fn main() {
|
||||
let app = Application::default("/")
|
||||
let app = Application::new("/")
|
||||
.resource("/", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
# }
|
||||
@ -79,7 +79,7 @@ fn main() {
|
||||
let sys = actix::System::new("example");
|
||||
|
||||
HttpServer::new(
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/", |r| r.f(index)))
|
||||
.serve::<_, ()>("127.0.0.1:8088").unwrap();
|
||||
|
||||
|
@ -20,7 +20,7 @@ has same url path prefix:
|
||||
# "Hello world!"
|
||||
# }
|
||||
# fn main() {
|
||||
let app = Application::default("/prefix")
|
||||
let app = Application::new("/prefix")
|
||||
.resource("/index.html", |r| r.method(Method::GET).f(index))
|
||||
.finish()
|
||||
# }
|
||||
@ -40,15 +40,12 @@ use tokio_core::net::TcpStream;
|
||||
|
||||
fn main() {
|
||||
HttpServer::<TcpStream, SocketAddr, _>::new(vec![
|
||||
Application::default("/app1")
|
||||
.resource("/", |r| r.f(|r| httpcodes::HTTPOk))
|
||||
.finish(),
|
||||
Application::default("/app2")
|
||||
.resource("/", |r| r.f(|r| httpcodes::HTTPOk))
|
||||
.finish(),
|
||||
Application::default("/")
|
||||
.resource("/", |r| r.f(|r| httpcodes::HTTPOk))
|
||||
.finish(),
|
||||
Application::new("/app1")
|
||||
.resource("/", |r| r.f(|r| httpcodes::HTTPOk)),
|
||||
Application::new("/app2")
|
||||
.resource("/", |r| r.f(|r| httpcodes::HTTPOk)),
|
||||
Application::new("/")
|
||||
.resource("/", |r| r.f(|r| httpcodes::HTTPOk)),
|
||||
]);
|
||||
}
|
||||
```
|
||||
|
@ -46,8 +46,8 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||
Let's create response for custom type that serializes to `application/json` response:
|
||||
|
||||
```rust
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
# extern crate actix;
|
||||
# extern crate actix_web;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
@ -77,7 +77,7 @@ fn main() {
|
||||
let sys = actix::System::new("example");
|
||||
|
||||
HttpServer::new(
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/", |r| r.method(
|
||||
Method::GET).f(|req| {MyObj{name: "user".to_owned()}})))
|
||||
.serve::<_, ()>("127.0.0.1:8088").unwrap();
|
||||
@ -113,7 +113,7 @@ fn index(req: HttpRequest) -> FutureResult<HttpResponse, Error> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/async", |r| r.route().a(index))
|
||||
.finish();
|
||||
}
|
||||
@ -138,7 +138,7 @@ fn index(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/async", |r| r.f(index))
|
||||
.finish();
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ fn index(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/prefix", |r| r.f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -36,7 +36,7 @@ fn index(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/app")
|
||||
Application::new("/app")
|
||||
.resource("/prefix", |r| r.f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -53,7 +53,7 @@ if no route could be matched default response `HTTPMethodNotAllowed` get resturn
|
||||
# use actix_web::*;
|
||||
#
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/prefix", |r| {
|
||||
r.method(Method::GET).h(httpcodes::HTTPOk);
|
||||
r.method(Method::POST).h(httpcodes::HTTPForbidden);
|
||||
@ -86,7 +86,7 @@ fn index(req: HttpRequest) -> String {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource("/{name}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -104,7 +104,7 @@ You can also specify a custom regex in the form `{identifier:regex}`:
|
||||
# }
|
||||
#
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource(r"{name:\d+}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -125,7 +125,7 @@ fn index(req: HttpRequest) -> Result<String> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource(r"/a/{v1}/{v2}/", |r| r.f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -143,7 +143,7 @@ It is possible to match path tail with custom `.*` regex.
|
||||
# unimplemented!()
|
||||
# }
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource(r"/test/{tail:.*}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
@ -179,7 +179,7 @@ fn index(req: HttpRequest) -> Result<String> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ fn index(req: HttpRequest<AppState>) -> String {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::build("/", AppState{counter: Cell::new(0)})
|
||||
Application::with_state("/", AppState{counter: Cell::new(0)})
|
||||
.resource("/", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Application::default("/")
|
||||
Application::new("/")
|
||||
.resource(r"/a/{name}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
}
|
||||
|
Reference in New Issue
Block a user