#http-response #built #js #scratch #express #query

bin+lib choki

从零开始构建的简单 HTTP 服务器。不适用于生产环境,但可以制作包含一些查询功能的简单网站。(每秒可以承受大量请求)

10 个稳定版本

新版本 1.0.9 2024 年 8 月 20 日
1.0.8 2024 年 8 月 17 日
1.0.5 2024 年 6 月 6 日
1.0.4 2024 年 5 月 28 日

#217 in HTTP 服务器

Download history 132/week @ 2024-05-17 265/week @ 2024-05-24 142/week @ 2024-05-31 60/week @ 2024-06-07 12/week @ 2024-06-14 2/week @ 2024-06-21 3/week @ 2024-06-28 11/week @ 2024-07-05 23/week @ 2024-07-26 3/week @ 2024-08-02 462/week @ 2024-08-16

每月 488 次下载

MIT/Apache

31KB
625

从零开始构建的简单 HTTP 服务器库。
仅使用 threadpool crate 和 num_cpus. (当然还有内置的 std)
深受 express.js 启发

https://express.js.cn/

📂・安装

cargo add choki

或者在 Cargo.toml 中添加它

choki = "1.0.6"

💡・功能

  • 创建 GET 和 POST 端点,并像在 express.js 中一样使用它们
  • 创建静态端点

在文件中声明它们

use choki::structs::{Request, Response};
use choki::Server;

Server 类创建一个对象

  let mut server: Server<u8> = Server::new(Some(1024),None); // you can also type None if you dont want any restrictions

括号中的数字是请求的最大内容长度,简单来说就是客户端发送请求的最大大小。

创建 GET 端点

server.get("/".to_string(), |req: Request, mut res: Response, public_var: Option<u8>| {
    res.send_string("HI");
}).unwrap();

创建 POST 端点

server.post("/".to_string(), |req: Request, mut res: Response, public_var: Option<u8>| {
    res.send_string("HI");
}).unwrap();

创建 STATIC 端点

 server.new_static("/images".to_string(), "./tests/images".to_string()).unwrap(); // The first one is the path in the browser for example: example.com/images and the second one is the exposed path from the computer(local)

创建带参数的 Get/Set 端点

自 1.0.8 版起,choki 支持参数

 server.post("/search/[id]".to_string(), |req: Request, mut res: Response, public_var: Option<u8>| {
    println!("{}", req.params.get("id").unwrap()); // if i make request to /search/pizza this will print pizza
    res.send_string("HI");
}).unwrap();

响应

所以它们是四个简单的函数

res.send_bytes(&mut self, data: &[u8], content_type: Option<ContentType>) // sends raw bytes with content type you provide (you can provide ContentType::None and let the browser decide)
res.send_string(&mut self, data: &str) // sends string as response
res.send_json(&mut self, data: &str) // sends json as response
res.send_code(&mut self, code: usize) // sends a HTTP response code (404,200...)

自 1.0.3 版起,您可以设置或删除 cookie,当然可以读取它们。

pub struct Cookie {
    pub name: String,
    pub value: String,
    pub path: String,
    pub expires: String,
}

您可以使用 req.cookies 读取 cookie(存储为 vec)

您可以使用以下方法设置/删除它们

res.set_cookie(cookie: &Cookie);
res.delete_cookie(name: &str);

自 1.0.6 版起,您可以设置或删除标头,当然可以读取它们。

pub struct Header {
    pub name: String,
    pub value: String,
}

您可以使用以下方法设置/删除它们

res.set_header(header: &Header);
res.delete_cookie(name: &str);

请求

当您创建端点时,您有请求和响应。
请求包含有关请求的信息。

pub struct Request {
    pub query: HashMap<String, String>, // for example in the url www.example.com/?name=Kartof the query will be ["name" => "Kartof"] as hashmap
    pub user_agent: Option<String>, // this is the user agent from which the user accesses the website
    pub content_length: usize, // the length of the request (they are no implementations for multy form thingy so its not so useful)
}

最后

您需要让服务器实际“监听”请求,所以使用此方法

server.listen(port: u32, address: Option<String>) -> Result<(), HttpServerError>

最后,因为您想保持主线程运行,否则服务器将在代码运行后立即关闭。

请将此添加到文件末尾

  Server::<i32>::lock();

此外,在 src 文件夹中有一个 main.rs 文件,可以作为示例使用。

就是这样,享受使用它,并记住这不是生产就绪!!!

依赖关系

~120KB