1
0
Fork 0
forked from gorb/backend

feat: add bunny-api-tokio

This commit is contained in:
Radical 2025-05-20 14:54:34 +02:00
parent a676962316
commit cf333b4eba
3 changed files with 53 additions and 3 deletions

View file

@ -30,6 +30,8 @@ uuid = { version = "1.16", features = ["serde", "v7"] }
random-string = "1.1" random-string = "1.1"
actix-ws = "0.3.0" actix-ws = "0.3.0"
futures-util = "0.3.31" futures-util = "0.3.31"
bunny-api-tokio = "0.2.1"
bindet = "0.3.2"
[dependencies.tokio] [dependencies.tokio]
version = "1.44" version = "1.44"

View file

@ -1,14 +1,17 @@
use crate::Error; use crate::Error;
use bunny_api_tokio::edge_storage::Endpoint;
use log::debug; use log::debug;
use serde::Deserialize; use serde::Deserialize;
use sqlx::postgres::PgConnectOptions; use sqlx::postgres::PgConnectOptions;
use tokio::fs::read_to_string; use tokio::fs::read_to_string;
use url::Url;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct ConfigBuilder { pub struct ConfigBuilder {
database: Database, database: Database,
cache_database: CacheDatabase, cache_database: CacheDatabase,
web: Option<WebBuilder>, web: Option<WebBuilder>,
bunny: BunnyBuilder,
} }
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
@ -36,6 +39,14 @@ struct WebBuilder {
_ssl: Option<bool>, _ssl: Option<bool>,
} }
#[derive(Debug, Deserialize)]
struct BunnyBuilder {
api_key: String,
endpoint: String,
storage_zone: String,
cdn_url: Url,
}
impl ConfigBuilder { impl ConfigBuilder {
pub async fn load(path: String) -> Result<Self, Error> { pub async fn load(path: String) -> Result<Self, Error> {
debug!("loading config from: {}", path); debug!("loading config from: {}", path);
@ -59,10 +70,31 @@ impl ConfigBuilder {
} }
}; };
let endpoint = match &*self.bunny.endpoint {
"Frankfurt" => Endpoint::Frankfurt,
"London" => Endpoint::London,
"New York" => Endpoint::NewYork,
"Los Angeles" => Endpoint::LosAngeles,
"Singapore" => Endpoint::Singapore,
"Stockholm" => Endpoint::Stockholm,
"Sao Paulo" => Endpoint::SaoPaulo,
"Johannesburg" => Endpoint::Johannesburg,
"Sydney" => Endpoint::Sydney,
url => Endpoint::Custom(url.to_string()),
};
let bunny = Bunny {
api_key: self.bunny.api_key,
endpoint,
storage_zone: self.bunny.storage_zone,
cdn_url: self.bunny.cdn_url,
};
Config { Config {
database: self.database, database: self.database,
cache_database: self.cache_database, cache_database: self.cache_database,
web, web,
bunny,
} }
} }
} }
@ -72,6 +104,7 @@ pub struct Config {
pub database: Database, pub database: Database,
pub cache_database: CacheDatabase, pub cache_database: CacheDatabase,
pub web: Web, pub web: Web,
pub bunny: Bunny,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -80,6 +113,14 @@ pub struct Web {
pub port: u16, pub port: u16,
} }
#[derive(Debug, Clone)]
pub struct Bunny {
pub api_key: String,
pub endpoint: Endpoint,
pub storage_zone: String,
pub cdn_url: Url,
}
impl Database { impl Database {
pub fn connect_options(&self) -> PgConnectOptions { pub fn connect_options(&self) -> PgConnectOptions {
PgConnectOptions::new() PgConnectOptions::new()

View file

@ -25,9 +25,10 @@ struct Args {
pub struct Data { pub struct Data {
pub pool: Pool<Postgres>, pub pool: Pool<Postgres>,
pub cache_pool: redis::Client, pub cache_pool: redis::Client,
pub _config: Config, pub config: Config,
pub argon2: Argon2<'static>, pub argon2: Argon2<'static>,
pub start_time: SystemTime, pub start_time: SystemTime,
pub bunny_cdn: bunny_api_tokio::Client,
} }
#[tokio::main] #[tokio::main]
@ -48,6 +49,10 @@ async fn main() -> Result<(), Error> {
let cache_pool = redis::Client::open(config.cache_database.url())?; let cache_pool = redis::Client::open(config.cache_database.url())?;
let mut bunny_cdn = bunny_api_tokio::Client::new(config.bunny.api_key.clone()).await?;
bunny_cdn.storage.init(config.bunny.endpoint.clone(), config.bunny.storage_zone.clone())?;
/* /*
TODO: Figure out if a table should be used here and if not then what. TODO: Figure out if a table should be used here and if not then what.
Also figure out if these should be different types from what they currently are and if we should add more "constraints" Also figure out if these should be different types from what they currently are and if we should add more "constraints"
@ -94,7 +99,8 @@ async fn main() -> Result<(), Error> {
uuid uuid PRIMARY KEY NOT NULL, uuid uuid PRIMARY KEY NOT NULL,
owner_uuid uuid NOT NULL REFERENCES users(uuid), owner_uuid uuid NOT NULL REFERENCES users(uuid),
name VARCHAR(100) NOT NULL, name VARCHAR(100) NOT NULL,
description VARCHAR(300) description VARCHAR(300),
icon VARCHAR(100) DEFAULT NULL
); );
CREATE TABLE IF NOT EXISTS guild_members ( CREATE TABLE IF NOT EXISTS guild_members (
uuid uuid PRIMARY KEY NOT NULL, uuid uuid PRIMARY KEY NOT NULL,
@ -164,10 +170,11 @@ async fn main() -> Result<(), Error> {
let data = Data { let data = Data {
pool, pool,
cache_pool, cache_pool,
_config: config, config,
// TODO: Possibly implement "pepper" into this (thinking it could generate one if it doesnt exist and store it on disk) // TODO: Possibly implement "pepper" into this (thinking it could generate one if it doesnt exist and store it on disk)
argon2: Argon2::default(), argon2: Argon2::default(),
start_time: SystemTime::now(), start_time: SystemTime::now(),
bunny_cdn,
}; };
HttpServer::new(move || { HttpServer::new(move || {