refactor: reorganize module imports for consistency and clarity
This commit is contained in:
parent
cb84d40a48
commit
9dbfeef94f
9 changed files with 41 additions and 67 deletions
|
@ -1,6 +1,7 @@
|
||||||
use actix_web::web::{self, ServiceConfig};
|
use actix_web::web::{self, ServiceConfig};
|
||||||
|
|
||||||
pub mod auth; // TODO: Refactor to use re-exports instead of making module public
|
// TODO: Refactor to use re-exports instead of making module public
|
||||||
|
pub mod auth;
|
||||||
pub mod class;
|
pub mod class;
|
||||||
pub mod group;
|
pub mod group;
|
||||||
pub mod project;
|
pub mod project;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use validator::Validate;
|
||||||
|
|
||||||
use crate::db::project::CreateProject;
|
use crate::db::project::CreateProject;
|
||||||
use crate::db::Database;
|
use crate::db::Database;
|
||||||
use crate::entity;
|
use crate::db::entity;
|
||||||
use crate::error::ApiError;
|
use crate::error::ApiError;
|
||||||
|
|
||||||
pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
|
pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{Database, entity, error::ApiError};
|
use crate::{Database, db::entity, error::ApiError};
|
||||||
use actix_web::{Responder, delete, get, post, put, web};
|
use actix_web::{Responder, delete, get, post, put, web};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
|
|
@ -2,7 +2,7 @@ use super::Database;
|
||||||
use crate::error::ApiError;
|
use crate::error::ApiError;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
use crate::entity::project;
|
use crate::db::entity::project;
|
||||||
use sea_orm::ActiveValue::{NotSet, Set, Unchanged};
|
use sea_orm::ActiveValue::{NotSet, Set, Unchanged};
|
||||||
use sea_orm::{ActiveModelTrait, DeleteResult, EntityTrait};
|
use sea_orm::{ActiveModelTrait, DeleteResult, EntityTrait};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
|
@ -10,7 +10,7 @@ use sea_orm::{
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{Database, entity};
|
use crate::{Database, db::entity};
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
pub async fn get_users(&self) -> Result<Vec<entity::user::Model>, ApiError> {
|
pub async fn get_users(&self) -> Result<Vec<entity::user::Model>, ApiError> {
|
||||||
|
|
|
@ -2,38 +2,8 @@ pub mod controller;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod utoipa;
|
pub mod utoipa;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
pub use db::Database;
|
pub use db::Database;
|
||||||
pub use db::entity;
|
pub use db::entity;
|
||||||
|
pub use utils::{build_database_url, get_env_var};
|
||||||
use dotenvy;
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
#[cfg(not(test))]
|
|
||||||
fn get_env_var(name: &str) -> dotenvy::Result<String> {
|
|
||||||
dotenvy::var(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn get_env_var(name: &str) -> Result<String, std::env::VarError> {
|
|
||||||
std::env::var(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Extract build_database_url into a utils module or similar
|
|
||||||
pub fn build_database_url() -> String {
|
|
||||||
let db_user = get_env_var("DB_USER").unwrap_or_else(|_| "pgg".to_owned());
|
|
||||||
let db_name = get_env_var("DB_NAME").unwrap_or_else(|_| "pgg".to_owned());
|
|
||||||
let db_password = get_env_var("DB_PASSWORD").unwrap_or_else(|_| "pgg".to_owned());
|
|
||||||
let db_host = get_env_var("DB_HOST").expect("DB_HOST must be set in .env");
|
|
||||||
let db_port = get_env_var("DB_PORT")
|
|
||||||
.map(|x| x.parse::<u16>().expect("DB_PORT is not a valid port"))
|
|
||||||
.unwrap_or(5432);
|
|
||||||
|
|
||||||
let result = format!(
|
|
||||||
"postgresql://{}:{}@{}:{}/{}",
|
|
||||||
db_user, db_password, db_host, db_port, db_name
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("Database URL: {}", result);
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
|
@ -8,12 +8,13 @@ mod controller;
|
||||||
mod db;
|
mod db;
|
||||||
mod error;
|
mod error;
|
||||||
mod utoipa;
|
mod utoipa;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
pub use db::Database;
|
use db::Database;
|
||||||
pub use db::entity;
|
|
||||||
use log::info;
|
use log::info;
|
||||||
use migration::Migrator;
|
use migration::Migrator;
|
||||||
use migration::MigratorTrait;
|
use migration::MigratorTrait;
|
||||||
|
use utils::{build_database_url, get_env_var};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppConfig {
|
struct AppConfig {
|
||||||
|
@ -80,15 +81,6 @@ async fn main() -> std::io::Result<()> {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
|
||||||
fn get_env_var(name: &str) -> dotenvy::Result<String> {
|
|
||||||
dotenvy::var(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn get_env_var(name: &str) -> Result<String, std::env::VarError> {
|
|
||||||
std::env::var(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn connect_to_redis_database() -> RedisSessionStore {
|
pub async fn connect_to_redis_database() -> RedisSessionStore {
|
||||||
let redis_host = get_env_var("REDIS_HOST").expect("REDIS_HOST must be set in .env");
|
let redis_host = get_env_var("REDIS_HOST").expect("REDIS_HOST must be set in .env");
|
||||||
|
@ -102,24 +94,6 @@ pub async fn connect_to_redis_database() -> RedisSessionStore {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_database_url() -> String {
|
|
||||||
let db_user = get_env_var("DB_USER").unwrap_or_else(|_| "pgg".to_owned());
|
|
||||||
let db_name = get_env_var("DB_NAME").unwrap_or_else(|_| "pgg".to_owned());
|
|
||||||
let db_password = get_env_var("DB_PASSWORD").unwrap_or_else(|_| "pgg".to_owned());
|
|
||||||
let db_host = get_env_var("DB_HOST").expect("DB_HOST must be set in .env");
|
|
||||||
let db_port = get_env_var("DB_PORT")
|
|
||||||
.map(|x| x.parse::<u16>().expect("DB_PORT is not a valid port"))
|
|
||||||
.unwrap_or(5432);
|
|
||||||
|
|
||||||
let result = format!(
|
|
||||||
"postgresql://{}:{}@{}:{}/{}",
|
|
||||||
db_user, db_password, db_host, db_port, db_name
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("Database URL: {}", result);
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
29
crates/backend/src/utils.rs
Normal file
29
crates/backend/src/utils.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use log::info;
|
||||||
|
|
||||||
|
#[cfg(not(test))]
|
||||||
|
pub fn get_env_var(name: &str) -> dotenvy::Result<String> {
|
||||||
|
dotenvy::var(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn get_env_var(name: &str) -> Result<String, std::env::VarError> {
|
||||||
|
std::env::var(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_database_url() -> String {
|
||||||
|
let db_user = get_env_var("DB_USER").unwrap_or_else(|_| "pgg".to_owned());
|
||||||
|
let db_name = get_env_var("DB_NAME").unwrap_or_else(|_| "pgg".to_owned());
|
||||||
|
let db_password = get_env_var("DB_PASSWORD").unwrap_or_else(|_| "pgg".to_owned());
|
||||||
|
let db_host = get_env_var("DB_HOST").expect("DB_HOST must be set in .env");
|
||||||
|
let db_port = get_env_var("DB_PORT")
|
||||||
|
.map(|x| x.parse::<u16>().expect("DB_PORT is not a valid port"))
|
||||||
|
.unwrap_or(5432);
|
||||||
|
|
||||||
|
let result = format!(
|
||||||
|
"postgresql://{}:{}@{}:{}/{}",
|
||||||
|
db_user, db_password, db_host, db_port, db_name
|
||||||
|
);
|
||||||
|
|
||||||
|
info!("Database URL: {}", result);
|
||||||
|
result
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
use utoipa::OpenApi;
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
use crate::{controller, db, entity, error};
|
use crate::{controller, db, db::entity, error};
|
||||||
|
|
||||||
#[derive(OpenApi)]
|
#[derive(OpenApi)]
|
||||||
#[openapi(
|
#[openapi(
|
||||||
|
|
Loading…
Add table
Reference in a new issue