Struct actix_web::Application
[−]
[src]
pub struct Application<S = ()> { /* fields omitted */ }
Structure that follows the builder pattern for building Application
structs.
Methods
impl Application<()>
[src]
fn new() -> Application<()>
[src]
Create application with empty state. Application can be configured with builder-like pattern.
impl<S> Application<S> where
S: 'static,
[src]
S: 'static,
fn with_state(state: S) -> Application<S>
[src]
Create application with specific state. Application can be configured with builder-like pattern.
State is shared with all resources within same application and could be
accessed with HttpRequest::state()
method.
fn prefix<P: Into<String>>(self, prefix: P) -> Application<S>
[src]
Set application prefix
Only requests that matches application's prefix get processed by this application.
Application prefix always contains leading "/" slash. If supplied prefix
does not contain leading slash, it get inserted. Prefix should
consists valid path segments. i.e for application with
prefix /app
any request with following paths /app
, /app/
or /app/test
would match, but path /application
would not match.
In the following example only requests with "/app/" path prefix get handled. Request with path "/app/test/" would be handled, but request with path "/application" or "/other/..." would return NOT FOUND
use actix_web::*; fn main() { let app = Application::new() .prefix("/app") .resource("/test", |r| { r.method(Method::GET).f(|_| httpcodes::HttpOk); r.method(Method::HEAD).f(|_| httpcodes::HttpMethodNotAllowed); }) .finish(); }
fn resource<F>(self, path: &str, f: F) -> Application<S> where
F: FnOnce(&mut Resource<S>) + 'static,
[src]
F: FnOnce(&mut Resource<S>) + 'static,
Configure resource for specific path.
Resource may have variable path also. For instance, a resource with the path /a/{name}/c would match all incoming requests with paths such as /a/b/c, /a/1/c, and /a/etc/c.
A variable part is specified in the form {identifier}
, where
the identifier can be used later in a request handler to access the matched
value for that part. This is done by looking up the identifier
in the Params
object returned by HttpRequest.match_info()
method.
By default, each part matches the regular expression [^{}/]+
.
You can also specify a custom regex in the form {identifier:regex}
:
For instance, to route Get requests on any route matching /users/{userid}/{friend}
and
store userid and friend in the exposed Params object:
use actix_web::*; fn main() { let app = Application::new() .resource("/test", |r| { r.method(Method::GET).f(|_| httpcodes::HttpOk); r.method(Method::HEAD).f(|_| httpcodes::HttpMethodNotAllowed); }); }
fn default_resource<F>(self, f: F) -> Application<S> where
F: FnOnce(&mut Resource<S>) + 'static,
[src]
F: FnOnce(&mut Resource<S>) + 'static,
Default resource is used if no matched route could be found.
fn default_encoding<F>(self, encoding: ContentEncoding) -> Application<S>
[src]
Set default content encoding. ContentEncoding::Auto
is set by default.
fn external_resource<T, U>(self, name: T, url: U) -> Application<S> where
T: AsRef<str>,
U: AsRef<str>,
[src]
T: AsRef<str>,
U: AsRef<str>,
Register external resource.
External resources are useful for URL generation purposes only and
are never considered for matching at request time.
Call to HttpRequest::url_for()
will work as expected.
use actix_web::*; fn index(mut req: HttpRequest) -> Result<HttpResponse> { let url = req.url_for("youtube", &["oHg5SJYRHA0"])?; assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0"); Ok(httpcodes::HttpOk.into()) } fn main() { let app = Application::new() .resource("/index.html", |r| r.f(index)) .external_resource("youtube", "https://youtube.com/watch/{video_id}") .finish(); }
fn handler<H: Handler<S>>(self, path: &str, handler: H) -> Application<S>
[src]
Configure handler for specific path prefix.
Path prefix consists valid path segments. i.e for prefix /app
any request with following paths /app
, /app/
or /app/test
would match, but path /application
would not match.
use actix_web::*; fn main() { let app = Application::new() .handler("/app", |req: HttpRequest| { match *req.method() { Method::GET => httpcodes::HttpOk, Method::POST => httpcodes::HttpMethodNotAllowed, _ => httpcodes::HttpNotFound, }}); }
fn middleware<M: Middleware<S>>(self, mw: M) -> Application<S>
[src]
Register a middleware
fn finish(&mut self) -> HttpApplication<S>
[src]
Finish application configuration and create HttpHandler object
fn boxed(self) -> Box<HttpHandler>
[src]
Convenience method for creating Box<HttpHandler>
instance.
This method is useful if you need to register several application instances with different state.
use actix_web::*; struct State1; struct State2; fn main() { HttpServer::new(|| { vec![ Application::with_state(State1) .prefix("/app1") .resource("/", |r| r.h(httpcodes::HttpOk)) .boxed(), Application::with_state(State2) .prefix("/app2") .resource("/", |r| r.h(httpcodes::HttpOk)) .boxed() ]}) .bind("127.0.0.1:8080").unwrap() .run() }
Trait Implementations
impl Default for Application<()>
[src]
impl<S: 'static> IntoHttpHandler for Application<S>
[src]
type Handler = HttpApplication<S>
The associated type which is result of conversion.
fn into_handler(self, settings: ServerSettings) -> HttpApplication<S>
[src]
Convert into HttpHandler
object.
impl<'a, S: 'static> IntoHttpHandler for &'a mut Application<S>
[src]
type Handler = HttpApplication<S>
The associated type which is result of conversion.
fn into_handler(self, settings: ServerSettings) -> HttpApplication<S>
[src]
Convert into HttpHandler
object.