[−][src]Struct scrappy::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 scrappy::{web, 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(|| 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 Scope
[src]
impl<T> Scope<T> where
T: ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>,
[src]
T: ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>,
pub fn guard<G: Guard + 'static>(self, guard: G) -> Self
[src]
Add match guard to a scope.
use scrappy::{web, guard, App, HttpRequest, HttpResponse}; async 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 data<U: 'static>(self, data: U) -> Self
[src]
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 scrappy::{web, App, HttpResponse, Responder}; struct MyData { counter: Cell<usize>, } async fn index(data: web::Data<MyData>) -> impl Responder { data.counter.set(data.counter.get() + 1); HttpResponse::Ok() } 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))) ); }
pub fn app_data<U: 'static>(self, data: U) -> Self
[src]
Set or override application data.
This method overrides data stored with App::app_data()
pub fn configure<F>(self, f: F) -> Self where
F: FnOnce(&mut ServiceConfig),
[src]
F: FnOnce(&mut ServiceConfig),
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 scrappy::{web, 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(|| HttpResponse::Ok())) .route(web::head().to(|| HttpResponse::MethodNotAllowed())) ); } fn main() { let app = App::new() .wrap(middleware::Logger::default()) .service( web::scope("/api") .configure(config) ) .route("/index.html", web::get().to(|| HttpResponse::Ok())); }
pub fn service<F>(self, factory: F) -> Self where
F: HttpServiceFactory + 'static,
[src]
F: HttpServiceFactory + 'static,
Register http service.
This is similar to App's
service registration.
scrappy 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 scrappy::{web, 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))) ); }
pub fn route(self, path: &str, route: Route) -> 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 scrappy::{web, App, HttpResponse}; async 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_service<F, U>(self, f: F) -> Self where
F: IntoServiceFactory<U>,
U: ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
U::InitError: Debug,
[src]
F: IntoServiceFactory<U>,
U: ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
U::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 wrap<M>(
self,
mw: M
) -> Scope<impl ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>> where
M: Transform<T::Service, Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>,
[src]
self,
mw: M
) -> Scope<impl ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>> where
M: Transform<T::Service, Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>,
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<impl ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
R: Future<Output = Result<ServiceResponse, Error>>,
[src]
self,
mw: F
) -> Scope<impl ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()>> where
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
R: Future<Output = Result<ServiceResponse, 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 scrappy_service::Service; use scrappy::{web, App}; use scrappy::http::{header::CONTENT_TYPE, HeaderValue}; async fn index() -> &'static str { "Welcome!" } fn main() { let app = App::new().service( web::scope("/app") .wrap_fn(|req, srv| { let fut = srv.call(req); async { let mut res = fut.await?; res.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static("text/plain"), ); Ok(res) } }) .route("/index.html", web::get().to(index))); }
Trait Implementations
impl<T> HttpServiceFactory for Scope<T> where
T: ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
[src]
T: ServiceFactory<Config = (), Request = ServiceRequest, Response = ServiceResponse, Error = Error, InitError = ()> + 'static,
fn register(self, config: &mut AppService)
[src]
Auto Trait Implementations
impl<T = ScopeEndpoint> !RefUnwindSafe for Scope<T>
impl<T = ScopeEndpoint> !Send for Scope<T>
impl<T = ScopeEndpoint> !Sync for Scope<T>
impl<T> Unpin for Scope<T> where
T: Unpin,
T: Unpin,
impl<T = ScopeEndpoint> !UnwindSafe for Scope<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> 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, U> TryInto<U> 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<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,