diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3e943f2 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +MYSQL_HOST= +MYSQL_PORT= +MYSQL_USERNAME= +MYSQL_PASSWORD= +MYSQL_DATABASE= diff --git a/.gitignore b/.gitignore index 018ca47..f778acf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ Binaries/ Cargo.lock + +.env diff --git a/Cargo.toml b/Cargo.toml index 84e84ea..32301eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "API" +name = "RustAPI" version = "0.0.1" description = "Rust API" repository = "https://github.com/GuilhermeWerner/RustAPI" @@ -14,7 +14,8 @@ path = "Source/Server.rs" [dependencies] rocket = "0.4.6" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" rocket_contrib = "0.4.6" -# serde = "1.0" -# serde_json = "1.0" -# serde_derive = "1.0" +mysql = "20.1.0" +dotenv = "0.15.0" diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..c1b776f --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,26 @@ +[development] +address = "localhost" +port = 5000 +keep_alive = 5 +read_timeout = 5 +write_timeout = 5 +log = "normal" +limits = { forms = 32768 } + +[staging] +address = "localhost" +port = 5000 +keep_alive = 5 +read_timeout = 5 +write_timeout = 5 +log = "normal" +limits = { forms = 32768 } + +[production] +address = "localhost" +port = 5000 +keep_alive = 5 +read_timeout = 5 +write_timeout = 5 +log = "critical" +limits = { forms = 32768 } diff --git a/RustAPI.code-workspace b/RustAPI.code-workspace index 8739f76..98980d4 100644 --- a/RustAPI.code-workspace +++ b/RustAPI.code-workspace @@ -1,10 +1,10 @@ { - "folders": [ - { - "path": "." - } - ], - "settings": { + "folders": [ + { + "path": "." + } + ], + "settings": { "files.associations": { "*.toml": "properties" } diff --git a/Source/Server.rs b/Source/Server.rs index 57c286e..6965744 100644 --- a/Source/Server.rs +++ b/Source/Server.rs @@ -1,18 +1,57 @@ #![feature(proc_macro_hygiene, decl_macro)] -#[macro_use] -extern crate rocket; - -#[macro_use] -extern crate rocket_contrib; +use std::env; +use dotenv::dotenv; +use rocket::{get, routes, catch, catchers}; +use rocket_contrib::json; use rocket_contrib::json::{Json, JsonValue}; +use mysql::*; +use mysql::prelude::*; + +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Debug)] +struct Email { + address: String, + user_id: String, + is_confirmed: bool, + is_primary: bool, +} + #[get("/")] fn hello() -> JsonValue { return json!({ "TribuFu": "Hello World" }); } +#[get("/emails")] +fn emails() -> JsonValue { + dotenv().ok(); + + let MYSQL_HOST: String = env::var("MYSQL_HOST").expect("Missing MYSQL_HOST in Environment"); + let MYSQL_PORT: String = env::var("MYSQL_PORT").expect("Missing MYSQL_PORT in Environmment"); + let MYSQL_USERNAME: String = env::var("MYSQL_USERNAME").expect("Missing MYSQL_USERNAME in Environmment"); + let MYSQL_PASSWORD: String = env::var("MYSQL_PASSWORD").expect("Missing MYSQL_PASSWORD in Environmment"); + let MYSQL_DATABASE: String = env::var("MYSQL_DATABASE").expect("Missing MYSQL_DATABASE in Environmment"); + + let url = format!("mysql://{}:{}@{}:{}/{}", MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_DATABASE); + + let pool = Pool::new(url).unwrap(); + + let mut connection = pool.get_conn().unwrap(); + + // Let's select emails from database. Type inference should do the trick here. + let emails = connection.query_map( + "SELECT Address, UserId, IsConfirmed, IsPrimary from Email", + |(address, user_id, is_confirmed, is_primary)| { + Email { address, user_id, is_confirmed, is_primary } + }, + ).unwrap(); + + return json!(emails); +} + #[catch(404)] fn not_found() -> JsonValue { return json!({ @@ -20,12 +59,9 @@ fn not_found() -> JsonValue { }); } -fn rocket() -> rocket::Rocket { - return rocket::ignite() - .mount("/", routes![hello]) - .register(catchers![not_found]); -} - fn main() { - rocket().launch(); + rocket::ignite() + .mount("/", routes![hello, emails]) + .register(catchers![not_found]) + .launch(); }