Skip to content

Instantly share code, notes, and snippets.

@snobu
Created February 26, 2019 11:46
Show Gist options
  • Save snobu/058ba1079c36d8ddacb48c6d682d4f75 to your computer and use it in GitHub Desktop.
Save snobu/058ba1079c36d8ddacb48c6d682d4f75 to your computer and use it in GitHub Desktop.
Rust Iron minimal API
[package]
name = "iron"
version = "0.1.0"
authors = ["snobu <foo@snobu.org>"]
edition = "2018"
[dependencies]
iron = "*"
rustc-serialize = "*"
chrono = "*"
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