Rocket/examples/msgpack/src/main.rs

28 lines
628 B
Rust
Raw Normal View History

#[macro_use] extern crate rocket;
2017-02-08 03:40:14 +00:00
#[macro_use] extern crate serde_derive;
#[cfg(test)] mod tests;
2018-10-07 00:24:11 +00:00
use rocket_contrib::msgpack::MsgPack;
2017-02-08 03:40:14 +00:00
#[derive(Serialize, Deserialize)]
struct Message<'r> {
2017-02-08 03:40:14 +00:00
id: usize,
contents: &'r str
2017-02-08 03:40:14 +00:00
}
#[get("/<id>", format = "msgpack")]
fn get(id: usize) -> MsgPack<Message<'static>> {
MsgPack(Message { id: id, contents: "Hello, world!", })
2017-02-08 03:40:14 +00:00
}
#[post("/", data = "<data>", format = "msgpack")]
2019-06-13 02:41:29 +00:00
fn create(data: MsgPack<Message<'_>>) -> String {
data.contents.to_string()
2017-02-08 03:40:14 +00:00
}
#[rocket::launch]
2017-02-08 03:40:14 +00:00
fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/message", routes![get, create])
2017-02-08 03:40:14 +00:00
}