https://github.com/routerify/routerify-json-response
A Routerify utility library to generate JSON response
https://github.com/routerify/routerify-json-response
hyper-rs json response routerify rust
Last synced: 5 months ago
JSON representation
A Routerify utility library to generate JSON response
- Host: GitHub
- URL: https://github.com/routerify/routerify-json-response
- Owner: routerify
- License: mit
- Created: 2020-05-02T14:18:13.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-02T12:57:08.000Z (over 3 years ago)
- Last Synced: 2024-12-29T03:45:41.174Z (6 months ago)
- Topics: hyper-rs, json, response, routerify, rust
- Language: Rust
- Homepage:
- Size: 44.9 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/routerify/routerify-json-response/actions)
[](https://crates.io/crates/routerify-json-response)
[](https://docs.rs/routerify-json-response)
[](./LICENSE)# routerify-json-response
A [`Routerify`](https://github.com/routerify/routerify) utility library to generate JSON response.
In `Success` case, It generates JSON response in the following format:
```json
{
"status": "success",
"code": "",
"data": ""
}
```In `Failed` case, It generates JSON response in the following format:
```json
{
"status": "failed",
"code": "",
"message": ""
}
```[Docs](https://docs.rs/routerify-json-response)
## Install
Add this to your `Cargo.toml`:```toml
[dependencies]
routerify = "3"
routerify-json-response = "3"
```## Example
```rust
use hyper::{Body, Request, Response, Server, StatusCode};
// Import required routerify_json_response methods.
use routerify_json_response::{json_failed_resp_with_message, json_success_resp};
use routerify::{Router, RouterService};
use std::net::SocketAddr;async fn list_users_handler(_: Request) -> Result, routerify_json_response::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) -> Result, routerify_json_response::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 {
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).unwrap();// 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);
}
}
```## Contributing
Your PRs and suggestions are always welcome.