#request-response #routes #actix-web #modular #path #guard #response-status

lest

一种模块化的Web服务器实现方法。基于actix-web。

4个版本

新版本 0.2.0 2024年8月20日
0.1.2 2024年8月18日
0.1.1 2024年8月18日
0.1.0 2024年8月18日

288HTTP服务器 中排名

Download history 235/week @ 2024-08-13

235 每月下载次数

WTFPL 许可证

86KB
2K SLoC

lest

此crate提供了一个对 actix-web 的模块化抽象,允许高级守卫、请求/响应验证和独立的模块。这些概念在模块化和请求/响应验证方面与NestJS和ElysiaJS类似。它旨在易于使用,并使用我的其他库 libcoerced 提供高级验证。

示例

基本用法

这提供了一个根路由,并服务于除路径 routes/** 之外的所有目录。

use lest::{models::{guard::Guard, app::App, request::Request, response::{Response, Status}, route::{Route, RouteResponse}, served::Served}, types::method::Method, leaked};

fn route_handler(_req: Request) -> RouteResponse {
    Box::pin(async {
        let mut response = Response::new(Status::from_u16(302));
        response.redirect("/index.html".to_string());
        Ok(response)
    })
}

let root_route = Route::new(
    Method::Get, // Require GET method
    route_handler, /// Route handler
    None, // NO request verification
    None, // NO response verification
    Guard::Combine(Box::from([
        Guard::Host("localhost"),
        Guard::Port(8080),
    ])) // Enforce host and port
);

let mut app = App::new(("/".to_string(), Some(root_route)));

app.dir(Served {
    root: "/".to_string(),
    path: "static".to_string(),
    index_html: true,
}, Box::from([
    "routes/**",
]));

leaked(app)
    .serve(
        8080,
        Box::pin(async {
            println!("Server started at https://127.0.0.1:8080");
        })
    ).await;

特性

Lest旨在提供您所需的一切,无需中间件或额外依赖。

  • 守卫:强制执行主机、端口、方法、头、查询参数等。
  • 请求/响应验证:验证请求和响应体。
  • 模块化:将不同的路由分离到不同的模块中。
  • 简单易用:设计得易于使用和理解。
  • 重写:将请求路径重写到不同的路径。
  • 服务:服务目录和文件,并排除受限制的路径。
  • [即将推出] 缓存:缓存响应和请求。
  • [即将推出] 自定义错误:自定义错误处理。
  • [即将推出] 速率限制:限制请求速率。

依赖项

~21–33MB
~572K SLoC