Compare commits
4 Commits
6cd55d06a2
...
f4e6eb56c8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f4e6eb56c8 | ||
|
|
f71cf370cd | ||
|
|
fae951c902 | ||
|
|
8b98590a1e |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -4270,11 +4270,13 @@ checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
|
||||
name = "yet-another-nginx-proxy-manager"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"chrono",
|
||||
"config",
|
||||
"database",
|
||||
"migration",
|
||||
"sea-orm",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
|
||||
@@ -12,3 +12,8 @@ resolver = "3"
|
||||
|
||||
[workspace.lints.clippy]
|
||||
module_inception = "allow"
|
||||
|
||||
[workspace.dependencies]
|
||||
sea-orm = "2.0.0-rc"
|
||||
sea-orm-cli = "2.0.0-rc"
|
||||
sea-orm-migration = "2.0.0-rc"
|
||||
|
||||
@@ -8,6 +8,7 @@ database = { path = "../../public/database" }
|
||||
migration = { path = "../../public/migration" }
|
||||
|
||||
axum = { version = "0.8.7", features = ["form", "http1", "json", "matched-path", "original-uri", "query", "tokio", "tower-log", "tracing", "macros"]}
|
||||
async-trait = { version = "0.1.89" }
|
||||
chrono = { version = "0.4.42", features = ["clock", "std", "oldtime", "wasmbind", "serde"] }
|
||||
config = { version = "0.15.19", features = ["toml", "json", "yaml", "ini", "ron", "json5", "convert-case", "async"] }
|
||||
tokio = { version = "1", features = ["fs", "io-util", "io-std", "macros", "net", "parking_lot", "process", "rt", "rt-multi-thread", "signal", "sync", "time", "tracing"] }
|
||||
@@ -16,3 +17,4 @@ tracing = { version = "0.1.41", features = ["std", "attributes"] }
|
||||
tracing-subscriber = { version = "0.3.20", features = ["smallvec", "fmt", "ansi", "tracing-log", "std", "chrono", "json", "serde", "serde_json", "time", "tracing"] }
|
||||
serde_json = { version = "1.0.145", features = ["std"] }
|
||||
serde = { version = "1.0.228", features = ["std", "derive"] }
|
||||
sea-orm = { workspace = true }
|
||||
|
||||
1
apps/api/src/errors.rs
Normal file
1
apps/api/src/errors.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod service_error;
|
||||
15
apps/api/src/errors/service_error.rs
Normal file
15
apps/api/src/errors/service_error.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
pub type ServiceError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
#[allow(dead_code)] // TODO: remove when used
|
||||
pub trait IntoServiceError {
|
||||
fn into_service_error(self) -> ServiceError;
|
||||
}
|
||||
|
||||
impl<T> IntoServiceError for T
|
||||
where
|
||||
T: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
fn into_service_error(self) -> ServiceError {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,23 @@
|
||||
mod configs;
|
||||
mod errors;
|
||||
mod middlewares;
|
||||
mod routes;
|
||||
mod services;
|
||||
mod tasks;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::Router;
|
||||
use database::{ConnectOptions, get_connection};
|
||||
use database::get_connection;
|
||||
use sea_orm::ConnectOptions;
|
||||
use tracing::{debug, info};
|
||||
use tracing_subscriber::fmt::format::{DefaultFields, Format};
|
||||
|
||||
use crate::configs::{ProgramSettings, get_program_settings, logging::LoggingSettings};
|
||||
use crate::{
|
||||
configs::{ProgramSettings, get_program_settings, logging::LoggingSettings},
|
||||
routes::{AppService, AppState},
|
||||
services::settings::SettingsService,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -53,18 +62,17 @@ async fn main() {
|
||||
options.max_connections(settings.database.max_connections);
|
||||
};
|
||||
|
||||
let db_connection = get_connection(&settings.database.url, Some(db_options))
|
||||
.await
|
||||
.expect("Failed to establish database connection");
|
||||
let db_connection = Arc::new(
|
||||
get_connection(&settings.database.url, Some(db_options))
|
||||
.await
|
||||
.expect("Failed to establish database connection"),
|
||||
);
|
||||
|
||||
info!("Database connection established.");
|
||||
|
||||
// build the axum app and run the server...
|
||||
info!("Starting application...");
|
||||
let app: Router = routes::get_root_router(routes::AppState {
|
||||
database_connection: db_connection,
|
||||
service: std::sync::Arc::new(routes::AppService {}),
|
||||
});
|
||||
let app: Router = routes::get_root_router(Arc::new(get_app_state(&db_connection)));
|
||||
|
||||
let address = format!("{}:{}", settings.server.address, settings.server.port);
|
||||
info!("Starting server at http://{}", address);
|
||||
@@ -101,6 +109,15 @@ fn get_global_tracing_subscriber_builder(
|
||||
}
|
||||
}
|
||||
|
||||
fn get_app_state(db_connection: &Arc<sea_orm::DatabaseConnection>) -> AppState {
|
||||
AppState {
|
||||
database_connection: db_connection.clone(),
|
||||
service: Arc::new(AppService {
|
||||
settings: Arc::new(SettingsService::new(db_connection.clone())),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// A small wrapper that holds a boxed `FormatTime` trait object and itself
|
||||
// implements `FormatTime`, allowing us to use it as a concrete type with
|
||||
// `builder.with_timer` while still picking the concrete timer implementation
|
||||
|
||||
@@ -3,20 +3,23 @@ use std::sync::Arc;
|
||||
use axum::{Extension, Router};
|
||||
use migration::sea_orm::DatabaseConnection;
|
||||
|
||||
use crate::middlewares;
|
||||
use crate::{middlewares, services::settings::SettingsStore};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
// TODO: remove dead_code allowances when fields are used
|
||||
#[allow(dead_code)]
|
||||
pub database_connection: DatabaseConnection,
|
||||
pub database_connection: Arc<DatabaseConnection>,
|
||||
// TODO: remove dead_code allowances when fields are used
|
||||
#[allow(dead_code)]
|
||||
pub service: Arc<AppService>,
|
||||
}
|
||||
|
||||
pub type ServiceState<T> = Arc<T>;
|
||||
|
||||
pub struct AppService {
|
||||
//
|
||||
#[allow(dead_code)] // TODO: remove when used
|
||||
pub settings: ServiceState<dyn SettingsStore>,
|
||||
}
|
||||
|
||||
pub fn get_root_router(state: impl Into<Arc<AppState>>) -> Router {
|
||||
|
||||
1
apps/api/src/services.rs
Normal file
1
apps/api/src/services.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod settings;
|
||||
92
apps/api/src/services/settings.rs
Normal file
92
apps/api/src/services/settings.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use database::generated::entities::config::{self, ActiveModel as ConfigActiveModel};
|
||||
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ActiveValue, ColumnTrait, DatabaseConnection, DbErr, EntityTrait,
|
||||
IntoActiveModel, QueryFilter,
|
||||
};
|
||||
|
||||
use crate::errors::service_error::{IntoServiceError, ServiceError};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait SettingsStore: Send + Sync {
|
||||
#[allow(dead_code)] // TODO: remove when used
|
||||
async fn get_setting(&self, key: &str) -> Result<String, ServiceError>;
|
||||
#[allow(dead_code)] // TODO: remove when used
|
||||
async fn set_setting(&self, key: &str, value: String) -> Result<(), ServiceError>;
|
||||
}
|
||||
|
||||
pub struct SettingsService {
|
||||
#[allow(dead_code)] // TODO: remove when used
|
||||
connection: Arc<DatabaseConnection>,
|
||||
}
|
||||
|
||||
impl SettingsService {
|
||||
pub fn new(connection: Arc<DatabaseConnection>) -> Self {
|
||||
Self { connection }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl SettingsStore for SettingsService {
|
||||
async fn get_setting(&self, key: &str) -> Result<String, ServiceError> {
|
||||
let setting = config::Entity::find()
|
||||
.filter(config::Column::Key.eq(key))
|
||||
.one(&*self.connection)
|
||||
.await;
|
||||
|
||||
match setting {
|
||||
Err(err) => Err(err.into_service_error()),
|
||||
Ok(None) => Err(
|
||||
DbErr::RecordNotFound(format!("Setting with key '{}' not found", key))
|
||||
.into_service_error(),
|
||||
),
|
||||
Ok(Some(record)) => Ok(record.value),
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_setting(&self, key: &str, value: String) -> Result<(), ServiceError> {
|
||||
let existing = config::Entity::find()
|
||||
.filter(config::Column::Key.eq(key))
|
||||
.one(&*self.connection)
|
||||
.await;
|
||||
|
||||
let handle_not_found = async |key: String, value: String| {
|
||||
let new_record = ConfigActiveModel {
|
||||
key: ActiveValue::Set(key),
|
||||
value: ActiveValue::Set(value),
|
||||
created_at: ActiveValue::Set(chrono::Utc::now()),
|
||||
updated_at: ActiveValue::Set(chrono::Utc::now()),
|
||||
};
|
||||
new_record
|
||||
.insert(&*self.connection)
|
||||
.await
|
||||
.map_err(|err| err.into_service_error())
|
||||
};
|
||||
|
||||
match existing {
|
||||
Err(err) => match err {
|
||||
DbErr::RecordNotFound(_) => {
|
||||
handle_not_found(key.to_string(), value).await?;
|
||||
}
|
||||
_ => {
|
||||
return Err(Box::new(err));
|
||||
}
|
||||
},
|
||||
Ok(None) => {
|
||||
handle_not_found(key.to_string(), value).await?;
|
||||
}
|
||||
Ok(Some(mut record)) => {
|
||||
record.value = value;
|
||||
record
|
||||
.into_active_model()
|
||||
.update(&*self.connection)
|
||||
.await
|
||||
.map_err(|err| err.into_service_error())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ chrono = { version = "0.4", features = ["serde"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tokio = { version = "1.47.0", features = ["full"] }
|
||||
sea-orm = { version = "2.0.0-rc", features = [ "sqlx-postgres", "sqlx-mysql", "sqlx-sqlite", "runtime-tokio-rustls", "macros", "mock", "with-chrono", "with-json", "with-uuid", "sqlite-use-returning-for-3_35", "mariadb-use-returning" ] }
|
||||
sea-orm = { workspace = true, features = [ "sqlx-postgres", "sqlx-mysql", "sqlx-sqlite", "runtime-tokio-rustls", "macros", "mock", "with-chrono", "with-json", "with-uuid", "sqlite-use-returning-for-3_35", "mariadb-use-returning" ] }
|
||||
log = "0.4.28"
|
||||
|
||||
[lints]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use log::LevelFilter;
|
||||
pub use sea_orm::ConnectOptions;
|
||||
use sea_orm::ConnectOptions;
|
||||
pub mod generated;
|
||||
|
||||
pub async fn get_connection<T: FnOnce(&mut ConnectOptions)>(
|
||||
|
||||
@@ -11,11 +11,11 @@ path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] }
|
||||
sea-orm-cli = { version = "2.0.0-rc", features = ["sqlx-postgres", "sqlx-mysql", "sqlx-sqlite", "runtime-tokio"] }
|
||||
sea-orm-cli = { workspace = true, features = ["sqlx-postgres", "sqlx-mysql", "sqlx-sqlite", "runtime-tokio"] }
|
||||
log = "0.4.28"
|
||||
|
||||
[dependencies.sea-orm-migration]
|
||||
version = "2.0.0-rc"
|
||||
workspace = true
|
||||
features = [
|
||||
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
|
||||
"sqlx-postgres", "sqlx-mysql", "sqlx-sqlite" # `DATABASE_DRIVER` features
|
||||
|
||||
Reference in New Issue
Block a user