Added working database connectivity

This commit is contained in:
franklinblanco 2022-06-23 15:55:06 -04:00
parent c08f0874bd
commit 921e0a03d1
5 changed files with 36 additions and 6 deletions

View File

@ -0,0 +1,22 @@
use std::collections::HashMap;
use sqlx::{MySqlConnection, Connection};
pub async fn start_database_connection(env_vars: &HashMap<String, String>) -> Result<MySqlConnection, sqlx::Error>{
let db_user = match env_vars.get("DB_USER") {
Some(str) => str,
None => panic!("DB_USER env var not found")
};
let db_pass = match env_vars.get("DB_PASS") {
Some(str) => str,
None => panic!("DB_PASS env var not found")
};
let db_host = match env_vars.get("DB_HOST") {
Some(str) => str,
None => panic!("DB_HOST env var not found")
};
let db_database_name = match env_vars.get("DB_DATABASE_NAME") {
Some(str) => str,
None => panic!("DB_DATABASE_NAME env var not found")
};
MySqlConnection::connect(&format!("mysql://{db_user}:{db_pass}@{db_host}/{db_database_name}")).await
}

View File

@ -1,6 +1,8 @@
use sqlx::{ MySqlConnection};
use crate::r#do::user::User; use crate::r#do::user::User;
pub async fn insert_user(user: User) -> Result<(), sqlx::Error>{ pub async fn _insert_user(_conn: &mut MySqlConnection, _user: User) -> Result<(), sqlx::Error>{
Ok(()) Ok(())
} }

View File

@ -1,17 +1,23 @@
mod r#do; mod dao; mod r#do; mod dao;
mod routes; mod service; mod routes; mod service;
mod util; mod util;
use util::env_util; use util::env_util;
use routes::main_router::{start_all_routes, after_startup_fn}; use routes::main_router::{start_all_routes, after_startup_fn};
use dao::main_dao;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), std::io::Error> { async fn main() -> Result<(), std::io::Error> {
// Retrieve env variables and send to services that need them. // Retrieve env variables and send to services that need them.
let env_vars = env_util::get_dot_env_map(); let env_vars = env_util::get_dot_env_map();
// Start database // Start database
let _db_conn = match main_dao::start_database_connection(&env_vars).await {
Ok(conn) => conn,
Err(e) => panic!("{}", e)
};
// Start server // Start server
start_all_routes(&after_startup_fn, env_vars).await start_all_routes(&after_startup_fn, &env_vars).await
} }

View File

@ -7,7 +7,7 @@ pub fn after_startup_fn(){
println!("{}", "Started server."); println!("{}", "Started server.");
} }
pub async fn start_all_routes(after_startup_fn_call: &dyn Fn(), env_vars: HashMap<String, String>) pub async fn start_all_routes(after_startup_fn_call: &dyn Fn(), env_vars: &HashMap<String, String>)
-> Result<(), std::io::Error> -> Result<(), std::io::Error>
{ {
// Get env variables to build server address // Get env variables to build server address

View File

@ -1,8 +1,8 @@
use actix_web::{get, web, Responder, HttpResponse, body::MessageBody}; use actix_web::{get, web::{self, Path}, HttpResponse};
use crate::r#do::user::User; use crate::r#do::user::User;
#[get("/user/{id}")] #[get("/user/{id}")]
pub async fn get_user_from_db(id: web::Path<u16>) -> HttpResponse { pub async fn get_user_from_db(id: Path<u16>) -> HttpResponse {
HttpResponse::Ok().json(web::Json(User {id: *id, name: "nigga".to_string()})) HttpResponse::Ok().json(web::Json(User {id: *id, name: "nigga".to_string()}))
} }