implement httpservicefactory for named file

This commit is contained in:
ibraheemdev 2021-04-01 15:36:56 -04:00
parent 36ff76141f
commit cd600a5118
1 changed files with 38 additions and 2 deletions

View File

@ -1,5 +1,6 @@
use actix_service::{Service, ServiceFactory}; use actix_service::{Service, ServiceFactory};
use actix_utils::future::{ok, ready, Ready}; use actix_utils::future::{ok, ready, Ready};
use actix_web::dev::{AppService, HttpServiceFactory, ResourceDef};
use std::fs::{File, Metadata}; use std::fs::{File, Metadata};
use std::io; use std::io;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@ -41,6 +42,29 @@ impl Default for Flags {
} }
/// A file with an associated name. /// A file with an associated name.
///
/// `NamedFile` can be registered as services:
/// ```
/// use actix_web::App;
/// use actix_files::NamedFile;
///
/// # fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let app = App::new()
/// .service(NamedFile::open("./static/index.html")?);
/// # Ok(())
/// # }
/// ```
///
/// They can also be returned from handlers:
/// ```
/// use actix_web::{Responder, get};
/// use actix_files::NamedFile;
///
/// #[get("/")]
/// async fn index() -> impl Responder {
/// NamedFile::open("./static/index.html")
/// }
/// ```
#[derive(Debug)] #[derive(Debug)]
pub struct NamedFile { pub struct NamedFile {
path: PathBuf, path: PathBuf,
@ -487,9 +511,9 @@ impl ServiceFactory<ServiceRequest> for NamedFile {
type Response = ServiceResponse; type Response = ServiceResponse;
type Error = Error; type Error = Error;
type Config = (); type Config = ();
type InitError = io::Error; type InitError = ();
type Service = NamedFileService; type Service = NamedFileService;
type Future = Ready<Result<Self::Service, io::Error>>; type Future = Ready<Result<Self::Service, ()>>;
fn new_service(&self, _: ()) -> Self::Future { fn new_service(&self, _: ()) -> Self::Future {
ok(NamedFileService { ok(NamedFileService {
@ -521,3 +545,15 @@ impl Service<ServiceRequest> for NamedFileService {
) )
} }
} }
impl HttpServiceFactory for NamedFile {
fn register(self, config: &mut AppService) {
let rdef = if config.is_root() {
ResourceDef::root_prefix(self.path.to_string_lossy().as_ref())
} else {
ResourceDef::prefix(self.path.to_string_lossy().as_ref())
};
config.register_service(rdef, None, self, None)
}
}