Add a command to roll a random value

This commit is contained in:
2023-04-11 04:37:21 +02:00
parent 46d3b28aec
commit 279b70e063
6 changed files with 226 additions and 155 deletions

65
src/lib.rs Normal file
View File

@@ -0,0 +1,65 @@
use std::str::FromStr;
use anyhow::bail;
use axum::extract::FromRef;
use base64::{engine, alphabet, Engine};
use ed25519_dalek::VerifyingKey;
use sqlx::PgPool;
use twilight_model::id::{marker::ApplicationMarker, Id};
pub mod database;
pub mod discord;
#[derive(Clone)]
pub struct Config {
pub discord_client_id: Id<ApplicationMarker>,
discord_client_secret: String,
discord_pub_key: VerifyingKey,
pub database_url: String,
pub listen_port: u16,
}
impl Config {
pub fn configure() -> anyhow::Result<Self> {
dotenvy::dotenv().ok();
let pub_key_bytes: Vec<u8> = hex::decode(std::env::var("DISCORD_PUB_KEY")?)?;
let pub_key: [u8; 32] = match pub_key_bytes.try_into() {
Ok(pk) => pk,
Err(_) => bail!("Invalid discord public key"),
};
Ok(Config {
discord_client_id: Id::from_str(std::env::var("DISCORD_CLIENT_ID")?.as_str())?,
discord_client_secret: std::env::var("DISCORD_CLIENT_SECRET")?,
discord_pub_key: VerifyingKey::from_bytes(&pub_key)?,
database_url: std::env::var("DATABASE_URL")?,
listen_port: std::env::var("LISTEN_PORT")?.parse()?,
})
}
pub fn authorization(&self) -> String {
let engine = engine::GeneralPurpose::new(&alphabet::STANDARD, engine::general_purpose::PAD);
let auth = format!("{}:{}", self.discord_client_id, self.discord_client_secret);
engine.encode(auth)
}
}
#[derive(Clone)]
pub struct AppState {
pub config: Config,
pub pg_pool: PgPool,
}
impl FromRef<AppState> for PgPool {
fn from_ref(app_state: &AppState) -> Self {
app_state.pg_pool.clone()
}
}
impl FromRef<AppState> for VerifyingKey {
fn from_ref(app_state: &AppState) -> Self {
app_state.config.discord_pub_key
}
}