Feather là một framework web nhẹ nhàng, được thiết kế để mang lại trải nghiệm lập trình (DX) tốt nhất cho các nhà phát triển Rust. Với kiến trúc middleware-first, quản lý state đơn giản thông qua Context API, cùng nhiều công cụ mạnh mẽ đi kèm, Feather hứa hẹn trở thành lựa chọn lý tưởng cho những ai muốn bắt đầu với Rust mà không cần hiểu sâu về async.
Mục lục
Điểm Nổi Bật của Feather
Kiến Trúc Middleware-First
Mọi thứ trong Feather đều là middleware, từ xử lý route đến xác thực và logging. Điều này giúp code trở nên sạch sẽ và dễ tái sử dụng.
Quản Lý State Dễ Dàng với Context API
Feather cung cấp Context API, giúp quản lý trạng thái ứng dụng một cách đơn giản mà không cần đến các extractors hay macros phức tạp.
Trải Nghiệm Lập Trình Ưu Việt
API của Feather được thiết kế tối giản, dễ đọc và dễ sử dụng, giúp nhà phát triển tập trung vào logic thay vì cú pháp.
Công Cụ Mạnh Mẽ Đi Kèm
Feather-CLI giúp tạo API và web server một cách nhanh chóng, tiết kiệm thời gian và công sức.
Bắt Đầu với Feather
Để tích hợp Feather vào dự án của bạn, chỉ cần thêm nó vào file Cargo.toml
:
[dependencies]
feather = "0.3.1"
Ví dụ đơn giản:
use feather::{App, AppContext, MiddlewareResult, Request, Response};
fn main() {
let mut app = App::new();
app.get("/", |_req: &mut Request, res: &mut Response, _ctx: &mut AppContext| {
res.send_text("Hello, world!");
MiddlewareResult::Next
});
app.listen("127.0.0.1:3000");
}
Middleware trong Feather
Middleware là trái tim của Feather. Bạn có thể viết middleware dưới dạng closure, struct hoặc kết hợp chúng lại với nhau:
use feather::{App, AppContext, Request, Response};
use feather::middleware::builtins;
use feather::middleware::{Middleware, MiddlewareResult};
struct Custom;
impl Middleware for Custom {
fn handle(&self, request: &mut Request, _response: &mut Response, _ctx: &mut AppContext) -> MiddlewareResult {
println!("Middleware tùy chỉnh (struct Custom) đang chạy!");
println!("Path của request: {:?}", request.uri);
MiddlewareResult::Next
}
}
fn main() {
let mut app = App::new();
app.use_middleware(builtins::Logger);
app.use_middleware(Custom);
app.use_middleware(|_req: &mut Request, _res: &mut Response, _ctx: &mut AppContext| {
println!("Middleware tùy chỉnh (closure) đang chạy!");
MiddlewareResult::Next
});
app.get("/", |_req: &mut Request, res: &mut Response, _ctx: &mut AppContext| {
res.send_text("Hello, world!");
MiddlewareResult::Next
});
app.listen("127.0.0.1:3000");
}
Quản Lý State với Context API
Context API giúp bạn quản lý trạng thái toàn cục một cách dễ dàng:
use feather::{App, AppContext, MiddlewareResult, Response, Request};
struct Counter {
pub count: i32,
}
fn main() {
let mut app = App::new();
let counter = Counter { count: 0 };
app.context().set_state(counter);
app.get("/", move |_req: &mut Request, res: &mut Response, ctx: &mut AppContext| {
let counter: &mut Counter = ctx.get_mut_state::<Counter>().unwrap();
counter.count += 1;
res.send_text(format!("Counted! {}", counter.count));
MiddlewareResult::Next
});
app.get("/count", move |_req: &mut Request, res: &mut Response, ctx: &mut AppContext| {
let counter = ctx.get_state::<Counter>().unwrap();
res.send_text(counter.count.to_string());
MiddlewareResult::Next
});
app.listen("127.0.0.1:5050");
}
Xác Thực JWT Tích Hợp
Feather cung cấp module JWT tích hợp, kích hoạt thông qua feature jwt
:
[dependencies]
feather = { version = "0.3.1", features = ["jwt"] }
use feather::jwt::{generate_jwt, with_jwt_auth};
use feather::{App, AppContext};
fn main() {
let mut app = App::new();
app.get("/auth", with_jwt_auth("secretcode", |_req, res, _ctx, claim| {
println!("Claim: {:?}", claim);
res.send_text("Hello, JWT!");
feather::MiddlewareResult::Next
}));
app.listen("127.0.0.1:8080")
}
Mục Tiêu của Feather
- Là framework Rust đơn giản nhất để bắt đầu.
- Modular và dễ dàng mở rộng.
- Tập trung vào trải nghiệm lập trình mà không đánh đổi tính an toàn và hiệu năng của Rust.
Đóng Góp
Các PR luôn được chào đón! Nếu bạn có ý tưởng hoặc phát hiện lỗi, hãy mở issue hoặc gửi pull request.
Feather hiện đã có trên GitHub với hơn 492 sao và 15 fork. Đừng quên ⭐ để ủng hộ dự án và chia sẻ với cộng đồng!