use rpm flag when creating a user

This commit is contained in:
Bryan Stitt 2022-08-16 17:47:04 +00:00
parent 5c95338573
commit d82fae161f
2 changed files with 20 additions and 10 deletions

View File

@ -2,12 +2,12 @@ use anyhow::Context;
use argh::FromArgs;
use entities::{user, user_keys};
use ethers::prelude::Address;
use sea_orm::ActiveModelTrait;
use sea_orm::{ActiveModelTrait, TransactionTrait};
use tracing::info;
use uuid::Uuid;
use web3_proxy::users::new_api_key;
fn default_rpm() -> usize {
fn default_rpm() -> u32 {
6_000_000
}
@ -30,11 +30,13 @@ pub struct CreateUserSubCommand {
#[argh(option, default = "default_rpm()")]
/// maximum requests per minute
rpm: usize,
rpm: u32,
}
impl CreateUserSubCommand {
pub async fn main(self, db: &sea_orm::DatabaseConnection) -> anyhow::Result<()> {
let txn = db.begin().await?;
// TODO: would be nice to use the fixed array instead of a Vec in the entities
let address = self
.address
@ -49,23 +51,29 @@ impl CreateUserSubCommand {
..Default::default()
};
let u = u.insert(db).await.context("Failed saving new user")?;
let u = u.save(&txn).await.context("Failed saving new user")?;
info!("user #{}: {:?}", u.id, Address::from_slice(&u.address));
info!(
"user #{}: {:?}",
u.id.as_ref(),
Address::from_slice(u.address.as_ref())
);
// create a key for the new user
// TODO: requests_per_minute should be configurable
let uk = user_keys::ActiveModel {
user_id: sea_orm::Set(u.id),
user_id: u.id,
api_key: sea_orm::Set(self.api_key),
requests_per_minute: sea_orm::Set(6_000_000),
requests_per_minute: sea_orm::Set(self.rpm),
..Default::default()
};
// TODO: if this fails, rever adding the user, too
let uk = uk.insert(db).await.context("Failed saving new user key")?;
let uk = uk.save(&txn).await.context("Failed saving new user key")?;
info!("user key: {}", uk.api_key);
txn.commit().await?;
info!("user key: {}", uk.api_key.as_ref());
Ok(())
}

View File

@ -21,7 +21,7 @@ use std::sync::Arc;
use crate::app::Web3ProxyApp;
use super::{rate_limit::RateLimitResult, errors::anyhow_error_into_response};
use super::{errors::anyhow_error_into_response, rate_limit::RateLimitResult};
pub async fn create_user(
// this argument tells axum to parse the request body
@ -65,6 +65,8 @@ pub async fn create_user(
// TODO: proper error message
let user = user.insert(db).await.unwrap();
//
// TODO: do not expose user ids
(StatusCode::CREATED, Json(user)).into_response()
}