1 个不稳定版本
0.1.0 | 2021 年 3 月 30 日 |
---|
#23 in #spa
14KB
182 行
spa-server
spa-server 是一个用于嵌入所有 SPA Web 应用程序文件并将其作为单个二进制可执行文件发布的库。它基于 actix-web 和 rust-embed
通过 proc macro 方式工作,示例
#[derive(SPAServer)]
#[spa_server(
static_files = "ui/dist/ui", # SPA dist dir, all the files will be packad into binary
apis( # define apis that SPA application interacting with
api( # define a api group
prefix = "/api/v1", # prefix for this api group
v1::foo, # api function
v1::bar,
),
api(
prefix = "/api/v2",
v2::foo,
v2::bar,
),
api(test), # api without prefix
),
cors, # enable cors permissive for debug
identity(name = "a", age = 30) # identity support, cookie name and age in minutes
)]
pub struct Server {
data: String,
num: u32,
}
mod v1 {
use spa_server::re_export::*; # use actix-web symbol directly
#[get("foo")] # match route /api/v1/foo
async fn foo(s: web::Data<Server>) -> Result<HttpResponse> {
let data = s.data; # web context stored in struct
let num = s.num;
Ok(HttpResponse::Ok().finish())
}
}
#[spa_server::main] # replace actix_web::main with spa_server::main
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Server {
data: String::new(), num: 1234
}
.run(8080) # listen at 0.0.0.0::8080
.await?;
Ok(())
}
访问 https://127.0.0.1:8080 将显示 SPA index.html 页面
依赖
~30–41MB
~667K SLoC