Crates.io | actix-plus-static-files |
lib.rs | actix-plus-static-files |
version | 0.1.0 |
source | src |
created_at | 2021-04-05 03:08:46.01374 |
updated_at | 2021-04-05 03:08:46.01374 |
description | A library that integrates with actix-web and include_dir to cleanly package, via a macro, static resources (e.g. a frontend) with an actix-web binary, thus serving these resources from RAM at runtime and simplifying deployment by containing the entire web application in a single executable binary; Fork of actix-web-static-files by Alexander Korolev |
homepage | https://github.com/john01dav/actix-plus |
repository | https://github.com/john01dav/actix-plus |
max_upload_size | |
id | 379107 |
size | 64,505 |
Dual-licensed under MIT
or the UNLICENSE.
actix-web
Create folder with static resources in your project (for example static
):
cd project_dir
mkdir static
echo "Hello, world" > static/hello
Add to Cargo.toml
dependency to actix-web-static-files
:
[dependencies]
actix-plus-static-files = "0.1.0"
Include static files in Actix Web application:
use actix_web::{App, HttpServer};
use actix_plus_static_files::{build_hashmap_from_included_dir, ResourceFiles, Dir, include_dir};
const DIR: Dir = include_dir!("./examples/static");
#[actix_web::main]
async fn main() {
HttpServer::new(|| {
let hash_map = build_hashmap_from_included_dir(&DIR);
App::new().service(ResourceFiles::new("/", hash_map))
})
.bind("127.0.0.1:8192")
.expect("Failed to bind to port")
.run()
.await
.expect("Failed to run server");
}
Run the server:
cargo run
Request the resource:
$ curl -v http://localhost:8080/static/hello
* Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /static/hello HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.65.3
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-length: 13
< date: Tue, 06 Aug 2019 13:36:50 GMT
<
Hello, world
* Connection #0 to host localhost left intact
If 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.
use actix_web::{App, HttpServer};
use actix_plus_static_files::{build_hashmap_from_included_dir, ResourceFiles, Dir, include_dir};
const DIR: Dir = include_dir!("./examples/static");
#[actix_web::main]
async fn main() {
HttpServer::new(|| {
let hash_map = build_hashmap_from_included_dir(&DIR);
App::new().service(ResourceFiles::new("/", hash_map).resolve_not_found_to_root())
})
.bind("127.0.0.1:8192")
.expect("Failed to bind to port")
.run()
.await
.expect("Failed to run server");
}
Remember to place you static resources route after all other routes.