Test MySQL Connection

This commit is contained in:
GuilhermeWerner
2021-01-31 08:44:24 -03:00
parent 09a99f4860
commit 5a38931498
6 changed files with 92 additions and 22 deletions

5
.env.example Normal file
View File

@ -0,0 +1,5 @@
MYSQL_HOST=
MYSQL_PORT=
MYSQL_USERNAME=
MYSQL_PASSWORD=
MYSQL_DATABASE=

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
Binaries/ Binaries/
Cargo.lock Cargo.lock
.env

View File

@ -1,5 +1,5 @@
[package] [package]
name = "API" name = "RustAPI"
version = "0.0.1" version = "0.0.1"
description = "Rust API" description = "Rust API"
repository = "https://github.com/GuilhermeWerner/RustAPI" repository = "https://github.com/GuilhermeWerner/RustAPI"
@ -14,7 +14,8 @@ path = "Source/Server.rs"
[dependencies] [dependencies]
rocket = "0.4.6" rocket = "0.4.6"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rocket_contrib = "0.4.6" rocket_contrib = "0.4.6"
# serde = "1.0" mysql = "20.1.0"
# serde_json = "1.0" dotenv = "0.15.0"
# serde_derive = "1.0"

26
Rocket.toml Normal file
View File

@ -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 }

View File

@ -1,10 +1,10 @@
{ {
"folders": [ "folders": [
{ {
"path": "." "path": "."
} }
], ],
"settings": { "settings": {
"files.associations": { "files.associations": {
"*.toml": "properties" "*.toml": "properties"
} }

View File

@ -1,18 +1,57 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] use std::env;
extern crate rocket; use dotenv::dotenv;
#[macro_use]
extern crate rocket_contrib;
use rocket::{get, routes, catch, catchers};
use rocket_contrib::json;
use rocket_contrib::json::{Json, JsonValue}; 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("/")] #[get("/")]
fn hello() -> JsonValue { fn hello() -> JsonValue {
return json!({ "TribuFu": "Hello World" }); 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)] #[catch(404)]
fn not_found() -> JsonValue { fn not_found() -> JsonValue {
return json!({ 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() { fn main() {
rocket().launch(); rocket::ignite()
.mount("/", routes![hello, emails])
.register(catchers![not_found])
.launch();
} }