From 7e8ee5ed9cad6484e9f13f81731b102ced58402e Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 9 Jul 2019 15:14:04 -0400 Subject: Init. --- meap/meap-code/ch1/ch1-time-api/Cargo.toml | 16 +++++++++++++ meap/meap-code/ch1/ch1-time-api/src/main.rs | 36 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 meap/meap-code/ch1/ch1-time-api/Cargo.toml create mode 100755 meap/meap-code/ch1/ch1-time-api/src/main.rs (limited to 'meap/meap-code/ch1/ch1-time-api') diff --git a/meap/meap-code/ch1/ch1-time-api/Cargo.toml b/meap/meap-code/ch1/ch1-time-api/Cargo.toml new file mode 100755 index 0000000..da28bde --- /dev/null +++ b/meap/meap-code/ch1/ch1-time-api/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "ch1-time-api" +version = "0.1.0" +authors = ["Tim McNamara "] + +[dependencies] +chrono = "0.4.0" +rocket = "0.3.0" +rocket_codegen = "0.3.0" +serde = "1.0" +serde_derive = "1.0" + +[dependencies.rocket_contrib] +version = "0.3.0" +default-features = false +features = ["json"] \ No newline at end of file diff --git a/meap/meap-code/ch1/ch1-time-api/src/main.rs b/meap/meap-code/ch1/ch1-time-api/src/main.rs new file mode 100755 index 0000000..660c1cc --- /dev/null +++ b/meap/meap-code/ch1/ch1-time-api/src/main.rs @@ -0,0 +1,36 @@ +#![feature(plugin)] // <1> +#![plugin(rocket_codegen)] // <1> + +extern crate serde; // <2> +extern crate chrono; // <2> +extern crate rocket; // <2> +extern crate rocket_contrib; // <2> + +#[macro_use] // <3> Syntax to indicate that we want to import macros from another module +extern crate serde_derive; // <3> + +use chrono::prelude::*; // <4> brings all exported members into local scope (e.g. DateTime and Utc) +use rocket_contrib::{Json}; // <5> bring single member into local scope + +#[derive(Serialize)] // <6> Automatically generate a string representation of this struct (which will be used as JSON) +struct Timestamp { // <7> Syntax to create a custom type + time: String, // <8> The `Timestamp` `time` field is of type `String` +} + +#[get("/")] // <9> Custom syntax provided by the library that indicates to code generation +fn index() -> &'static str { // <10> Define a function with no arguments and its return type + "Hello, world!" // <11> Rust returns the result of the final expression +} + +#[get("/time")] +fn time_now() -> Json { + let now: DateTime = Utc::now(); + let timestamp = Timestamp { time: now.to_rfc3339() }; + Json(timestamp) +} + +fn main() { + rocket::ignite() + .mount("/", routes![index, time_now]) + .launch(); +} \ No newline at end of file -- cgit v1.2.3