1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-22 20:52:27 +00:00
DEPRECATED code of gotham-restful, mirror from deprecated GitLab repository The NON-DEPRECATED source code lives on GitHub
Find a file
2019-10-20 16:56:50 +02:00
example add search method 2019-10-13 17:43:42 +02:00
gotham_restful dual-license under EPL-2.0 and Apache-2.0 2019-10-20 16:56:50 +02:00
gotham_restful_derive dual-license under EPL-2.0 and Apache-2.0 2019-10-20 16:56:50 +02:00
.codecov.yml attempt to remove codecov from pipelines 2019-10-14 15:12:24 +02:00
.gitignore remove Cargo.lock 2019-10-13 14:36:36 +02:00
.gitlab-ci.yml manually set path to codecov yml 2019-10-14 16:07:19 +02:00
Cargo.toml add proc macro derive for openapitype 2019-10-02 10:59:25 +02:00
LICENSE-Apache dual-license under EPL-2.0 and Apache-2.0 2019-10-20 16:56:50 +02:00
LICENSE-EPL dual-license under EPL-2.0 and Apache-2.0 2019-10-20 16:56:50 +02:00
LICENSE.md dual-license under EPL-2.0 and Apache-2.0 2019-10-20 16:56:50 +02:00
README.md dual-license under EPL-2.0 and Apache-2.0 2019-10-20 16:56:50 +02:00
README.tpl add codecov badge 2019-10-14 02:50:43 +02:00

gotham_restful

Build Status Coverage Status

This crate is an extension to the popular gotham web framework 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 and 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:

[dependencies]
gotham_restful = "0.0.1"

A basic server with only one resource, handling a simple GET request, could look like this:

/// Our RESTful Resource.
#[derive(Resource)]
#[rest_resource(read_all)]
struct UsersResource;

/// Our return type.
#[derive(Deserialize, Serialize)]
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:

#[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)
}

Look at the example for more methods and usage with the openapi feature.

License

Licensed under your option of: