actix-web static files as resources support
Legal
Dual-licensed under MIT
or the UNLICENSE.
Features
- Embed static resources in executuble
- Serve static resources as directory in
actix-web
- Install dependencies with npm package manager
- Run custom
npm
run commands (such as webpack) - Support for npm-like package managers (yarn)
- Support for angular-like routers
Usage
Use-case #1: Static resources folder
Create folder with static resources in your project (for example static
):
Add to Cargo.toml
dependency to actix-web-static-files
:
[]
= "3.0"
[]
= "3.0"
Add build script to Cargo.toml
:
[]
= "build.rs"
Add build.rs
with call to bundle resources:
use resource_dir;
Include generated code in main.rs
:
use ;
use actix_web_static_files;
include!;
async
Run the server:
Request the resource:
> GET
> Host:
> User-Agent:
>
< HTTP/1.1
< content-length:
< date:
<
) )
Use-case #2: package.json - npm managed folder
Create folder with static resources in your project (for example static
):
# install your npm dependencies (here we use fontawesome as an example)
Add generated folder to ignore file of your version control system (here: git):
Add dependencies
and build-dependencies
in Cargo.toml
same way as in the first use-case.
Add build.rs
with call to bundle resources:
use npm_resource_dir;
Include generated code in main.rs
same way as in the first use-case.
Reference resources in your HTML
:
Hi
Use-case #3: package.json - WebPack usage
Create folder with static resources in your project (for example web
), install required packages and webpack:
# install lodash for usage in example
# install webpack npm dependencies
Add web/webpack.config.js
:
const path = require;
const = require;
const HtmlWebpackPlugin = require;
module.exports = ;
Add web/src/index.js
:
import _ from 'lodash';
document..;
Modify web/package.json
by adding "scripts" sections:
Add to Cargo.toml
dependency to actix-web-static-files
as in the first use case.
Add build script to Cargo.toml
as in the first use case.
Add build.rs
with call to bundle resources:
use NpmBuild;
Include generated code in src/main.rs
:
use ;
use actix_web_static_files;
include!;
async
Run the server:
Request the resource:
> GET
> Host:
> User-Agent:
>
< HTTP/1.1
< content-length:
< content-type:
< etag:
< date:
<
<!DOCTYPE <html>
<head>
<meta charset="UTF-8">
<title>actix-web-static-files </head>
<body>
<script type="text/javascript" src="main.js"></script></body>
) )
Use-case #4: yarn package manager
We can use another package manager instead of npm
. For example, to use yarn just add .executable("yarn")
to NpmBuild
call:
use NpmBuild;
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.
use ;
use actix_web_static_files;
include!;
async
Remember to place you static resources route after all other routes.
You can check complete example Angular Router Sample.