static_file_util
static_file_util is a utility crate for generating and managing static files in Rust applications. It simplifies the
process of embedding static assets into your project and automatically provides hashes for cache-busting, making it
ideal for web applications and similar use cases.
Check the axum example for details of how to use this in a web application to serve static files, like images or CSS.
Features
- Define static files with ease using a single macro.
- Automatically computes content hashes, allowing for effective cache control.
- Provides a convenient interface to access static files at runtime.
Installation
Add static_file_util as a dependency in your Cargo.toml:
[]
= "0.2"
= "1.4" # Required dependency for lazy static initialization
= "0.3" # For handling MIME types
[]
= { = "0.2", = ["process_file"] }
PLEASE NOTE: In v0.2 the process_file feature is required for the build-dependencies.
Usage
This usage example demonstrates the steps required to make use of static_file_util.
- The file hashes are calculated using a
build.rsscript. - The files are defined using the
static_files!macro. - The
.nameproperty is used to access the generated file names (including the hash). - The StaticFile struct is utilised to resolve the files and serve up the content with the correct MIME type.
Environment Variable for Hashing
In order to generate unique names for each file, a build.rs script can be used to generate content hashes during the
build process. These hashes are passed as environment variables to the main code.
Here's an example of a build.rs script that works with static_file_util:
use process_file;
The process_file function reads the file contents, generates a hash, and sets it as an environment variable that the
macro uses during compilation.
It is recommended to add this build.rs script to your project in order to automatically generate the
environment variables required to define static files.
Define Static Files
Use the static_files! macro to define your static assets. This macro allows you to embed files directly into your
binary, providing easy access to their contents and metadata.
Here's an example of how to use static_file_util in your project:
use static_files;
static_files!;
Axum Example
Here's an example of using static_file_util with the Axum web framework to serve static files:
use Body;
use Path;
use ;
use IntoResponse;
// StaticFile definition is created by the static_files! macro
// include it here to use within an Axum handler
use StaticFile;
pub async
How It Works
The original idea for this crate was inspired by the Assets and Cache Busting section of the Rust on Nails guide. That made use of the Ructe HTML template system for Rust. This solution is a more minimal implementation. It is just focused on embedding static assets and cache busting using generated hashes.
The static_files! macro generates:
- A
StaticFilestruct for each file, containing:content: The raw content of the file as a byte slice.name: The name of the file with a unique hash appended for cache-busting.mime: The MIME type of the file.
- A
STATICSvector that holds references to all the defined static files. - A utility function
StaticFile::get(path)is provided for easy lookup of a file based on the path.
Example Use Cases
- Web Applications: Embed images, stylesheets, and other static resources directly into your binary.
- Caching: The unique hash in each static file's name ensures that assets can be cached effectively by browsers while ensuring cache invalidation when content changes.
Documentation
The complete documentation for static_file_util is available on docs.rs.
License
This project is licensed under either the MIT license or the Apache License 2.0, at your option.