Refactor container definitions

This commit is contained in:
GW_MC
2025-12-22 14:32:57 +08:00
parent 7db23b01df
commit 6e85bda13f
12 changed files with 92 additions and 91 deletions

View File

@@ -0,0 +1,41 @@
#[derive(Default)]
pub struct OptionalContainerConfig {
pub image: Option<String>,
pub tag: Option<String>,
pub container_name: Option<String>,
pub database_name: Option<String>,
pub user: Option<String>,
pub password: Option<String>,
}
#[derive(Clone)]
pub struct DatabaseContainerConfig {
pub image: String,
pub tag: String,
pub container_name: String,
pub database_name: String,
pub user: String,
pub password: String,
}
impl OptionalContainerConfig {
pub fn fill_with(&self, other: &DatabaseContainerConfig) -> DatabaseContainerConfig {
DatabaseContainerConfig {
image: self.image.clone().unwrap_or_else(|| other.image.clone()),
tag: self.tag.clone().unwrap_or_else(|| other.tag.clone()),
container_name: self
.container_name
.clone()
.unwrap_or_else(|| other.container_name.clone()),
database_name: self
.database_name
.clone()
.unwrap_or_else(|| other.database_name.clone()),
user: self.user.clone().unwrap_or_else(|| other.user.clone()),
password: self
.password
.clone()
.unwrap_or_else(|| other.password.clone()),
}
}
}

View File

@@ -0,0 +1,98 @@
use std::{error::Error, sync::Arc};
use async_trait::async_trait;
use testcontainers::{
GenericImage, ImageExt,
core::{IntoContainerPort, WaitFor},
runners::AsyncRunner,
};
use crate::{
ConfigInfoType,
containers::{
UnStartedContainer,
db::{
ContainerizedDBInfo, DBConfigInfoType, DBInfo,
config::{DatabaseContainerConfig, OptionalContainerConfig},
},
},
};
pub fn get_default_config() -> DatabaseContainerConfig {
DatabaseContainerConfig {
container_name: "yanpm-postgres".to_string(),
database_name: "postgres".to_string(),
user: "postgres".to_string(),
password: "postgres".to_string(),
image: "postgres".to_string(),
tag: "16-alpine".to_string(),
}
}
pub struct PostgreSQLContainer {
pub config: DatabaseContainerConfig,
}
#[async_trait]
impl DBInfo<OptionalContainerConfig> for PostgreSQLContainer {
async fn get_db_container_config_info(&self) -> DBConfigInfoType {
let pg_container = self
.get_unstarted_container()
.unwrap()
.await
.expect("Failed to start PostgreSQL container");
let pg_host = pg_container.get_host().await.expect("Failed to get host");
let pg_host_port = pg_container
.get_host_port_ipv4(5432.tcp())
.await
.expect("Failed to get host port");
let pg_url = format!(
"postgres://{}:{}@{}:{}/{}",
self.config.user,
self.config.password,
pg_host,
pg_host_port,
self.config.database_name
);
ConfigInfoType::Containerized(ContainerizedDBInfo {
db_type: crate::containers::db::DBType::PostgreSQL,
container: Arc::new(pg_container),
container_name: self.config.container_name.clone(),
database_name: self.config.database_name.clone(),
host: pg_host,
port: pg_host_port,
url: pg_url,
user: self.config.user.clone(),
password: self.config.password.clone(),
})
}
async fn new(user_default_config: Option<OptionalContainerConfig>) -> Self
where
Self: Sized,
{
let default_config = get_default_config();
let config = user_default_config
.unwrap_or_default()
.fill_with(&default_config);
PostgreSQLContainer {
config: config.clone(),
}
}
fn get_unstarted_container(&self) -> Result<UnStartedContainer, Box<dyn Error>> {
Ok(
GenericImage::new(self.config.image.clone(), self.config.tag.clone())
.with_exposed_port(5432.tcp())
.with_wait_for(WaitFor::message_on_stderr(
"database system is ready to accept connections",
))
.with_container_name(self.config.container_name.clone())
.with_env_var("POSTGRES_USER", self.config.user.clone())
.with_env_var("POSTGRES_PASSWORD", self.config.password.clone())
.start(),
)
}
}

View File

@@ -0,0 +1,107 @@
use std::{error::Error, path::PathBuf, sync::Arc};
use async_trait::async_trait;
use crate::{
ConfigInfoType,
containers::db::{DBConfigInfoType, DBInfo, PreExistingDBInfo, UnStartedContainer},
util::to_absolute_path,
};
#[derive(Clone)]
pub struct ContainerConfig {
// Add any SQLite-specific configuration options here if needed
pub database_name: String,
pub absolute_dir_path: PathBuf,
}
#[derive(Default)]
pub struct OptionalContainerConfig {
// Add any optional configuration fields here
pub database_name: Option<String>,
pub absolute_path: Option<PathBuf>,
}
impl OptionalContainerConfig {
pub fn fill_with(&self, other: &ContainerConfig) -> ContainerConfig {
ContainerConfig {
database_name: self
.database_name
.clone()
.unwrap_or_else(|| other.database_name.clone()),
absolute_dir_path: self
.absolute_path
.clone()
.unwrap_or_else(|| other.absolute_dir_path.clone()),
}
}
}
pub fn get_default_config() -> ContainerConfig {
ContainerConfig {
database_name: "sqlite".to_string(),
absolute_dir_path: to_absolute_path("./generated/sqlite"),
}
}
pub struct SQLiteContainer {
pub config: ContainerConfig,
}
impl SQLiteContainer {
fn get_db_absolute_path(&self) -> PathBuf {
self.config
.absolute_dir_path
.join(&self.config.database_name)
.with_extension("db")
}
}
#[async_trait]
impl DBInfo<OptionalContainerConfig> for SQLiteContainer {
async fn get_db_container_config_info(&self) -> DBConfigInfoType {
// sqlite filepath url does not include the "sqlite://" prefix
let sqlite_url = format!("sqlite://{}", self.get_db_absolute_path().to_string_lossy());
// create the file
std::fs::create_dir_all(&self.config.absolute_dir_path)
.expect("Failed to create directories for SQLite database");
std::fs::File::create(self.get_db_absolute_path())
.expect("Failed to create SQLite database file");
//
ConfigInfoType::PreExisting(PreExistingDBInfo {
db_type: crate::containers::db::DBType::SQLite,
url: sqlite_url,
on_delete: {
let db_path = self.get_db_absolute_path();
Arc::new(move || {
// delete the sqlite database file
if db_path.exists()
&& let Err(e) = std::fs::remove_file(&db_path)
{
eprintln!("Failed to delete SQLite database file: {}", e);
}
})
},
})
}
async fn new(user_default_config: Option<OptionalContainerConfig>) -> Self
where
Self: Sized,
{
let default_config = get_default_config();
let config = user_default_config
.unwrap_or_default()
.fill_with(&default_config);
SQLiteContainer {
config: config.clone(),
}
}
fn get_unstarted_container(&self) -> Result<UnStartedContainer, Box<dyn Error>> {
Err(Box::new(std::io::Error::other(
"SQLite does not use a container",
)))
}
}