mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-12 00:30:11 +00:00
Merge remote-tracking branch 'upstream/main' into rust-command-parser
This commit is contained in:
commit
a29ed2bda0
28 changed files with 446 additions and 193 deletions
|
|
@ -127,13 +127,10 @@ fn router(ctx: ApiContext) -> Router {
|
|||
.route("/v2/groups/{group_id}/oembed.json", get(rproxy))
|
||||
|
||||
.layer(middleware::ratelimit::ratelimiter(middleware::ratelimit::do_request_ratelimited)) // this sucks
|
||||
|
||||
.layer(axum::middleware::from_fn(middleware::ignore_invalid_routes::ignore_invalid_routes))
|
||||
.layer(axum::middleware::from_fn(middleware::logger::logger))
|
||||
|
||||
.layer(axum::middleware::from_fn_with_state(ctx.clone(), middleware::params::params))
|
||||
.layer(axum::middleware::from_fn_with_state(ctx.clone(), middleware::auth::auth))
|
||||
|
||||
.layer(axum::middleware::from_fn(middleware::logger::logger))
|
||||
.layer(axum::middleware::from_fn(middleware::cors::cors))
|
||||
.layer(tower_http::catch_panic::CatchPanicLayer::custom(util::handle_panic))
|
||||
|
||||
|
|
|
|||
|
|
@ -76,5 +76,10 @@ pub async fn auth(State(ctx): State<ApiContext>, mut req: Request, next: Next) -
|
|||
req.extensions_mut()
|
||||
.insert(AuthState::new(authed_system_id, authed_app_id, internal));
|
||||
|
||||
next.run(req).await
|
||||
let mut res = next.run(req).await;
|
||||
|
||||
res.extensions_mut()
|
||||
.insert(AuthState::new(authed_system_id, authed_app_id, internal));
|
||||
|
||||
res
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,9 +12,10 @@ const MIN_LOG_TIME: u128 = 2_000;
|
|||
|
||||
pub async fn logger(request: Request, next: Next) -> Response {
|
||||
let method = request.method().clone();
|
||||
let headers = request.headers().clone();
|
||||
|
||||
let remote_ip = header_or_unknown(request.headers().get("X-PluralKit-Client-IP"));
|
||||
let user_agent = header_or_unknown(request.headers().get("User-Agent"));
|
||||
let remote_ip = header_or_unknown(headers.get("X-PluralKit-Client-IP"));
|
||||
let user_agent = header_or_unknown(headers.get("User-Agent"));
|
||||
|
||||
let extensions = request.extensions().clone();
|
||||
|
||||
|
|
@ -24,10 +25,6 @@ pub async fn logger(request: Request, next: Next) -> Response {
|
|||
.map(|v| v.as_str().to_string())
|
||||
.unwrap_or("unknown".to_string());
|
||||
|
||||
let auth = extensions
|
||||
.get::<AuthState>()
|
||||
.expect("should always have AuthState");
|
||||
|
||||
let uri = request.uri().clone();
|
||||
|
||||
let request_span = span!(
|
||||
|
|
@ -43,15 +40,24 @@ pub async fn logger(request: Request, next: Next) -> Response {
|
|||
let response = next.run(request).instrument(request_span).await;
|
||||
let elapsed = start.elapsed().as_millis();
|
||||
|
||||
let system_id = auth
|
||||
.system_id()
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or("none".to_string());
|
||||
let rext = response.extensions().clone();
|
||||
let auth = rext.get::<AuthState>();
|
||||
|
||||
let app_id = auth
|
||||
.app_id()
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or("none".to_string());
|
||||
let system_id = if let Some(auth) = auth {
|
||||
auth.system_id()
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or("none".to_string())
|
||||
} else {
|
||||
"none".to_string()
|
||||
};
|
||||
|
||||
let app_id = if let Some(auth) = auth {
|
||||
auth.app_id()
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or("none".to_string())
|
||||
} else {
|
||||
"none".to_string()
|
||||
};
|
||||
|
||||
counter!(
|
||||
"pluralkit_api_requests",
|
||||
|
|
@ -73,6 +79,14 @@ pub async fn logger(request: Request, next: Next) -> Response {
|
|||
.record(elapsed as f64 / 1_000_f64);
|
||||
|
||||
info!(
|
||||
status = response.status().as_str(),
|
||||
method = method.to_string(),
|
||||
endpoint,
|
||||
elapsed,
|
||||
user_agent,
|
||||
remote_ip,
|
||||
system_id,
|
||||
app_id,
|
||||
"{} handled request for {} {} in {}ms",
|
||||
response.status(),
|
||||
method,
|
||||
|
|
|
|||
14
crates/app-commands/Cargo.toml
Normal file
14
crates/app-commands/Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "app-commands"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
libpk = { path = "../libpk" }
|
||||
anyhow = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
twilight-http = { workspace = true }
|
||||
twilight-model = { workspace = true }
|
||||
twilight-util = { workspace = true }
|
||||
41
crates/app-commands/src/main.rs
Normal file
41
crates/app-commands/src/main.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use twilight_model::{
|
||||
application::command::{Command, CommandType},
|
||||
guild::IntegrationApplication,
|
||||
};
|
||||
use twilight_util::builder::command::CommandBuilder;
|
||||
|
||||
#[libpk::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let discord = twilight_http::Client::builder()
|
||||
.token(
|
||||
libpk::config
|
||||
.discord
|
||||
.as_ref()
|
||||
.expect("missing discord config")
|
||||
.bot_token
|
||||
.clone(),
|
||||
)
|
||||
.build();
|
||||
|
||||
let interaction = discord.interaction(twilight_model::id::Id::new(
|
||||
libpk::config
|
||||
.discord
|
||||
.as_ref()
|
||||
.expect("missing discord config")
|
||||
.client_id
|
||||
.clone()
|
||||
.get(),
|
||||
));
|
||||
|
||||
let commands = vec![
|
||||
// message commands
|
||||
// description must be empty string
|
||||
CommandBuilder::new("\u{2753} Message info", "", CommandType::Message).build(),
|
||||
CommandBuilder::new("\u{274c} Delete message", "", CommandType::Message).build(),
|
||||
CommandBuilder::new("\u{1f514} Ping author", "", CommandType::Message).build(),
|
||||
];
|
||||
|
||||
interaction.set_global_commands(&commands).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue