Created
February 26, 2019 11:46
-
-
Save snobu/058ba1079c36d8ddacb48c6d682d4f75 to your computer and use it in GitHub Desktop.
Rust Iron minimal API
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
[package] | |
name = "iron" | |
version = "0.1.0" | |
authors = ["snobu <foo@snobu.org>"] | |
edition = "2018" | |
[dependencies] | |
iron = "*" | |
rustc-serialize = "*" | |
chrono = "*" |
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 iron; | |
extern crate rustc_serialize; | |
extern crate chrono; | |
use iron::prelude::*; | |
use iron::status; | |
use rustc_serialize::json; | |
use chrono::Utc; | |
#[derive(RustcEncodable)] | |
struct Greeting { | |
msg: String, | |
when: String | |
} | |
fn main() { | |
fn hello_world(_: &mut Request) -> IronResult<Response> { | |
let greeting = Greeting { | |
msg: "Rust says hi.".to_string(), | |
when: Utc::now().to_rfc2822().to_string() | |
}; | |
let payload = json::encode(&greeting).unwrap(); | |
Ok(Response::with((status::Ok, payload))) | |
} | |
Iron::new(hello_world).http("localhost:3000").unwrap(); | |
println!("On 3000"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment