Created
September 6, 2015 19:18
-
-
Save bnmrrs/debd1eb838a64bb09c2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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