Skip to content

Commit

Permalink
Merge pull request #28 from kilork/not_found_resolves_to
Browse files Browse the repository at this point in the history
Not found resolves to default index.html
  • Loading branch information
kilork authored Dec 18, 2020
2 parents 5107a29 + 6ef1f71 commit f9e2760
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Dual-licensed under `MIT` or the [UNLICENSE](http://unlicense.org/).
- Install dependencies with [npm](https://npmjs.org) package manager
- Run custom `npm` run commands (such as [webpack](https://webpack.js.org/))
- Support for npm-like package managers ([yarn](https://yarnpkg.com/))
- Support for angular-like routers

## Usage

Expand Down Expand Up @@ -61,7 +62,7 @@ use std::collections::HashMap;

include!(concat!(env!("OUT_DIR"), "/generated.rs"));

#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let generated = generate();
Expand All @@ -70,7 +71,7 @@ async fn main() -> std::io::Result<()> {
))
})
.bind("127.0.0.1:8080")?
.start()
.run()
.await
}
```
Expand Down Expand Up @@ -258,7 +259,7 @@ use std::collections::HashMap;

include!(concat!(env!("OUT_DIR"), "/generated.rs"));

#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let generated = generate();
Expand Down Expand Up @@ -326,3 +327,33 @@ fn main() {
.build().unwrap();
}
```
### Use-case #5: Angular-like applications
If you are using Angular as frontend, you may want to resolve all not found calls via `index.html` of frontend app. To do this just call method `resolve_not_found_to_root` after resource creation.
```rust
use actix_web::{App, HttpServer};
use actix_web_static_files;

use std::collections::HashMap;

include!(concat!(env!("OUT_DIR"), "/generated.rs"));

#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let generated = generate();
App::new().service(actix_web_static_files::ResourceFiles::new(
"/", generated,
).resolve_not_found_to_root(),)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
```
Remember to place you static resources route after all other routes.
You can check complete example [Angular Router Sample](https://github.com/kilork/actix-web-static-files-example-angular-router).
32 changes: 30 additions & 2 deletions src/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct Resource {
/// ```
pub struct ResourceFiles {
not_resolve_defaults: bool,
not_found_resolves_to: Option<String>,
inner: Rc<ResourceFilesInner>,
}

Expand All @@ -60,6 +61,8 @@ pub struct ResourceFilesInner {
files: HashMap<&'static str, Resource>,
}

const INDEX_HTML: &str = "index.html";

impl ResourceFiles {
pub fn new(path: &str, files: HashMap<&'static str, Resource>) -> Self {
let inner = ResourceFilesInner {
Expand All @@ -69,6 +72,7 @@ impl ResourceFiles {
Self {
inner: Rc::new(inner),
not_resolve_defaults: false,
not_found_resolves_to: None,
}
}

Expand All @@ -78,6 +82,21 @@ impl ResourceFiles {
self.not_resolve_defaults = true;
self
}

/// Resolves not found references to this path.
///
/// This can be useful for angular-like applications.
pub fn resolve_not_found_to<S: ToString>(mut self, path: S) -> Self {
self.not_found_resolves_to = Some(path.to_string());
self
}

/// Resolves not found references to root path.
///
/// This can be useful for angular-like applications.
pub fn resolve_not_found_to_root(self) -> Self {
self.resolve_not_found_to(INDEX_HTML)
}
}

impl Deref for ResourceFiles {
Expand Down Expand Up @@ -111,6 +130,7 @@ impl ServiceFactory for ResourceFiles {
fn new_service(&self, _: ()) -> Self::Future {
ok(ResourceFilesService {
resolve_defaults: !self.not_resolve_defaults,
not_found_resolves_to: self.not_found_resolves_to.clone(),
inner: self.inner.clone(),
})
.boxed_local()
Expand All @@ -119,6 +139,7 @@ impl ServiceFactory for ResourceFiles {

pub struct ResourceFilesService {
resolve_defaults: bool,
not_found_resolves_to: Option<String>,
inner: Rc<ResourceFilesInner>,
}

Expand Down Expand Up @@ -162,7 +183,7 @@ impl<'a> Service for ResourceFilesService {
&& self.resolve_defaults
&& (req_path.is_empty() || req_path.ends_with("/"))
{
let index_req_path = req_path.to_string() + "index.html";
let index_req_path = req_path.to_string() + INDEX_HTML;
item = self.files.get(index_req_path.as_str());
}

Expand All @@ -177,7 +198,14 @@ impl<'a> Service for ResourceFilesService {
};

let (req, _) = req.into_parts();
let item = self.files.get(real_path.as_str());

let mut item = self.files.get(real_path.as_str());

if item.is_none() && self.not_found_resolves_to.is_some() {
let not_found_path = self.not_found_resolves_to.as_ref().unwrap();
item = self.files.get(not_found_path.as_str());
}

let response = respond_to(&req, item);
(req, response)
};
Expand Down
34 changes: 32 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Dual-licensed under `MIT` or the [UNLICENSE](http://unlicense.org/).
- Install dependencies with [npm](https://npmjs.org) package manager
- Run custom `npm` run commands (such as [webpack](https://webpack.js.org/))
- Support for npm-like package managers ([yarn](https://yarnpkg.com/))
- Support for angular-like routers
## Usage
Expand Down Expand Up @@ -63,7 +64,7 @@ use std::collections::HashMap;
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let generated = generate();
Expand Down Expand Up @@ -260,7 +261,7 @@ use std::collections::HashMap;
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let generated = generate();
Expand Down Expand Up @@ -329,6 +330,35 @@ fn main() {
}
```
### Use-case #5: Angular-like applications
If you are using Angular as frontend, you may want to resolve all not found calls via `index.html` of frontend app. To do this just call method `resolve_not_found_to_root` after resource creation.
```rust#ignore
use actix_web::{App, HttpServer};
use actix_web_static_files;
use std::collections::HashMap;
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let generated = generate();
App::new().service(actix_web_static_files::ResourceFiles::new(
"/", generated,
).resolve_not_found_to_root(),)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
```
Remember to place you static resources route after all other routes.
You can check complete example [Angular Router Sample](https://github.com/kilork/actix-web-static-files-example-angular-router).
*/

mod r#impl;
Expand Down

0 comments on commit f9e2760

Please sign in to comment.