mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-08 14:57:54 +00:00
feat(api): update rust deps, move /private/meta endpoint to rust-api
This commit is contained in:
parent
f14c421e23
commit
e415c6704f
20 changed files with 1835 additions and 244 deletions
|
|
@ -6,12 +6,20 @@ edition = "2021"
|
|||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
config = "0.13.3"
|
||||
fred = { workspace = true }
|
||||
gethostname = "0.4.1"
|
||||
lazy_static = { workspace = true }
|
||||
metrics = { workspace = true }
|
||||
metrics-exporter-prometheus = { version = "0.11.0", default-features = false, features = ["tokio", "http-listener", "tracing"] }
|
||||
serde = { workspace = true }
|
||||
sqlx = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tracing-gelf = "0.7.1"
|
||||
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
|
||||
|
||||
prost = { workspace = true }
|
||||
prost-types = { workspace = true }
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = { workspace = true }
|
||||
|
|
|
|||
8
lib/libpk/build.rs
Normal file
8
lib/libpk/build.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use std::io::Result;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
prost_build::Config::new()
|
||||
.type_attribute(".ShardState", "#[derive(serde::Serialize)]")
|
||||
.compile_protos(&["../../proto/state.proto"], &["../../proto/"])?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -12,9 +12,11 @@ pub struct DiscordConfig {
|
|||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct DatabaseConfig {
|
||||
pub(crate) _data_db_uri: String,
|
||||
pub(crate) _messages_db_uri: String,
|
||||
pub(crate) _db_password: Option<String>,
|
||||
pub(crate) data_db_uri: String,
|
||||
pub(crate) data_db_max_connections: Option<u32>,
|
||||
pub(crate) data_db_min_connections: Option<u32>,
|
||||
// pub(crate) _messages_db_uri: String,
|
||||
pub(crate) db_password: Option<String>,
|
||||
pub data_redis_addr: String,
|
||||
}
|
||||
|
||||
|
|
@ -42,6 +44,8 @@ fn _metrics_default() -> bool {
|
|||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct PKConfig {
|
||||
pub db: DatabaseConfig,
|
||||
|
||||
pub discord: DiscordConfig,
|
||||
pub api: ApiConfig,
|
||||
|
||||
|
|
|
|||
42
lib/libpk/src/db/mod.rs
Normal file
42
lib/libpk/src/db/mod.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use fred::pool::RedisPool;
|
||||
use sqlx::postgres::{PgConnectOptions, PgPool, PgPoolOptions};
|
||||
use std::str::FromStr;
|
||||
use tracing::info;
|
||||
|
||||
pub mod repository;
|
||||
|
||||
pub async fn init_redis() -> anyhow::Result<RedisPool> {
|
||||
info!("connecting to redis");
|
||||
let redis = fred::pool::RedisPool::new(
|
||||
fred::types::RedisConfig::from_url_centralized(crate::config.db.data_redis_addr.as_ref())
|
||||
.expect("redis url is invalid"),
|
||||
10,
|
||||
)?;
|
||||
|
||||
let redis_handle = redis.connect(Some(fred::types::ReconnectPolicy::default()));
|
||||
tokio::spawn(async move { redis_handle });
|
||||
|
||||
Ok(redis)
|
||||
}
|
||||
|
||||
pub async fn init_data_db() -> anyhow::Result<PgPool> {
|
||||
info!("connecting to database");
|
||||
|
||||
let mut options = PgConnectOptions::from_str(&crate::config.db.data_db_uri)?;
|
||||
|
||||
if let Some(password) = crate::config.db.db_password.clone() {
|
||||
options = options.password(&password);
|
||||
}
|
||||
|
||||
let mut pool = PgPoolOptions::new();
|
||||
|
||||
if let Some(max_conns) = crate::config.db.data_db_max_connections {
|
||||
pool = pool.max_connections(max_conns);
|
||||
}
|
||||
|
||||
if let Some(min_conns) = crate::config.db.data_db_min_connections {
|
||||
pool = pool.min_connections(min_conns);
|
||||
}
|
||||
|
||||
Ok(pool.connect_with(options).await?)
|
||||
}
|
||||
2
lib/libpk/src/db/repository/mod.rs
Normal file
2
lib/libpk/src/db/repository/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
mod stats;
|
||||
pub use stats::*;
|
||||
13
lib/libpk/src/db/repository/stats.rs
Normal file
13
lib/libpk/src/db/repository/stats.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
pub async fn get_stats(pool: &sqlx::postgres::PgPool) -> anyhow::Result<Counts> {
|
||||
let counts: Counts = sqlx::query_as("select * from info").fetch_one(pool).await?;
|
||||
Ok(counts)
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, sqlx::FromRow)]
|
||||
pub struct Counts {
|
||||
pub system_count: i64,
|
||||
pub member_count: i64,
|
||||
pub group_count: i64,
|
||||
pub switch_count: i64,
|
||||
pub message_count: i64,
|
||||
}
|
||||
|
|
@ -2,7 +2,10 @@ use gethostname::gethostname;
|
|||
use metrics_exporter_prometheus::PrometheusBuilder;
|
||||
use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, EnvFilter, Registry};
|
||||
|
||||
mod _config;
|
||||
pub mod db;
|
||||
pub mod proto;
|
||||
|
||||
pub mod _config;
|
||||
pub use crate::_config::CONFIG as config;
|
||||
|
||||
pub fn init_logging(component: &str) -> anyhow::Result<()> {
|
||||
|
|
|
|||
1
lib/libpk/src/proto.rs
Normal file
1
lib/libpk/src/proto.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
include!(concat!(env!("OUT_DIR"), "/_.rs"));
|
||||
Loading…
Add table
Add a link
Reference in a new issue