1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
//! 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": "<status_code>", //! "data": "<data>" //! } //! ``` //! //! In `Failed` case, It generates JSON response in the following format: //! //! ```json //! { //! "status": "failed", //! "code": "<status_code>", //! "message": "<error_message>" //! } //! ``` //! //! # Examples //! //! ```no_run //! 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<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).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); //! } //! } //! ``` pub use failed_resp::{json_failed_resp, json_failed_resp_with_message}; pub use success_resp::{json_success_resp, json_success_resp_with_code}; mod failed_resp; mod gen_resp; mod success_resp;