use rpm flag when creating a user
This commit is contained in:
parent
5c95338573
commit
d82fae161f
@ -2,12 +2,12 @@ use anyhow::Context;
|
|||||||
use argh::FromArgs;
|
use argh::FromArgs;
|
||||||
use entities::{user, user_keys};
|
use entities::{user, user_keys};
|
||||||
use ethers::prelude::Address;
|
use ethers::prelude::Address;
|
||||||
use sea_orm::ActiveModelTrait;
|
use sea_orm::{ActiveModelTrait, TransactionTrait};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use web3_proxy::users::new_api_key;
|
use web3_proxy::users::new_api_key;
|
||||||
|
|
||||||
fn default_rpm() -> usize {
|
fn default_rpm() -> u32 {
|
||||||
6_000_000
|
6_000_000
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,11 +30,13 @@ pub struct CreateUserSubCommand {
|
|||||||
|
|
||||||
#[argh(option, default = "default_rpm()")]
|
#[argh(option, default = "default_rpm()")]
|
||||||
/// maximum requests per minute
|
/// maximum requests per minute
|
||||||
rpm: usize,
|
rpm: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CreateUserSubCommand {
|
impl CreateUserSubCommand {
|
||||||
pub async fn main(self, db: &sea_orm::DatabaseConnection) -> anyhow::Result<()> {
|
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
|
// TODO: would be nice to use the fixed array instead of a Vec in the entities
|
||||||
let address = self
|
let address = self
|
||||||
.address
|
.address
|
||||||
@ -49,23 +51,29 @@ impl CreateUserSubCommand {
|
|||||||
..Default::default()
|
..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
|
// create a key for the new user
|
||||||
// TODO: requests_per_minute should be configurable
|
// TODO: requests_per_minute should be configurable
|
||||||
let uk = user_keys::ActiveModel {
|
let uk = user_keys::ActiveModel {
|
||||||
user_id: sea_orm::Set(u.id),
|
user_id: u.id,
|
||||||
api_key: sea_orm::Set(self.api_key),
|
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()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: if this fails, rever adding the user, too
|
// 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use crate::app::Web3ProxyApp;
|
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(
|
pub async fn create_user(
|
||||||
// this argument tells axum to parse the request body
|
// this argument tells axum to parse the request body
|
||||||
@ -65,6 +65,8 @@ pub async fn create_user(
|
|||||||
// TODO: proper error message
|
// TODO: proper error message
|
||||||
let user = user.insert(db).await.unwrap();
|
let user = user.insert(db).await.unwrap();
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
// TODO: do not expose user ids
|
// TODO: do not expose user ids
|
||||||
(StatusCode::CREATED, Json(user)).into_response()
|
(StatusCode::CREATED, Json(user)).into_response()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user