4 个版本
0.1.3 | 2024 年 6 月 24 日 |
---|---|
0.1.2 | 2024 年 6 月 24 日 |
0.1.1 | 2024 年 6 月 24 日 |
0.1.0 | 2024 年 6 月 11 日 |
#1729 在 Web 编程
8KB
159 行
httplite
使用 Rust 编写的超轻量级 HTTP 服务器,旨在模仿 Go 中 "NET/HTTP" 模块的功能。目前处于早期访问阶段,正在编写文档。
安装 httplite
在项目的 Cargo.toml 中包含以下内容。 [dependencies] httplite = "0.1.3"
基本示例
Hello World 示例
use httplite::{Httplite, ResponseWriter, Request};
fn main() {
println!("Server is running at https://127.0.0.1:8080");
let port = Httplite::new(":8080");
port.add_route("/hello", hello_server);
port.listen().unwrap();
}
fn hello_server(mut w: ResponseWriter, r: Request) {
let response_text = format!("Hello, {}", r.url().trim_start_matches("/hello/"));
w.print_text(&response_text).unwrap();
}
托管 JSON 示例
use httplite::{Httplite, ResponseWriter, Request};
use std::collections::HashMap;
fn main() {
println!("Server is running at https://127.0.0.1:8080");
let port = Httplite::new(":8080");
port.add_route("/json", json_server);
port.listen().unwrap();
}
fn json_server(mut w: ResponseWriter, _r: Request) {
let mut map = HashMap::new();
map.insert("JSON Thing", "This is a thing");
map.insert("Another JSON Thing", "This is another thing");
w.print_hashmap_to_json(&map).unwrap();
}
最新重要更新
0.1.3 添加了在端点上托管序列化 JSON 映射的能力。