2019-10-14 00:59:02 +02:00
|
|
|
/*!
|
|
|
|
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
|
|
|
|
|
|
|
|
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| {
|
2020-04-05 22:18:31 +02:00
|
|
|
route.resource::<UsersResource>("users");
|
2019-10-14 00:59:02 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-10-20 14:49:53 +00:00
|
|
|
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| {
|
2020-04-05 22:18:31 +02:00
|
|
|
# route.resource::<ImageResource>("image");
|
2019-10-20 14:49:53 +00:00
|
|
|
# }));
|
|
|
|
# }
|
|
|
|
```
|
|
|
|
|
2019-10-14 00:59:02 +02:00
|
|
|
Look at the [example] for more methods and usage with the `openapi` feature.
|
|
|
|
|
2019-10-20 17:18:51 +02:00
|
|
|
# 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))
|
2019-10-20 18:23:36 +02:00
|
|
|
- For `chrono`'s `DateTime` types, the format is `date-time` instead of `datetime` ([openapiv3#14](https://github.com/glademiller/openapiv3/pull/14))
|
2019-10-20 17:18:51 +02:00
|
|
|
|
2019-10-14 00:59:02 +02:00
|
|
|
# License
|
|
|
|
|
2019-10-20 16:55:45 +02:00
|
|
|
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)
|
2019-10-14 00:59:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
[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----
|
|
|
|
*/
|
|
|
|
|
2019-10-14 02:37:50 +02:00
|
|
|
// weird proc macro issue
|
|
|
|
extern crate self as gotham_restful;
|
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
#[macro_use] extern crate gotham_derive;
|
|
|
|
#[macro_use] extern crate serde;
|
2019-09-26 17:24:40 +02:00
|
|
|
|
2019-10-20 14:49:53 +00:00
|
|
|
#[doc(no_inline)]
|
2020-04-14 17:44:07 +02:00
|
|
|
pub use gotham::hyper::{header::HeaderName, StatusCode};
|
2019-10-20 14:49:53 +00:00
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use mime::Mime;
|
2019-09-26 17:24:40 +02:00
|
|
|
|
2019-10-06 15:03:30 +02:00
|
|
|
pub use gotham_restful_derive::*;
|
2019-10-13 23:19:34 +02:00
|
|
|
|
2019-10-06 15:03:30 +02:00
|
|
|
/// Not public API
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod export
|
|
|
|
{
|
2020-04-14 17:44:07 +02:00
|
|
|
pub use gotham::{
|
|
|
|
hyper::body::Bytes,
|
|
|
|
state::{FromState, State}
|
|
|
|
};
|
2020-01-14 23:16:31 +01:00
|
|
|
|
2020-01-14 03:27:49 +01:00
|
|
|
#[cfg(feature = "database")]
|
|
|
|
pub use gotham_middleware_diesel::Repo;
|
2020-01-14 23:16:31 +01:00
|
|
|
|
2019-10-06 15:03:30 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
pub use indexmap::IndexMap;
|
|
|
|
#[cfg(feature = "openapi")]
|
2019-10-13 23:19:34 +02:00
|
|
|
pub use openapiv3 as openapi;
|
2019-10-06 15:03:30 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 16:53:02 +00:00
|
|
|
#[cfg(feature = "auth")]
|
|
|
|
mod auth;
|
|
|
|
#[cfg(feature = "auth")]
|
|
|
|
pub use auth::{
|
|
|
|
AuthHandler,
|
|
|
|
AuthMiddleware,
|
|
|
|
AuthSource,
|
|
|
|
AuthStatus,
|
|
|
|
AuthValidation,
|
|
|
|
StaticAuthHandler
|
|
|
|
};
|
|
|
|
|
2019-09-29 21:15:22 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
2019-10-14 00:59:02 +02:00
|
|
|
mod openapi;
|
2019-09-30 18:18:10 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
2019-09-30 20:58:15 +02:00
|
|
|
pub use openapi::{
|
|
|
|
router::{GetOpenapi, OpenapiRouter},
|
2019-10-01 15:33:05 +02:00
|
|
|
types::{OpenapiSchema, OpenapiType}
|
2019-09-30 20:58:15 +02:00
|
|
|
};
|
2019-09-29 21:15:22 +02:00
|
|
|
|
2019-09-26 17:24:40 +02:00
|
|
|
mod resource;
|
|
|
|
pub use resource::{
|
|
|
|
Resource,
|
2020-04-06 16:20:08 +00:00
|
|
|
ResourceMethod,
|
2019-09-28 13:38:08 +02:00
|
|
|
ResourceReadAll,
|
|
|
|
ResourceRead,
|
2019-10-13 17:43:42 +02:00
|
|
|
ResourceSearch,
|
2019-09-28 13:38:08 +02:00
|
|
|
ResourceCreate,
|
|
|
|
ResourceUpdateAll,
|
2019-09-29 19:19:38 +02:00
|
|
|
ResourceUpdate,
|
|
|
|
ResourceDeleteAll,
|
|
|
|
ResourceDelete
|
2019-09-26 17:24:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
mod result;
|
2019-10-05 14:50:05 +02:00
|
|
|
pub use result::{
|
2020-01-25 15:59:37 +01:00
|
|
|
AuthResult,
|
|
|
|
AuthResult::AuthErr,
|
2019-10-05 14:50:05 +02:00
|
|
|
NoContent,
|
2019-10-20 14:49:53 +00:00
|
|
|
Raw,
|
2019-10-05 14:50:05 +02:00
|
|
|
ResourceResult,
|
2019-10-20 14:49:53 +00:00
|
|
|
Response,
|
2019-10-05 14:50:05 +02:00
|
|
|
Success
|
|
|
|
};
|
2019-09-26 17:24:40 +02:00
|
|
|
|
|
|
|
mod routing;
|
2019-09-26 17:42:28 +02:00
|
|
|
pub use routing::{DrawResources, DrawResourceRoutes};
|
2019-09-30 18:18:10 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
pub use routing::WithOpenapi;
|
2019-09-30 20:58:15 +02:00
|
|
|
|
2019-10-14 00:59:02 +02:00
|
|
|
mod types;
|
2019-10-20 14:49:53 +00:00
|
|
|
pub use types::*;
|