Skip to content

Instantly share code, notes, and snippets.

@bnmrrs
Created September 6, 2015 19:18
Show Gist options
  • Save bnmrrs/debd1eb838a64bb09c2d to your computer and use it in GitHub Desktop.
Save bnmrrs/debd1eb838a64bb09c2d to your computer and use it in GitHub Desktop.
extern crate rustc_serialize;
#[macro_use] extern crate nickel;
use nickel::{Nickel, HttpRouter, JsonBody};
#[derive(RustcDecodable, RustcEncodable)]
pub struct Messages {
messages: Vec<Message>
}
#[derive(RustcDecodable, RustcEncodable)]
pub struct Message {
body: String,
from: String,
id: String,
type: String
}
pub struct Response {
body: String,
to: String,
type: String
}
fn handle_message(message: &Message) {
let response = Response {
body: "Hello there",
to: message.from,
};
}
fn main() {
let mut server = Nickel::new();
server.post("/message", middleware! { |request, response|
let messages = request.json_as::<Messages>().unwrap();
for message in messages.messages.iter() {
handle_message(message);
}
format!("Hello {}", messages.messages.len());
});
server.listen("127.0.0.1:6767");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment