From 60d31aa1b5a39a5bbcc485bb90cca55e8c61a116 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sat, 6 Aug 2022 01:55:18 +0000 Subject: [PATCH] select only --- web3_proxy/src/frontend/mod.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/web3_proxy/src/frontend/mod.rs b/web3_proxy/src/frontend/mod.rs index 678c39a2..6f6ac43c 100644 --- a/web3_proxy/src/frontend/mod.rs +++ b/web3_proxy/src/frontend/mod.rs @@ -13,10 +13,12 @@ use axum::{ }; use entities::user_keys; use reqwest::StatusCode; -use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, QuerySelect}; +use sea_orm::{ + ColumnTrait, DeriveColumn, EntityTrait, EnumIter, IdenStatic, QueryFilter, QuerySelect, +}; use std::net::{IpAddr, SocketAddr}; use std::sync::Arc; -use tracing::debug; +use tracing::info; use uuid::Uuid; use crate::app::Web3ProxyApp; @@ -55,17 +57,23 @@ pub async fn rate_limit_by_key( ) -> Result<(), impl IntoResponse> { let db = app.db_conn(); + #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] + enum QueryAs { + UserId, + } + // query the db to make sure this key is active // TODO: probably want a cache on this match user_keys::Entity::find() - // .select_only() - // .column(user_keys::Column::UserId) + .select_only() + .column_as(user_keys::Column::UserId, QueryAs::UserId) .filter(user_keys::Column::ApiKey.eq(user_key)) .filter(user_keys::Column::Active.eq(true)) + .into_values::<_, QueryAs>() .one(db) .await { - Ok(Some(_)) => { + Ok::, _>(Some(_)) => { // user key is valid if let Some(rate_limiter) = app.rate_limiter() { if rate_limiter