#static-file #serve-static #tower-service #axum #assets #embed #directory

tower-serve-static

用于提供静态文件的 Tower 服务

2 个版本

0.1.1 2024年1月25日
0.1.0 2024年1月5日

#495文件系统

Download history 296/week @ 2024-04-21 205/week @ 2024-04-28 107/week @ 2024-05-05 142/week @ 2024-05-12 107/week @ 2024-05-19 271/week @ 2024-05-26 110/week @ 2024-06-02 201/week @ 2024-06-09 144/week @ 2024-06-16 143/week @ 2024-06-23 132/week @ 2024-06-30 199/week @ 2024-07-07 159/week @ 2024-07-14 749/week @ 2024-07-21 198/week @ 2024-07-28 494/week @ 2024-08-04

1,630 每月下载次数

MIT 许可证

33KB
630 代码行

Tower Serve Static

crates.io docs.rs Build Status dependency status codecov Lines Of Code

Tower 文件服务,使用 include_dirinclude_bytes 将资源嵌入到二进制文件中。

用法

Cargo.toml

tower-serve-static = { git = "https://github.com/jannik4/tower-serve-static", version = "0.1.0" }
include_dir = "0.7.0"

提供静态文件

use tower_serve_static::{ServeFile, include_file};

// File is located relative to `CARGO_MANIFEST_DIR` (the directory containing the manifest of your package).
// This will embed and serve the `README.md` file.
let service = ServeFile::new(include_file!("/README.md"));

// Run our service using `axum`
let app = axum::Router::new().nest_service("/", service);

// run our app with axum, listening locally on port 3000
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
axum::serve(listener, app).await?;

提供静态目录

use tower_serve_static::{ServeDir};
use include_dir::{Dir, include_dir};

// Use `$CARGO_MANIFEST_DIR` to make path relative to your package.
// This will embed and serve files in the `src` directory and its subdirectories.
static ASSETS_DIR: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/src");
let service = ServeDir::new(&ASSETS_DIR);

// Run our service using `axum`
let app = axum::Router::new().nest_service("/", service);

// run our app with axum, listening locally on port 3000
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
axum::serve(listener, app).await?;

鸣谢

实现基于 tower-http 文件服务(更具体地说,是 版本 0.1.2)并修改为在运行时使用 include_dir/include_bytes 而不是文件系统。

依赖

~4–6MB
~100K SLoC