2019-03-07 00:47:15 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
use actix_http::Extensions;
|
2019-03-07 00:47:15 +01:00
|
|
|
use actix_router::ResourceDef;
|
2019-11-20 18:33:22 +01:00
|
|
|
use actix_service::{boxed, IntoServiceFactory, ServiceFactory};
|
2019-03-07 00:47:15 +01:00
|
|
|
|
2021-01-16 00:37:33 +01:00
|
|
|
use crate::data::Data;
|
2019-03-11 00:35:38 +01:00
|
|
|
use crate::error::Error;
|
2019-03-07 00:47:15 +01:00
|
|
|
use crate::guard::Guard;
|
2019-04-04 00:09:31 +02:00
|
|
|
use crate::resource::Resource;
|
2019-03-09 16:39:34 +01:00
|
|
|
use crate::rmap::ResourceMap;
|
2019-04-04 00:09:31 +02:00
|
|
|
use crate::route::Route;
|
|
|
|
use crate::service::{
|
2019-11-20 18:33:22 +01:00
|
|
|
AppServiceFactory, HttpServiceFactory, ServiceFactoryWrapper, ServiceRequest,
|
2019-04-04 00:09:31 +02:00
|
|
|
ServiceResponse,
|
|
|
|
};
|
2019-03-07 00:47:15 +01:00
|
|
|
|
2019-07-17 11:48:37 +02:00
|
|
|
type Guards = Vec<Box<dyn Guard>>;
|
2021-02-12 00:03:17 +01:00
|
|
|
type HttpNewService = boxed::BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
2019-03-07 00:47:15 +01:00
|
|
|
|
|
|
|
/// Application configuration
|
2019-04-15 16:32:49 +02:00
|
|
|
pub struct AppService {
|
2019-03-09 23:06:24 +01:00
|
|
|
config: AppConfig,
|
2019-03-07 00:47:15 +01:00
|
|
|
root: bool,
|
2019-04-13 23:50:54 +02:00
|
|
|
default: Rc<HttpNewService>,
|
2019-03-09 16:39:34 +01:00
|
|
|
services: Vec<(
|
|
|
|
ResourceDef,
|
2019-04-13 23:50:54 +02:00
|
|
|
HttpNewService,
|
2019-03-09 16:39:34 +01:00
|
|
|
Option<Guards>,
|
|
|
|
Option<Rc<ResourceMap>>,
|
|
|
|
)>,
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 16:32:49 +02:00
|
|
|
impl AppService {
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Crate server settings instance.
|
|
|
|
pub(crate) fn new(config: AppConfig, default: Rc<HttpNewService>) -> Self {
|
2019-04-15 16:32:49 +02:00
|
|
|
AppService {
|
2019-03-09 23:06:24 +01:00
|
|
|
config,
|
2019-03-07 00:47:15 +01:00
|
|
|
default,
|
|
|
|
root: true,
|
|
|
|
services: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:09:35 +02:00
|
|
|
/// Check if root is being configured
|
2019-03-07 00:47:15 +01:00
|
|
|
pub fn is_root(&self) -> bool {
|
|
|
|
self.root
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn into_services(
|
|
|
|
self,
|
2019-05-05 04:43:49 +02:00
|
|
|
) -> (
|
|
|
|
AppConfig,
|
|
|
|
Vec<(
|
|
|
|
ResourceDef,
|
|
|
|
HttpNewService,
|
|
|
|
Option<Guards>,
|
|
|
|
Option<Rc<ResourceMap>>,
|
|
|
|
)>,
|
|
|
|
) {
|
|
|
|
(self.config, self.services)
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
/// Clones inner config and default service, returning new `AppService` with empty service list
|
|
|
|
/// marked as non-root.
|
2019-03-07 00:47:15 +01:00
|
|
|
pub(crate) fn clone_config(&self) -> Self {
|
2019-04-15 16:32:49 +02:00
|
|
|
AppService {
|
2019-03-09 23:06:24 +01:00
|
|
|
config: self.config.clone(),
|
2019-03-07 00:47:15 +01:00
|
|
|
default: self.default.clone(),
|
|
|
|
services: Vec::new(),
|
|
|
|
root: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
/// Returns reference to configuration.
|
2019-03-09 23:06:24 +01:00
|
|
|
pub fn config(&self) -> &AppConfig {
|
|
|
|
&self.config
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
/// Returns default handler factory.
|
2019-04-13 23:50:54 +02:00
|
|
|
pub fn default_service(&self) -> Rc<HttpNewService> {
|
2019-03-07 00:47:15 +01:00
|
|
|
self.default.clone()
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Register HTTP service.
|
2019-03-07 00:47:15 +01:00
|
|
|
pub fn register_service<F, S>(
|
|
|
|
&mut self,
|
|
|
|
rdef: ResourceDef,
|
2019-07-17 11:48:37 +02:00
|
|
|
guards: Option<Vec<Box<dyn Guard>>>,
|
2019-11-20 18:33:22 +01:00
|
|
|
factory: F,
|
2019-03-09 16:39:34 +01:00
|
|
|
nested: Option<Rc<ResourceMap>>,
|
2019-03-07 00:47:15 +01:00
|
|
|
) where
|
2021-01-04 00:47:04 +01:00
|
|
|
F: IntoServiceFactory<S, ServiceRequest>,
|
2019-11-20 18:33:22 +01:00
|
|
|
S: ServiceFactory<
|
2021-01-04 00:47:04 +01:00
|
|
|
ServiceRequest,
|
2019-03-07 00:47:15 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2021-06-25 14:19:42 +02:00
|
|
|
Config = (),
|
2019-03-07 00:47:15 +01:00
|
|
|
InitError = (),
|
|
|
|
> + 'static,
|
|
|
|
{
|
2021-02-12 00:03:17 +01:00
|
|
|
self.services
|
|
|
|
.push((rdef, boxed::factory(factory.into_factory()), guards, nested));
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-09 23:06:24 +01:00
|
|
|
|
2021-04-02 09:26:59 +02:00
|
|
|
/// Application connection config.
|
|
|
|
#[derive(Debug, Clone)]
|
2021-01-10 23:59:44 +01:00
|
|
|
pub struct AppConfig {
|
2019-12-20 11:04:51 +01:00
|
|
|
secure: bool,
|
|
|
|
host: String,
|
|
|
|
addr: SocketAddr,
|
|
|
|
}
|
2019-03-09 23:06:24 +01:00
|
|
|
|
|
|
|
impl AppConfig {
|
2021-04-02 09:26:59 +02:00
|
|
|
pub(crate) fn new(secure: bool, host: String, addr: SocketAddr) -> Self {
|
2021-03-19 12:25:35 +01:00
|
|
|
AppConfig { secure, host, addr }
|
2019-03-09 23:06:24 +01:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
/// Needed in actix-test crate. Semver exempt.
|
2021-04-02 09:26:59 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn __priv_test_new(secure: bool, host: String, addr: SocketAddr) -> Self {
|
|
|
|
AppConfig::new(secure, host, addr)
|
|
|
|
}
|
|
|
|
|
2019-12-20 11:04:51 +01:00
|
|
|
/// Server host name.
|
2019-03-09 23:06:24 +01:00
|
|
|
///
|
2019-12-09 05:02:43 +01:00
|
|
|
/// Host name is used by application router as a hostname for url generation.
|
2020-12-13 14:28:39 +01:00
|
|
|
/// Check [ConnectionInfo](super::dev::ConnectionInfo::host())
|
2019-12-09 05:02:43 +01:00
|
|
|
/// documentation for more information.
|
2019-03-09 23:06:24 +01:00
|
|
|
///
|
|
|
|
/// By default host name is set to a "localhost" value.
|
|
|
|
pub fn host(&self) -> &str {
|
2021-01-10 23:59:44 +01:00
|
|
|
&self.host
|
2019-03-09 23:06:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if connection is secure(https)
|
|
|
|
pub fn secure(&self) -> bool {
|
2021-01-10 23:59:44 +01:00
|
|
|
self.secure
|
2019-03-09 23:06:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the socket address of the local half of this TCP connection
|
|
|
|
pub fn local_addr(&self) -> SocketAddr {
|
2021-01-10 23:59:44 +01:00
|
|
|
self.addr
|
2019-03-09 23:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 11:04:51 +01:00
|
|
|
impl Default for AppConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
AppConfig::new(
|
|
|
|
false,
|
|
|
|
"localhost:8080".to_owned(),
|
2021-04-02 09:26:59 +02:00
|
|
|
"127.0.0.1:8080".parse().unwrap(),
|
2019-12-20 11:04:51 +01:00
|
|
|
)
|
2019-03-09 23:06:24 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 00:09:31 +02:00
|
|
|
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Enables parts of app configuration to be declared separately from the app itself. Helpful for
|
|
|
|
/// modularizing large applications.
|
|
|
|
///
|
|
|
|
/// Merge a `ServiceConfig` into an app using [`App::configure`](crate::App::configure). Scope and
|
|
|
|
/// resources services have similar methods.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use actix_web::{web, 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()))
|
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // merge `/test` routes from config function to App
|
|
|
|
/// App::new().configure(config);
|
|
|
|
/// ```
|
2019-04-15 16:32:49 +02:00
|
|
|
pub struct ServiceConfig {
|
2019-11-20 18:33:22 +01:00
|
|
|
pub(crate) services: Vec<Box<dyn AppServiceFactory>>,
|
2019-04-04 00:09:31 +02:00
|
|
|
pub(crate) external: Vec<ResourceDef>,
|
2021-01-16 00:37:33 +01:00
|
|
|
pub(crate) app_data: Extensions,
|
2019-04-04 00:09:31 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 16:32:49 +02:00
|
|
|
impl ServiceConfig {
|
2019-04-04 00:09:31 +02:00
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
services: Vec::new(),
|
|
|
|
external: Vec::new(),
|
2021-01-16 00:37:33 +01:00
|
|
|
app_data: Extensions::new(),
|
2019-04-04 00:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Add shared app data item.
|
2019-04-04 00:09:31 +02:00
|
|
|
///
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Counterpart to [`App::data()`](crate::App::data).
|
2021-06-24 16:10:51 +02:00
|
|
|
#[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
|
2021-01-16 00:37:33 +01:00
|
|
|
pub fn data<U: 'static>(&mut self, data: U) -> &mut Self {
|
|
|
|
self.app_data(Data::new(data));
|
2019-04-04 00:09:31 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Add arbitrary app data item.
|
2020-10-26 18:02:45 +01:00
|
|
|
///
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Counterpart to [`App::app_data()`](crate::App::app_data).
|
2020-10-26 18:02:45 +01:00
|
|
|
pub fn app_data<U: 'static>(&mut self, ext: U) -> &mut Self {
|
2021-01-16 00:37:33 +01:00
|
|
|
self.app_data.insert(ext);
|
2020-10-26 18:02:45 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-04 00:09:31 +02:00
|
|
|
/// Configure route for a specific path.
|
|
|
|
///
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Counterpart to [`App::route()`](crate::App::route).
|
2019-04-13 23:50:54 +02:00
|
|
|
pub fn route(&mut self, path: &str, mut route: Route) -> &mut Self {
|
2019-04-04 00:09:31 +02:00
|
|
|
self.service(
|
|
|
|
Resource::new(path)
|
|
|
|
.add_guards(route.take_guards())
|
|
|
|
.route(route),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Register HTTP service factory.
|
2019-04-04 00:09:31 +02:00
|
|
|
///
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Counterpart to [`App::service()`](crate::App::service).
|
2019-04-04 00:09:31 +02:00
|
|
|
pub fn service<F>(&mut self, factory: F) -> &mut Self
|
|
|
|
where
|
2019-04-13 23:50:54 +02:00
|
|
|
F: HttpServiceFactory + 'static,
|
2019-04-04 00:09:31 +02:00
|
|
|
{
|
|
|
|
self.services
|
|
|
|
.push(Box::new(ServiceFactoryWrapper::new(factory)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register an external resource.
|
|
|
|
///
|
2021-01-16 00:37:33 +01:00
|
|
|
/// External resources are useful for URL generation purposes only and are never considered for
|
|
|
|
/// matching at request time. Calls to [`HttpRequest::url_for()`](crate::HttpRequest::url_for)
|
|
|
|
/// will work as expected.
|
2019-04-04 00:09:31 +02:00
|
|
|
///
|
2021-01-16 00:37:33 +01:00
|
|
|
/// Counterpart to [`App::external_resource()`](crate::App::external_resource).
|
2019-04-04 00:09:31 +02:00
|
|
|
pub fn external_resource<N, U>(&mut self, name: N, url: U) -> &mut Self
|
|
|
|
where
|
|
|
|
N: AsRef<str>,
|
|
|
|
U: AsRef<str>,
|
|
|
|
{
|
|
|
|
let mut rdef = ResourceDef::new(url.as_ref());
|
|
|
|
*rdef.name_mut() = name.as_ref().to_string();
|
|
|
|
self.external.push(rdef);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use actix_service::Service;
|
2019-04-24 06:21:49 +02:00
|
|
|
use bytes::Bytes;
|
2019-04-04 00:09:31 +02:00
|
|
|
|
|
|
|
use super::*;
|
2019-04-15 16:44:07 +02:00
|
|
|
use crate::http::{Method, StatusCode};
|
2019-11-26 06:25:50 +01:00
|
|
|
use crate::test::{call_service, init_service, read_body, TestRequest};
|
2019-04-24 06:21:49 +02:00
|
|
|
use crate::{web, App, HttpRequest, HttpResponse};
|
2019-04-04 00:09:31 +02:00
|
|
|
|
2021-06-24 16:10:51 +02:00
|
|
|
// allow deprecated `ServiceConfig::data`
|
|
|
|
#[allow(deprecated)]
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_data() {
|
|
|
|
let cfg = |cfg: &mut ServiceConfig| {
|
|
|
|
cfg.data(10usize);
|
2020-10-26 18:02:45 +01:00
|
|
|
cfg.app_data(15u8);
|
2019-11-26 06:25:50 +01:00
|
|
|
};
|
2019-04-04 00:09:31 +02:00
|
|
|
|
2021-02-12 00:03:17 +01:00
|
|
|
let srv = init_service(App::new().configure(cfg).service(web::resource("/").to(
|
|
|
|
|_: web::Data<usize>, req: HttpRequest| {
|
2020-10-26 18:02:45 +01:00
|
|
|
assert_eq!(*req.app_data::<u8>().unwrap(), 15u8);
|
|
|
|
HttpResponse::Ok()
|
2021-02-12 00:03:17 +01:00
|
|
|
},
|
|
|
|
)))
|
2020-10-26 18:02:45 +01:00
|
|
|
.await;
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_request();
|
|
|
|
let resp = srv.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-04-04 00:09:31 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
// #[actix_rt::test]
|
|
|
|
// async fn test_data_factory() {
|
2019-05-05 04:43:49 +02:00
|
|
|
// let cfg = |cfg: &mut ServiceConfig| {
|
|
|
|
// cfg.data_factory(|| {
|
|
|
|
// sleep(std::time::Duration::from_millis(50)).then(|_| {
|
|
|
|
// println!("READY");
|
|
|
|
// Ok::<_, ()>(10usize)
|
|
|
|
// })
|
|
|
|
// });
|
|
|
|
// };
|
|
|
|
|
2021-02-07 02:00:40 +01:00
|
|
|
// let srv =
|
2019-05-05 04:43:49 +02:00
|
|
|
// init_service(App::new().configure(cfg).service(
|
|
|
|
// web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
|
|
|
// ));
|
|
|
|
// let req = TestRequest::default().to_request();
|
2019-11-26 06:25:50 +01:00
|
|
|
// let resp = srv.call(req).await.unwrap();
|
2019-05-05 04:43:49 +02:00
|
|
|
// assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
// let cfg2 = |cfg: &mut ServiceConfig| {
|
|
|
|
// cfg.data_factory(|| Ok::<_, ()>(10u32));
|
|
|
|
// };
|
2021-02-07 02:00:40 +01:00
|
|
|
// let srv = init_service(
|
2019-05-05 04:43:49 +02:00
|
|
|
// App::new()
|
|
|
|
// .service(web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()))
|
|
|
|
// .configure(cfg2),
|
|
|
|
// );
|
|
|
|
// let req = TestRequest::default().to_request();
|
2019-11-26 06:25:50 +01:00
|
|
|
// let resp = srv.call(req).await.unwrap();
|
2019-05-05 04:43:49 +02:00
|
|
|
// assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
// }
|
2019-04-15 16:44:07 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_external_resource() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(
|
2019-11-26 06:25:50 +01:00
|
|
|
App::new()
|
|
|
|
.configure(|cfg| {
|
2021-02-12 00:03:17 +01:00
|
|
|
cfg.external_resource("youtube", "https://youtube.com/watch/{video_id}");
|
2019-11-26 06:25:50 +01:00
|
|
|
})
|
|
|
|
.route(
|
|
|
|
"/test",
|
|
|
|
web::get().to(|req: HttpRequest| {
|
2021-02-12 00:03:17 +01:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.body(req.url_for("youtube", &["12345"]).unwrap().to_string())
|
2019-11-26 06:25:50 +01:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
let body = read_body(resp).await;
|
|
|
|
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
2019-04-24 06:21:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_service() {
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = init_service(App::new().configure(|cfg| {
|
2021-02-12 00:03:17 +01:00
|
|
|
cfg.service(web::resource("/test").route(web::get().to(HttpResponse::Created)))
|
|
|
|
.route("/index.html", web::get().to(HttpResponse::Ok));
|
2019-11-26 06:25:50 +01:00
|
|
|
}))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/index.html")
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let resp = call_service(&srv, req).await;
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-04-15 16:44:07 +02:00
|
|
|
}
|
2019-04-04 00:09:31 +02:00
|
|
|
}
|