Add config controller in server
This commit is contained in:
44
server/src/utils/config.rs
Normal file
44
server/src/utils/config.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use crate::utils::{error::*, DbPool};
|
||||
use sqlx::Row;
|
||||
|
||||
pub struct ConfigManager;
|
||||
|
||||
impl ConfigManager {
|
||||
pub async fn get_config(pool: &DbPool, key: &str) -> AppResult<Option<String>> {
|
||||
let row = sqlx::query("SELECT value FROM config WHERE key = ?")
|
||||
.bind(key)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
|
||||
if let Some(row) = row {
|
||||
Ok(Some(row.get("value")))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_config(pool: &DbPool, key: &str, value: &str) -> AppResult<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
INSERT INTO config (key, value, updated_at)
|
||||
VALUES (?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(key) DO UPDATE SET
|
||||
value = excluded.value,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
"#,
|
||||
)
|
||||
.bind(key)
|
||||
.bind(value)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_external_url(pool: &DbPool) -> AppResult<String> {
|
||||
match Self::get_config(pool, "EXTERNAL_URL").await? {
|
||||
Some(url) => Ok(url),
|
||||
None => Err(internal_error("EXTERNAL_URL not configured")),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user