Merge remote-tracking branch 'origin/main' into combined_tables_2

This commit is contained in:
Dessalines 2024-12-12 09:39:10 -05:00
commit 1e6b3fbf27
12 changed files with 32 additions and 107 deletions

View file

@ -1,11 +1,7 @@
{ {
# settings related to the postgresql database # settings related to the postgresql database
database: { database: {
# Configure the database by specifying a URI # Configure the database by specifying URI pointing to a postgres instance
#
# This is the preferred method to specify database connection details since
# it is the most flexible.
# Connection URI pointing to a postgres instance
# #
# This example uses peer authentication to obviate the need for creating, # This example uses peer authentication to obviate the need for creating,
# configuring, and managing passwords. # configuring, and managing passwords.
@ -14,25 +10,7 @@
# PostgreSQL's documentation. # PostgreSQL's documentation.
# #
# [0]: https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6 # [0]: https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6
uri: "postgresql:///lemmy?user=lemmy&host=/var/run/postgresql" connection: "postgres://lemmy:password@localhost:5432/lemmy"
# or
# Configure the database by specifying parts of a URI
#
# Note that specifying the `uri` field should be preferred since it provides
# greater control over how the connection is made. This merely exists for
# backwards-compatibility.
# Username to connect to postgres
user: "string"
# Password to connect to postgres
password: "string"
# Host where postgres is running
host: "string"
# Port where postgres can be accessed
port: 123
# Name of the postgres database for lemmy
database: "string"
# Maximum number of active sql connections # Maximum number of active sql connections
pool_size: 30 pool_size: 30
} }

View file

@ -76,7 +76,7 @@ use tracing::warn;
use url::{ParseError, Url}; use url::{ParseError, Url};
use urlencoding::encode; use urlencoding::encode;
pub static AUTH_COOKIE_NAME: &str = "jwt"; pub const AUTH_COOKIE_NAME: &str = "jwt";
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn is_mod_or_admin( pub async fn is_mod_or_admin(

View file

@ -44,9 +44,9 @@ const MAX_SUCCESSFULS: usize = 1000;
/// in prod mode, try to collect multiple send results at the same time to reduce load /// in prod mode, try to collect multiple send results at the same time to reduce load
#[cfg(not(test))] #[cfg(not(test))]
static MIN_ACTIVITY_SEND_RESULTS_TO_HANDLE: usize = 4; const MIN_ACTIVITY_SEND_RESULTS_TO_HANDLE: usize = 4;
#[cfg(test)] #[cfg(test)]
static MIN_ACTIVITY_SEND_RESULTS_TO_HANDLE: usize = 0; const MIN_ACTIVITY_SEND_RESULTS_TO_HANDLE: usize = 0;
/// ///
/// SendManager --(has many)--> InstanceWorker --(has many)--> SendRetryTask /// SendManager --(has many)--> InstanceWorker --(has many)--> SendRetryTask

View file

@ -3,14 +3,12 @@ use anyhow::{anyhow, Context};
use deser_hjson::from_str; use deser_hjson::from_str;
use regex::Regex; use regex::Regex;
use std::{env, fs, io::Error, sync::LazyLock}; use std::{env, fs, io::Error, sync::LazyLock};
use structs::{PictrsConfig, PictrsImageMode, Settings};
use url::Url; use url::Url;
use urlencoding::encode;
pub mod structs; pub mod structs;
use structs::{DatabaseConnection, PictrsConfig, PictrsImageMode, Settings}; const DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
#[allow(clippy::expect_used)] #[allow(clippy::expect_used)]
pub static SETTINGS: LazyLock<Settings> = LazyLock::new(|| { pub static SETTINGS: LazyLock<Settings> = LazyLock::new(|| {
@ -51,20 +49,9 @@ impl Settings {
pub fn get_database_url(&self) -> String { pub fn get_database_url(&self) -> String {
if let Ok(url) = env::var("LEMMY_DATABASE_URL") { if let Ok(url) = env::var("LEMMY_DATABASE_URL") {
return url; url
} } else {
match &self.database.connection { self.database.connection.clone()
DatabaseConnection::Uri { uri } => uri.clone(),
DatabaseConnection::Parts(parts) => {
format!(
"postgres://{}:{}@{}:{}/{}",
encode(&parts.user),
encode(&parts.password),
parts.host,
parts.port,
encode(&parts.database),
)
}
} }
} }

View file

@ -132,64 +132,24 @@ pub enum PictrsImageMode {
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(default)] #[serde(default)]
pub struct DatabaseConfig { pub struct DatabaseConfig {
#[serde(flatten, default)] /// Configure the database by specifying URI pointing to a postgres instance
pub(crate) connection: DatabaseConnection, ///
/// This example uses peer authentication to obviate the need for creating,
/// configuring, and managing passwords.
///
/// For an explanation of how to use connection URIs, see [here][0] in
/// PostgreSQL's documentation.
///
/// [0]: https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6
#[default("postgres://lemmy:password@localhost:5432/lemmy")]
#[doku(example = "postgresql:///lemmy?user=lemmy&host=/var/run/postgresql")]
pub(crate) connection: String,
/// Maximum number of active sql connections /// Maximum number of active sql connections
#[default(30)] #[default(30)]
pub pool_size: usize, pub pool_size: usize,
} }
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(untagged)]
pub enum DatabaseConnection {
/// Configure the database by specifying a URI
///
/// This is the preferred method to specify database connection details since
/// it is the most flexible.
Uri {
/// Connection URI pointing to a postgres instance
///
/// This example uses peer authentication to obviate the need for creating,
/// configuring, and managing passwords.
///
/// For an explanation of how to use connection URIs, see [here][0] in
/// PostgreSQL's documentation.
///
/// [0]: https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6
#[doku(example = "postgresql:///lemmy?user=lemmy&host=/var/run/postgresql")]
uri: String,
},
/// Configure the database by specifying parts of a URI
///
/// Note that specifying the `uri` field should be preferred since it provides
/// greater control over how the connection is made. This merely exists for
/// backwards-compatibility.
#[default]
Parts(DatabaseConnectionParts),
}
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(default)]
pub struct DatabaseConnectionParts {
/// Username to connect to postgres
#[default("lemmy")]
pub(super) user: String,
/// Password to connect to postgres
#[default("password")]
pub(super) password: String,
#[default("localhost")]
/// Host where postgres is running
pub(super) host: String,
/// Port where postgres can be accessed
#[default(5432)]
pub(super) port: i32,
/// Name of the postgres database for lemmy
#[default("lemmy")]
pub(super) database: String,
}
#[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)] #[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct EmailConfig { pub struct EmailConfig {

View file

@ -8,7 +8,7 @@
site_name: lemmy-alpha site_name: lemmy-alpha
} }
database: { database: {
host: postgres_alpha connection: "postgres://lemmy:password@postgres_alpha:5432/lemmy"
} }
pictrs: { pictrs: {
api_key: "my-pictrs-key" api_key: "my-pictrs-key"

View file

@ -8,7 +8,7 @@
site_name: lemmy-beta site_name: lemmy-beta
} }
database: { database: {
host: postgres_beta connection: "postgres://lemmy:password@postgres_beta:5432/lemmy"
} }
pictrs: { pictrs: {
api_key: "my-pictrs-key" api_key: "my-pictrs-key"

View file

@ -8,6 +8,6 @@
site_name: lemmy-delta site_name: lemmy-delta
} }
database: { database: {
host: postgres_delta connection: "postgres://lemmy:password@postgres_delta:5432/lemmy"
} }
} }

View file

@ -8,7 +8,7 @@
site_name: lemmy-epsilon site_name: lemmy-epsilon
} }
database: { database: {
host: postgres_epsilon connection: "postgres://lemmy:password@postgres_epsilon:5432/lemmy"
} }
pictrs: { pictrs: {
api_key: "my-pictrs-key" api_key: "my-pictrs-key"

View file

@ -8,7 +8,7 @@
site_name: lemmy-gamma site_name: lemmy-gamma
} }
database: { database: {
host: postgres_gamma connection: "postgres://lemmy:password@postgres_gamma:5432/lemmy"
} }
pictrs: { pictrs: {
api_key: "my-pictrs-key" api_key: "my-pictrs-key"

View file

@ -11,7 +11,7 @@
site_name: "lemmy-dev" site_name: "lemmy-dev"
} }
database: { database: {
host: postgres connection: "postgres://lemmy:password@postgres:5432/lemmy"
} }
hostname: "localhost" hostname: "localhost"

View file

@ -579,13 +579,13 @@ async fn build_update_instance_form(
// This is the only kind of error that means the instance is dead // This is the only kind of error that means the instance is dead
return None; return None;
}; };
let status = res.status();
if status.is_client_error() || status.is_server_error() {
return None;
}
// In this block, returning `None` is ignored, and only means not writing nodeinfo to db // In this block, returning `None` is ignored, and only means not writing nodeinfo to db
async { async {
if res.status().is_client_error() {
return None;
}
let node_info_url = res let node_info_url = res
.json::<NodeInfoWellKnown>() .json::<NodeInfoWellKnown>()
.await .await