[−][src]Struct actix_web::Scope
Resources scope.
Scope is a set of resources with common root path.
Scopes collect multiple paths under a common path prefix.
Scope path can contain variable path segments as resources.
Scope prefix is always complete path segment, i.e /app
would
be converted to a /app/
and it would not match /app
path.
You can get variable path segments from HttpRequest::match_info()
.
Path
extractor also is able to extract scope level variable segments.
use actix_web::{web, App, HttpResponse}; fn main() { let app = App::new().service( web::scope("/{project_id}/") .service(web::resource("/path1").to(|| HttpResponse::Ok())) .service(web::resource("/path2").route(web::get().to(|| HttpResponse::Ok()))) .service(web::resource("/path3").route(web::head().to(|| HttpResponse::MethodNotAllowed()))) ); }
In the above example three routes get registered:
- /{project_id}/path1 - reponds to all http method
- /{project_id}/path2 -
GET
requests - /{project_id}/path3 -
HEAD
requests
Methods
impl<P: 'static> Scope<P>
[src]
impl<P, T> Scope<P, T> where
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
[src]
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
pub fn guard<G: Guard + 'static>(self, guard: G) -> Self
[src]
Add match guard to a scope.
use actix_web::{web, guard, App, HttpRequest, HttpResponse}; fn index(data: web::Path<(String, String)>) -> &'static str { "Welcome!" } fn main() { let app = App::new().service( web::scope("/app") .guard(guard::Header("content-type", "text/plain")) .route("/test1", web::get().to(index)) .route("/test2", web::post().to(|r: HttpRequest| { HttpResponse::MethodNotAllowed() })) ); }
pub fn service<F>(self, factory: F) -> Self where
F: HttpServiceFactory<P> + 'static,
[src]
F: HttpServiceFactory<P> + 'static,
Register http service.
This is similar to App's
service registration.
Actix web provides several services implementations:
- Resource is an entry in resource table which corresponds to requested URL.
- Scope is a set of resources with common root path.
- "StaticFiles" is a service for static files support
use actix_web::{web, App, HttpRequest}; struct AppState; fn index(req: HttpRequest) -> &'static str { "Welcome!" } fn main() { let app = App::new().service( web::scope("/app").service( web::scope("/v1") .service(web::resource("/test1").to(index))) ); }
pub fn route(self, path: &str, route: Route<P>) -> Self
[src]
Configure route for a specific path.
This is a simplified version of the Scope::service()
method.
This method can be called multiple times, in that case
multiple resources with one route would be registered for same resource path.
use actix_web::{web, App, HttpResponse}; fn index(data: web::Path<(String, String)>) -> &'static str { "Welcome!" } fn main() { let app = App::new().service( web::scope("/app") .route("/test1", web::get().to(index)) .route("/test2", web::post().to(|| HttpResponse::MethodNotAllowed())) ); }
pub fn default_resource<F, U>(self, f: F) -> Self where
F: FnOnce(Resource<P>) -> Resource<P, U>,
U: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
[src]
F: FnOnce(Resource<P>) -> Resource<P, U>,
U: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
Default resource to be used if no matching route could be found.
If default resource is not registered, app's default resource is being used.
pub fn wrap<M, F>(
self,
mw: F
) -> Scope<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
M: Transform<T::Service, Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
F: IntoTransform<M, T::Service>,
[src]
self,
mw: F
) -> Scope<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
M: Transform<T::Service, Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>,
F: IntoTransform<M, T::Service>,
Registers middleware, in the form of a middleware component (type), that runs during inbound processing in the request lifecycle (request -> response), modifying request as necessary, across all requests managed by the Scope. Scope-level middleware is more limited in what it can modify, relative to Route or Application level middleware, in that Scope-level middleware can not modify ServiceResponse.
Use middleware when you need to read or modify every request in some way.
pub fn wrap_fn<F, R>(
self,
mw: F
) -> Scope<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest<P>, &mut T::Service) -> R + Clone,
R: IntoFuture<Item = ServiceResponse, Error = Error>,
[src]
self,
mw: F
) -> Scope<P, impl NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest<P>, &mut T::Service) -> R + Clone,
R: IntoFuture<Item = ServiceResponse, Error = Error>,
Registers middleware, in the form of a closure, that runs during inbound
processing in the request lifecycle (request -> response), modifying
request as necessary, across all requests managed by the Scope.
Scope-level middleware is more limited in what it can modify, relative
to Route or Application level middleware, in that Scope-level middleware
can not modify ServiceResponse.
use actix_service::Service; use actix_web::{web, App}; use actix_web::http::{header::CONTENT_TYPE, HeaderValue}; fn index() -> &'static str { "Welcome!" } fn main() { let app = App::new().service( web::scope("/app") .wrap_fn(|req, srv| srv.call(req).map(|mut res| { res.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static("text/plain"), ); res })) .route("/index.html", web::get().to(index))); }
Trait Implementations
impl<P, T> HttpServiceFactory<P> for Scope<P, T> where
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
[src]
P: 'static,
T: NewService<Request = ServiceRequest<P>, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
fn register(self, config: &mut ServiceConfig<P>)
[src]
Auto Trait Implementations
impl<P, T = ScopeEndpoint<P>> !Send for Scope<P, T>
impl<P, T = ScopeEndpoint<P>> !Sync for Scope<P, T>
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,