Added working database connectivity
This commit is contained in:
parent
c08f0874bd
commit
921e0a03d1
|
@ -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
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
use sqlx::{ MySqlConnection};
|
||||
|
||||
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(())
|
||||
}
|
10
src/main.rs
10
src/main.rs
|
@ -1,17 +1,23 @@
|
|||
mod r#do; mod dao;
|
||||
mod routes; mod service;
|
||||
mod util;
|
||||
|
||||
use util::env_util;
|
||||
use routes::main_router::{start_all_routes, after_startup_fn};
|
||||
use dao::main_dao;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
|
||||
// Retrieve env variables and send to services that need them.
|
||||
let env_vars = env_util::get_dot_env_map();
|
||||
|
||||
// Start database
|
||||
|
||||
let _db_conn = match main_dao::start_database_connection(&env_vars).await {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => panic!("{}", e)
|
||||
};
|
||||
|
||||
// Start server
|
||||
start_all_routes(&after_startup_fn, env_vars).await
|
||||
start_all_routes(&after_startup_fn, &env_vars).await
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ pub fn after_startup_fn(){
|
|||
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>
|
||||
{
|
||||
// Get env variables to build server address
|
||||
|
|
|
@ -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;
|
||||
|
||||
#[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()}))
|
||||
}
|
Loading…
Reference in New Issue