actix_plus_static_files/
lib.rs

1#![doc(test(no_crate_inject))]
2/*!
3# actix-plus-static-files
4
5## Legal
6
7Dual-licensed under `MIT` or the [UNLICENSE](http://unlicense.org/).
8
9## Overview
10
11- Embed static resources in executable via convenient macro
12- Serve static resources as directory in `actix-web`
13- Support for angular-like routers
14- Fork of actix-web-static-files by Alexander Korolev
15
16## Usage
17
18### Use-case #1: Static resources folder
19
20Create folder with static resources in your project (for example `static`):
21
22```bash
23cd project_dir
24mkdir static
25echo "Hello, world" > static/hello
26```
27
28Add to `Cargo.toml` dependency to `actix-web-static-files`:
29
30```toml
31[dependencies]
32actix-plus-static-files = "0.1.0"
33```
34
35Include static files in Actix Web application:
36
37```rust
38use actix_web::{App, HttpServer};
39use actix_plus_static_files::{build_hashmap_from_included_dir, ResourceFiles, Dir, include_dir};
40
41const DIR: Dir = include_dir!("./examples/static");
42
43#[actix_web::main]
44async fn main() {
45    HttpServer::new(|| {
46        let hash_map = build_hashmap_from_included_dir(&DIR);
47        App::new().service(ResourceFiles::new("/", hash_map))
48    })
49        .bind("127.0.0.1:8192")
50        .expect("Failed to bind to port")
51        .run()
52        .await
53        .expect("Failed to run server");
54}
55
56```
57
58Run the server:
59
60```bash
61cargo run
62```
63
64Request the resource:
65
66```bash
67$ curl -v http://localhost:8080/static/hello
68*   Trying 127.0.0.1:8080...
69* TCP_NODELAY set
70* Connected to localhost (127.0.0.1) port 8080 (#0)
71> GET /static/hello HTTP/1.1
72> Host: localhost:8080
73> User-Agent: curl/7.65.3
74>
75* Mark bundle as not supporting multiuse
76< HTTP/1.1 200 OK
77< content-length: 13
78< date: Tue, 06 Aug 2019 13:36:50 GMT
79<
80Hello, world
81* Connection #0 to host localhost left intact
82```
83
84### Use-case #2: Angular-like applications
85
86If you are using Angular (or any of a large variety of other such libraries, such as Svelte + Routify) 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.
87
88```rust
89use actix_web::{App, HttpServer};
90use actix_plus_static_files::{build_hashmap_from_included_dir, ResourceFiles, Dir, include_dir};
91
92const DIR: Dir = include_dir!("./examples/static");
93
94#[actix_web::main]
95async fn main() {
96    HttpServer::new(|| {
97        let hash_map = build_hashmap_from_included_dir(&DIR);
98        App::new().service(ResourceFiles::new("/", hash_map).resolve_not_found_to_root())
99    })
100        .bind("127.0.0.1:8192")
101        .expect("Failed to bind to port")
102        .run()
103        .await
104        .expect("Failed to run server");
105}
106
107```
108
109Remember to place you static resources route after all other routes.
110*/
111
112mod fs_macro;
113mod r#impl;
114
115pub use fs_macro::build_hashmap_from_included_dir;
116pub use include_dir::{include_dir, Dir};
117pub use r#impl::{
118    Resource, ResourceFiles, ResourceFilesInner, ResourceFilesService, UriSegmentError,
119};