< 返回版块

Liangdi 发表于 2025-08-11 14:50

Tags:spa,actix,axum

起源

rust 通过 rust-embed 或者 include_dir! 等库,很容易将 spa 应用打包到二进制中。 但是每次都需要根据不同web 框架配置,设置路由,错误处理等,用的多了就考虑封装成一个库,最终以宏的方式实现。

功能说明

  • 支持将某个资源目录自动配置到 rust-embed ,以及通过一个暴露出来的方法绑定到 web 框架中
  • 支持多个目录并存,比如 index 一个 , dashboard 一个
  • 支持设置路由路径,首页文件等。

简单使用说明(axum)

use anycms_spa::spa;
use axum::{
    routing::get, Router
};
spa!(Spa, "assets");
spa!(Dashboard, "dashboard", "/dashboard", ["index.html"]);

#[tokio::main]
async fn main() {
    // initialize tracing
    tracing_subscriber::fmt::init();

    // build our application with a route
    let app = Router::new()
        // `GET /` goes to `root`
        
        .route("/home", get(root))
        .merge(Dashboard::spa_router())
        .merge(Spa::spa_router());

    // run our app with hyper, listening globally on port 3000
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

// basic handler that responds with a static string
async fn root() -> &'static str {
    "Hello, World!"
}

actix-web 的 example 看 github


Ext Link: https://github.com/anycms/anycms-spa

评论区

写评论

还没有评论

1 共 0 条评论, 1 页