#response #json #routerify #hyper #generate-json #response-body #request-response

json-response

一个用于为Routerify和hyper.rs应用程序发送JSON响应的实用库

1个稳定版本

1.0.0 2020年5月2日

#8 in #routerify

MIT许可证

15KB
103

Github Actions Status crates.io Documentation MIT

json-response

一个用于为Routerify和Rust HTTP库hyper.rs应用程序发送JSON响应的实用库。

Success情况下,它生成以下格式的JSON响应

{
    "status": "success",
    "code": "<status_code>",
    "data": "<data>"
}

Failed情况下,它生成以下格式的JSON响应

{
    "status": "failed",
    "code": "<status_code>",
    "message": "<error_message>"
}

文档

安装

将其添加到您的Cargo.toml

[dependencies]
routerify = "1.0"
json-response = "1.0"

示例

use hyper::{Body, Request, Response, Server, StatusCode};
// Import required json_response methods.
use json_response::{json_failed_resp_with_message, json_success_resp};
use routerify::{Router, RouterService};
use std::net::SocketAddr;

async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
    // Fetch response data from somewhere.
    let users = ["Alice", "John"];

    // Generate a success JSON response with the data in the following format:
    // { "status": "success", code: 200, data: ["Alice", "John"] }
    json_success_resp(&users)
}

async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
    // Generate a failed JSON response in the following format:
    // { "status": "failed", code: 500, data: "Internal Server Error: Couldn't fetch book list from database" }
    json_failed_resp_with_message(
        StatusCode::INTERNAL_SERVER_ERROR,
        "Couldn't fetch book list from database",
    )
}

// Create a router.
fn router() -> Router<Body, routerify::Error> {
    Router::builder()
        // Attach the handlers.
        .get("/users", list_users_handler)
        .get("/books", list_books_handler)
        .build()
        .unwrap()
}

#[tokio::main]
async fn main() {
    let router = router();

    // Create a Service from the router above to handle incoming requests.
    let service = RouterService::new(router);

    // The address on which the server will be listening.
    let addr = SocketAddr::from(([127, 0, 0, 1], 3001));

    // Create a server by passing the created service to `.serve` method.
    let server = Server::bind(&addr).serve(service);

    println!("App is running on: {}", addr);
    if let Err(err) = server.await {
        eprintln!("Server error: {}", err);
    }
}

贡献

您的PR和建议始终受到欢迎。

依赖项

~13MB
~225K SLoC