diff --git a/Cargo.lock b/Cargo.lock index efcf2cb1..1274b75a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6517,6 +6517,7 @@ dependencies = [ "reqwest 0.12.23", "revolt-coalesced", "revolt-config", + "revolt-database", "revolt-models", "revolt-result", "serde", diff --git a/crates/core/database/src/models/users/axum.rs b/crates/core/database/src/models/users/axum.rs index a6d8b5a3..6b1f2f32 100644 --- a/crates/core/database/src/models/users/axum.rs +++ b/crates/core/database/src/models/users/axum.rs @@ -1,21 +1,20 @@ -use axum::{ - extract::{FromRef, FromRequestParts}, - http::request::Parts, -}; +use axum::{extract::{FromRef, FromRequestParts}, http::request::Parts}; use revolt_result::{create_error, Error, Result}; use crate::{Database, User}; #[async_trait::async_trait] -impl FromRequestParts for User +impl FromRequestParts for User where Database: FromRef, + S: Send + Sync { type Rejection = Error; async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { let db = Database::from_ref(state); + if let Some(Ok(bot_token)) = parts.headers.get("x-bot-token").map(|v| v.to_str()) { let bot = db.fetch_bot_by_token(bot_token).await?; db.fetch_user(&bot.id).await diff --git a/crates/services/gifbox/Cargo.toml b/crates/services/gifbox/Cargo.toml index d2e65145..94cd0732 100644 --- a/crates/services/gifbox/Cargo.toml +++ b/crates/services/gifbox/Cargo.toml @@ -23,7 +23,7 @@ revolt-result = { version = "0.8.8", path = "../../core/result", features = [ "axum", ] } revolt-coalesced = { version = "0.8.8", path = "../../core/coalesced" } - +revolt-database = { version = "0.8.8", path = "../../core/database", features = ["axum-impl"] } # Axum / web server axum = { version = "0.7.5" } axum-extra = { version = "0.9", features = ["typed-header"] } diff --git a/crates/services/gifbox/src/api.rs b/crates/services/gifbox/src/api.rs index c137e7a3..07883652 100644 --- a/crates/services/gifbox/src/api.rs +++ b/crates/services/gifbox/src/api.rs @@ -4,10 +4,11 @@ use axum::{extract::{Query, State}, routing::get, Json, Router}; use revolt_result::{create_error, Result}; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; +use revolt_database::User; -use crate::tenor::{Tenor, types}; +use crate::{AppState, tenor::{Tenor, types}}; -pub async fn router() -> Router { +pub async fn router() -> Router { Router::new() .route("/", get(root)) .route("/search", get(search)) @@ -53,6 +54,7 @@ struct SearchQueryParams { ) )] async fn search( + _user: User, Query(params): Query, State(tenor): State, ) -> Result>> { diff --git a/crates/services/gifbox/src/main.rs b/crates/services/gifbox/src/main.rs index 32d1ea58..3cfb2e95 100644 --- a/crates/services/gifbox/src/main.rs +++ b/crates/services/gifbox/src/main.rs @@ -1,8 +1,9 @@ use std::net::{Ipv4Addr, SocketAddr}; -use axum::Router; +use axum::{extract::FromRef, Router}; use revolt_config::config; +use revolt_database::{Database, DatabaseInfo}; use tokio::net::TcpListener; use utoipa::{ openapi::security::{Http, HttpAuthScheme, SecurityScheme}, @@ -10,9 +11,29 @@ use utoipa::{ }; use utoipa_scalar::{Scalar, Servable as ScalarServable}; +use crate::tenor::Tenor; + mod api; mod tenor; +#[derive(Clone)] +struct AppState { + pub database: Database, + pub tenor: Tenor +} + +impl FromRef for Database { + fn from_ref(state: &AppState) -> Self { + state.database.clone() + } +} + +impl FromRef for Tenor { + fn from_ref(state: &AppState) -> Self { + state.tenor.clone() + } +} + #[tokio::main] async fn main() -> Result<(), std::io::Error> { // Configure logging and environment @@ -48,13 +69,16 @@ async fn main() -> Result<(), std::io::Error> { } let config = config().await; - let tenor = tenor::Tenor::new(&config.api.security.tenor_key); + let state = AppState { + database: DatabaseInfo::Auto.connect().await.expect("Unable to connect to database"), + tenor: tenor::Tenor::new(&config.api.security.tenor_key) + }; // Configure Axum and router let app = Router::new() .merge(Scalar::with_url("/scalar", ApiDoc::openapi())) .nest("/", api::router().await) - .with_state(tenor); + .with_state(state); // Configure TCP listener and bind tracing::info!("Listening on 0.0.0.0:14706");