diff --git a/Cargo.toml b/Cargo.toml index e2a8460..4ee5ce2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "user-svc-actix" version = "0.1.0" edition = "2021" - +build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -14,4 +14,9 @@ actix-web = "4" chrono = { version = "0.4", features = [ "serde" ] } ring = "0.16.20" data-encoding = "2.3.2" -futures-util = "0.3" \ No newline at end of file +futures-util = "0.3" + +[build-dependencies] +dotenv = "0.15.0" +sqlx = { version = "0.6.0", features = [ "runtime-tokio-rustls", "mysql", "chrono" ] } +tokio = { version = "1", features = ["full"] } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..9d7faf5 --- /dev/null +++ b/build.rs @@ -0,0 +1,27 @@ +extern crate dotenv; + +use std::{env, collections::HashMap}; + +use sqlx::Connection; +use dotenv::dotenv; + +#[tokio::main] +async fn main(){ + dotenv().ok(); + let mut dotenv_vars: HashMap = HashMap::new(); + for (key, val) in env::vars() { + dotenv_vars.insert(key, val); + } + let db_url = match dotenv_vars.get("DATABASE_URL") { + Some(var) => {var}, + None => {panic!("Nigga damn")} + }; + let mut conn = match sqlx::MySqlConnection::connect(&db_url).await { + Ok(res) => {res}, + Err(e) => {panic!("{}", e)} + }; + match sqlx::migrate!("./migrations").run(&mut conn).await { + Ok(()) => {println!("{}", "Successfully ran migrations.")}, + Err(error) => {panic!("{error}")} + }; +} \ No newline at end of file diff --git a/migrations/20220714012938_create_databases.sql b/migrations/20220714012938_create_databases.sql new file mode 100644 index 0000000..9477429 --- /dev/null +++ b/migrations/20220714012938_create_databases.sql @@ -0,0 +1,3 @@ +-- Add migration script here +CREATE DATABASE IF NOT EXISTS usersvc; +USE usersvc; \ No newline at end of file diff --git a/src/dao/main_dao.rs b/src/dao/main_dao.rs index c769380..af26feb 100644 --- a/src/dao/main_dao.rs +++ b/src/dao/main_dao.rs @@ -20,15 +20,7 @@ pub async fn start_database_connection(env_vars: &HashMap) -> Re None => panic!("DB_DATABASE_NAME env var not found") }; let formatted_db_url = &format!("mysql://{db_user}:{db_pass}@{db_host}/{db_database_name}"); - let mut rs = sqlx::MySqlConnection::connect(&formatted_db_url).await; - for _i in 1..20 { - match &rs { - Ok(_conn) => {break;}, - Err(_e) => {} - } - rs = sqlx::MySqlConnection::connect(&formatted_db_url).await; - } - rs + sqlx::MySqlConnection::connect(&formatted_db_url).await } pub async fn run_all_migrations(conn: &mut MySqlConnection){ match sqlx::migrate!("./migrations").run(conn).await { diff --git a/src/routes/user_routes.rs b/src/routes/user_routes.rs index ac828fe..bb52da9 100644 --- a/src/routes/user_routes.rs +++ b/src/routes/user_routes.rs @@ -7,14 +7,6 @@ use sqlx::MySqlConnection; use crate::{r#do::user::User, dao::{user_dao::{insert_user, find_user_by_email}, token_dao::{insert_token, self, update_token_with_id}}, dto::{user_dtos::{UserForCreationDto, UserForLoginDto}, message_resources_dtos::MessageResourceDto}, validation::user_validator, util::hasher::{self, generate_multiple_random_token_with_rng}, r#do::token::Token, resources::error_messages::{ERROR_USER_ALREADY_EXISTS, ERROR_USER_DOES_NOT_EXIST, ERROR_PASSWORD_INCORRECT, ERROR_INVALID_TOKEN, ERROR_MISSING_TOKEN, ERROR_INCORRECT_TOKEN, ERROR_EXPIRED_TOKEN}, r#do::token::AUTH_TOKEN_EXPIRATION_TIME_IN_DAYS, r#do::token::REFRESH_TOKEN_EXPIRATION_TIME_IN_DAYS}; -/*#[get("/u&ser/{id}")] -pub async fn get_user_from_db(id: Path, db_conn: Data>) -> HttpResponse { - match find_user_by_id(&mut db_conn.lock().unwrap(), *id).await{ - Ok(MySqlQueryResult) - } - HttpResponse::Ok().json(web::Json("ss")) -}*/ - #[post("/user")] pub async fn create_user(incoming_user: web::Json, db_conn: Data>) -> HttpResponse { let mut message_resources: Vec = Vec::new();