Struct ntex::web::Scope [−][src]
pub struct Scope<Err: ErrorRenderer, M = Identity, T = Filter<Err>> { /* fields omitted */ }
Expand description
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 ntex::web::{self, App, HttpResponse};
fn main() {
let app = App::new().service(
web::scope("/{project_id}/")
.service(web::resource("/path1").to(|| async { HttpResponse::Ok() }))
.service(web::resource("/path2").route(web::get().to(|| async { HttpResponse::Ok() })))
.service(web::resource("/path3").route(web::head().to(|| async { 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
Implementations
Create a new scope
impl<Err, M, T> Scope<Err, M, T> where
T: ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
Err: ErrorRenderer,
impl<Err, M, T> Scope<Err, M, T> where
T: ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
Err: ErrorRenderer,
Add match guard to a scope.
use ntex::web::{self, guard, App, HttpRequest, HttpResponse};
async fn index(data: web::types::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| async {
HttpResponse::MethodNotAllowed()
}))
);
}
Set or override application data. Application data could be accessed
by using Data<T>
extractor where T
is data type.
use std::cell::Cell;
use ntex::web::{self, App, HttpResponse};
struct MyData {
counter: Cell<usize>,
}
async fn index(data: web::types::Data<MyData>) -> HttpResponse {
data.counter.set(data.counter.get() + 1);
HttpResponse::Ok().into()
}
fn main() {
let app = App::new().service(
web::scope("/app")
.data(MyData{ counter: Cell::new(0) })
.service(
web::resource("/index.html").route(
web::get().to(index)))
);
}
Set or override application data.
This method overrides data stored with App::app_data()
Use ascii case-insensitive routing.
Only static segments could be case-insensitive.
Run external configuration as part of the scope building process
This function is useful for moving parts of configuration to a different module or even library. For example, some of the resource’s configuration could be moved to different module.
use ntex::web::{self, middleware, App, HttpResponse};
// this function could be located in different module
fn config(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("/test")
.route(web::get().to(|| async { HttpResponse::Ok() }))
.route(web::head().to(|| async { HttpResponse::MethodNotAllowed() }))
);
}
fn main() {
let app = App::new()
.wrap(middleware::Logger::default())
.service(
web::scope("/api")
.configure(config)
)
.route("/index.html", web::get().to(|| async { HttpResponse::Ok() }));
}
Register http service.
This is similar to App's
service registration.
ntex 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 ntex::web::{self, App, HttpRequest};
struct AppState;
async 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)))
);
}
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 ntex::web::{self, App, HttpResponse};
async fn index(data: web::types::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(|| async { HttpResponse::MethodNotAllowed() }))
);
}
pub fn default_service<F, S>(self, f: F) -> Self where
F: IntoServiceFactory<S, WebRequest<Err>>,
S: ServiceFactory<WebRequest<Err>, Response = WebResponse, Error = Err::Container> + 'static,
S::InitError: Debug,
pub fn default_service<F, S>(self, f: F) -> Self where
F: IntoServiceFactory<S, WebRequest<Err>>,
S: ServiceFactory<WebRequest<Err>, Response = WebResponse, Error = Err::Container> + 'static,
S::InitError: Debug,
Default service 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 filter<U, F>(
self,
filter: F
) -> Scope<Err, M, impl ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>> where
U: ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
F: IntoServiceFactory<U, WebRequest<Err>>,
pub fn filter<U, F>(
self,
filter: F
) -> Scope<Err, M, impl ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>> where
U: ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()>,
F: IntoServiceFactory<U, WebRequest<Err>>,
Register request filter.
Filter runs during inbound processing in the request lifecycle (request -> response), modifying request as necessary, across all requests managed by the Scope.
This is similar to App's
filters, but filter get invoked on scope level.
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 WebResponse.
Use middleware when you need to read or modify every request in some way.
Trait Implementations
impl<Err, M, T> WebServiceFactory<Err> for Scope<Err, M, T> where
T: ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()> + 'static,
M: Transform<ScopeService<T::Service, Err>> + 'static,
M::Service: Service<WebRequest<Err>, Response = WebResponse, Error = Err::Container>,
Err: ErrorRenderer,
impl<Err, M, T> WebServiceFactory<Err> for Scope<Err, M, T> where
T: ServiceFactory<WebRequest<Err>, Response = WebRequest<Err>, Error = Err::Container, InitError = ()> + 'static,
M: Transform<ScopeService<T::Service, Err>> + 'static,
M::Service: Service<WebRequest<Err>, Response = WebResponse, Error = Err::Container>,
Err: ErrorRenderer,
Auto Trait Implementations
impl<Err, M = Identity, T = Filter<Err>> !RefUnwindSafe for Scope<Err, M, T>
impl<Err, M = Identity, T = Filter<Err>> !UnwindSafe for Scope<Err, M, T>
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more