Basic settings support

This commit is contained in:
Bad Manners 2025-04-11 23:50:28 -03:00
parent 239258e324
commit 4960527af3
10 changed files with 189 additions and 20 deletions

View file

@ -1,4 +1,5 @@
pub(crate) mod auth;
pub(crate) mod config;
pub(crate) mod entities;
pub(crate) mod error;
pub(crate) mod query;
@ -13,20 +14,22 @@ use axum::{
routing::{delete, get, post, put},
};
use axum_login::AuthManagerLayerBuilder;
use entities::{prelude::SameyConfig, samey_config};
use password_auth::generate_hash;
use sea_orm::{ActiveValue::Set, DatabaseConnection, EntityTrait};
use tokio::fs;
use sea_orm::{ActiveValue::Set, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
use tokio::{fs, sync::RwLock};
use tower_http::services::ServeDir;
use tower_sessions::SessionManagerLayer;
use crate::auth::{Backend, SessionStorage};
use crate::config::APPLICATION_NAME_KEY;
use crate::entities::{prelude::SameyUser, samey_user};
pub use crate::error::SameyError;
use crate::views::{
add_post_source, add_post_to_pool, change_pool_visibility, create_pool, delete_post,
edit_post_details, get_full_media, get_media, get_pools, get_pools_page, index, login, logout,
post_details, posts, posts_page, remove_field, remove_pool_post, search_tags, select_tag,
sort_pool, submit_post_details, upload, view_pool, view_post,
settings, sort_pool, submit_post_details, update_settings, upload, view_pool, view_post,
};
pub(crate) const NEGATIVE_PREFIX: &str = "-";
@ -36,6 +39,7 @@ pub(crate) const RATING_PREFIX: &str = "rating:";
pub(crate) struct AppState {
files_dir: Arc<String>,
db: DatabaseConnection,
application_name: Arc<RwLock<String>>,
}
pub async fn create_user(
@ -56,9 +60,18 @@ pub async fn create_user(
}
pub async fn get_router(db: DatabaseConnection, files_dir: &str) -> Result<Router, SameyError> {
let application_name = match SameyConfig::find()
.filter(samey_config::Column::Key.eq(APPLICATION_NAME_KEY))
.one(&db)
.await?
{
Some(row) => row.data.as_str().unwrap_or("Samey").to_owned(),
None => "Samey".to_owned(),
};
let state = AppState {
files_dir: Arc::new(files_dir.into()),
db: db.clone(),
application_name: Arc::new(RwLock::new(application_name)),
};
fs::create_dir_all(files_dir).await?;
@ -96,6 +109,8 @@ pub async fn get_router(db: DatabaseConnection, files_dir: &str) -> Result<Route
.route("/pool/{pool_id}/post", post(add_post_to_pool))
.route("/pool/{pool_id}/sort", put(sort_pool))
.route("/pool_post/{pool_post_id}", delete(remove_pool_post))
// Settings routes
.route("/settings", get(settings).put(update_settings))
// Search routes
.route("/posts", get(posts))
.route("/posts/{page}", get(posts_page))