added build script and removed unnecesary comments. This service now embeds itself into the db

This commit is contained in:
franklinblanco 2022-07-13 21:33:30 -04:00
parent 9c6d04087a
commit 5c18b484a1
5 changed files with 38 additions and 19 deletions

View File

@ -2,7 +2,7 @@
name = "user-svc-actix" name = "user-svc-actix"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
@ -15,3 +15,8 @@ chrono = { version = "0.4", features = [ "serde" ] }
ring = "0.16.20" ring = "0.16.20"
data-encoding = "2.3.2" data-encoding = "2.3.2"
futures-util = "0.3" 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"] }

27
build.rs Normal file
View File

@ -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<String, String> = 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}")}
};
}

View File

@ -0,0 +1,3 @@
-- Add migration script here
CREATE DATABASE IF NOT EXISTS usersvc;
USE usersvc;

View File

@ -20,15 +20,7 @@ pub async fn start_database_connection(env_vars: &HashMap<String, String>) -> Re
None => panic!("DB_DATABASE_NAME env var not found") 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 formatted_db_url = &format!("mysql://{db_user}:{db_pass}@{db_host}/{db_database_name}");
let mut rs = sqlx::MySqlConnection::connect(&formatted_db_url).await; 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
} }
pub async fn run_all_migrations(conn: &mut MySqlConnection){ pub async fn run_all_migrations(conn: &mut MySqlConnection){
match sqlx::migrate!("./migrations").run(conn).await { match sqlx::migrate!("./migrations").run(conn).await {

View File

@ -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}; 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<i32>, db_conn: Data<Mutex<MySqlConnection>>) -> HttpResponse {
match find_user_by_id(&mut db_conn.lock().unwrap(), *id).await{
Ok(MySqlQueryResult)
}
HttpResponse::Ok().json(web::Json("ss"))
}*/
#[post("/user")] #[post("/user")]
pub async fn create_user(incoming_user: web::Json<UserForCreationDto>, db_conn: Data<Mutex<MySqlConnection>>) -> HttpResponse { pub async fn create_user(incoming_user: web::Json<UserForCreationDto>, db_conn: Data<Mutex<MySqlConnection>>) -> HttpResponse {
let mut message_resources: Vec<MessageResourceDto> = Vec::new(); let mut message_resources: Vec<MessageResourceDto> = Vec::new();