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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/*! This crate is an extension to the popular [gotham web framework][gotham] for Rust. The idea is to have several RESTful resources that can be added to the gotham router. This crate will take care of everything else, like parsing path/query parameters, request bodies, and writing response bodies, relying on [`serde`][serde] and [`serde_json`][serde_json] for (de)serializing. If you enable the `openapi` feature, you can also generate an OpenAPI Specification from your RESTful resources. # Usage To use this crate, add the following to your `Cargo.toml`: ```toml [dependencies] gotham_restful = "0.0.1" ``` A basic server with only one resource, handling a simple `GET` request, could look like this: ```rust,no_run # #[macro_use] extern crate gotham_restful_derive; # use gotham::{router::builder::*, state::State}; # use gotham_restful::{DrawResources, Resource, Success}; # use serde::{Deserialize, Serialize}; /// Our RESTful Resource. #[derive(Resource)] #[rest_resource(read_all)] struct UsersResource; /// Our return type. #[derive(Deserialize, Serialize)] # #[derive(OpenapiType)] struct User { id: i64, username: String, email: String } /// Our handler method. #[rest_read_all(UsersResource)] fn read_all(_state: &mut State) -> Success<Vec<User>> { vec![User { id: 1, username: "h4ck3r".to_string(), email: "h4ck3r@example.org".to_string() }].into() } /// Our main method. fn main() { gotham::start("127.0.0.1:8080", build_simple_router(|route| { route.resource::<UsersResource, _>("users"); })); } ``` Uploads and Downloads can also be handled, but you need to specify the mime type manually: ```rust,no_run # #[macro_use] extern crate gotham_restful_derive; # use gotham::{router::builder::*, state::State}; # use gotham_restful::{DrawResources, Raw, Resource, Success}; # use serde::{Deserialize, Serialize}; #[derive(Resource)] #[rest_resource(create)] struct ImageResource; #[derive(FromBody, RequestBody)] #[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)] struct RawImage(Vec<u8>); #[rest_create(ImageResource)] fn create(_state : &mut State, body : RawImage) -> Raw<Vec<u8>> { Raw::new(body.0, mime::APPLICATION_OCTET_STREAM) } # fn main() { # gotham::start("127.0.0.1:8080", build_simple_router(|route| { # route.resource::<ImageResource, _>("image"); # })); # } ``` Look at the [example] for more methods and usage with the `openapi` feature. # Known Issues These are currently known major issues. For a complete list please see [the issue tracker](https://gitlab.com/msrd0/gotham-restful/issues). If you encounter any issues that aren't yet reported, please report them [here](https://gitlab.com/msrd0/gotham-restful/issues/new). - Enabling the `openapi` feature might break code ([#4](https://gitlab.com/msrd0/gotham-restful/issues/4)) - For `chrono`'s `DateTime` types, the format is `date-time` instead of `datetime` ([openapiv3#14](https://github.com/glademiller/openapiv3/pull/14)) # License Licensed under your option of: - [Apache License Version 2.0](https://gitlab.com/msrd0/gotham-restful/blob/master/LICENSE-Apache) - [Eclipse Public License Version 2.0](https://gitlab.com/msrd0/gotham-restful/blob/master/LICENSE-EPL) [example]: https://gitlab.com/msrd0/gotham-restful/tree/master/example [gotham]: https://gotham.rs/ [serde]: https://github.com/serde-rs/serde#serde----- [serde_json]: https://github.com/serde-rs/json#serde-json---- */ // weird proc macro issue extern crate self as gotham_restful; #[macro_use] extern crate gotham_derive; #[macro_use] extern crate serde; #[doc(no_inline)] pub use hyper::{Chunk, StatusCode}; #[doc(no_inline)] pub use mime::Mime; pub use gotham_restful_derive::*; /// Not public API #[doc(hidden)] pub mod export { #[cfg(feature = "openapi")] pub use indexmap::IndexMap; #[cfg(feature = "openapi")] pub use openapiv3 as openapi; } #[cfg(feature = "openapi")] mod openapi; #[cfg(feature = "openapi")] pub use openapi::{ router::{GetOpenapi, OpenapiRouter}, types::{OpenapiSchema, OpenapiType} }; mod resource; pub use resource::{ Resource, ResourceReadAll, ResourceRead, ResourceSearch, ResourceCreate, ResourceUpdateAll, ResourceUpdate, ResourceDeleteAll, ResourceDelete }; mod result; pub use result::{ NoContent, Raw, ResourceResult, Response, Success }; mod routing; pub use routing::{DrawResources, DrawResourceRoutes}; #[cfg(feature = "openapi")] pub use routing::WithOpenapi; mod types; pub use types::*;