1 год назад
История
README.md
cryo-http
This is an experimental library. Use it at your own risk.
Usage example
use cryo_http::{HttpServer, Service, Request, Response, StatusCode, Headers, HttpVersion};
#[derive(serde::Serialize)]
struct Answer {
success: bool,
whoami: String,
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
HttpServer::new(Service::new(service_fn), "127.0.0.1:7878").await.unwrap().run().await
}
async fn service_fn(request: Request) -> Response {
match request.path() {
"/" => index(request).await,
"/test" => test_func(request).await,
_ => page_not_found(request).await
}
}
async fn index(req: Request) -> Response {
dbg!(req);
Response::builder().just_body(String::from("This is index.")).http_version(HttpVersion::Http11).status_code(StatusCode::OK).build()
}
async fn test_func(req: Request) -> Response {
dbg!(req);
Response::builder().json(Answer { success: true, whoami: "I'm server!".into() }).http_version(HttpVersion::Http11).status_code(StatusCode::OK).build()
}
async fn page_not_found(req: Request) -> Response {
dbg!(req);
Response::builder().html(r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 Not Found</title>
</head>
<body>
<h1>Привет, мир!</h1>
<p>I can't find this page.</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>
"#.into()).http_version(HttpVersion::Http11).status_code(StatusCode::OK).build()
}
Описание
Простая библиотека для создания HTTP серверов на Rust.
Конвейеры
0 успешных
0 с ошибкой