|
5 | 5 |
|
6 | 6 | # json-response
|
7 | 7 |
|
8 |
| -A utility library to send JSON response. |
| 8 | +A utility library to send JSON response for [`Routerify`](https://github.com/routerify/routerify) and the Rust HTTP library [`hyper.rs`](https://hyper.rs/) apps. |
| 9 | + |
| 10 | +In `Success` case, It generates JSON response in the following format: |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "status": "success", |
| 15 | + "code": "<status_code>", |
| 16 | + "data": "<data>" |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +In `Failed` case, It generates JSON response in the following format: |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "status": "failed", |
| 25 | + "code": "<status_code>", |
| 26 | + "message": "<error_message>" |
| 27 | +} |
| 28 | +``` |
9 | 29 |
|
10 | 30 | [Docs](https://docs.rs/json-response)
|
11 | 31 |
|
12 |
| -## Usage |
| 32 | +## Install |
13 | 33 |
|
14 |
| -First add this to your `Cargo.toml`: |
| 34 | +Add this to your `Cargo.toml`: |
15 | 35 |
|
16 | 36 | ```toml
|
17 | 37 | [dependencies]
|
| 38 | +routerify = "1.0" |
18 | 39 | json-response = "1.0"
|
19 | 40 | ```
|
20 | 41 |
|
21 |
| -An example: |
| 42 | +## Example |
| 43 | + |
22 | 44 | ```rust
|
23 |
| -use json_response; |
| 45 | +use hyper::{Body, Request, Response, Server, StatusCode}; |
| 46 | +// Import required json_response methods. |
| 47 | +use json_response::{json_failed_resp_with_message, json_success_resp}; |
| 48 | +use routerify::{Router, RouterService}; |
| 49 | +use std::net::SocketAddr; |
| 50 | + |
| 51 | +async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> { |
| 52 | + // Fetch response data from somewhere. |
| 53 | + let users = ["Alice", "John"]; |
| 54 | + |
| 55 | + // Generate a success JSON response with the data in the following format: |
| 56 | + // { "status": "success", code: 200, data: ["Alice", "John"] } |
| 57 | + json_success_resp(&users) |
| 58 | +} |
| 59 | + |
| 60 | +async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> { |
| 61 | + // Generate a failed JSON response in the following format: |
| 62 | + // { "status": "failed", code: 500, data: "Internal Server Error: Couldn't fetch book list from database" } |
| 63 | + json_failed_resp_with_message( |
| 64 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 65 | + "Couldn't fetch book list from database", |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +// Create a router. |
| 70 | +fn router() -> Router<Body, routerify::Error> { |
| 71 | + Router::builder() |
| 72 | + // Attach the handlers. |
| 73 | + .get("/users", list_users_handler) |
| 74 | + .get("/books", list_books_handler) |
| 75 | + .build() |
| 76 | + .unwrap() |
| 77 | +} |
| 78 | + |
| 79 | +#[tokio::main] |
| 80 | +async fn main() { |
| 81 | + let router = router(); |
| 82 | + |
| 83 | + // Create a Service from the router above to handle incoming requests. |
| 84 | + let service = RouterService::new(router); |
| 85 | + |
| 86 | + // The address on which the server will be listening. |
| 87 | + let addr = SocketAddr::from(([127, 0, 0, 1], 3001)); |
| 88 | + |
| 89 | + // Create a server by passing the created service to `.serve` method. |
| 90 | + let server = Server::bind(&addr).serve(service); |
24 | 91 |
|
25 |
| -fn main() { |
26 |
| - println!("{}", json_response::add(2, 3)); |
| 92 | + println!("App is running on: {}", addr); |
| 93 | + if let Err(err) = server.await { |
| 94 | + eprintln!("Server error: {}", err); |
| 95 | + } |
27 | 96 | }
|
28 | 97 | ```
|
29 | 98 |
|
|
0 commit comments