diff --git a/Cargo.toml b/Cargo.toml index 33d01e7..be98c7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,8 @@ uuid = { version = "1.16", features = ["serde", "v7"] } random-string = "1.1" actix-ws = "0.3.0" futures-util = "0.3.31" +bunny-api-tokio = "0.2.1" +bindet = "0.3.2" [dependencies.tokio] version = "1.44" diff --git a/src/config.rs b/src/config.rs index 65a5965..95818b3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,14 +1,17 @@ use crate::Error; +use bunny_api_tokio::edge_storage::Endpoint; use log::debug; use serde::Deserialize; use sqlx::postgres::PgConnectOptions; use tokio::fs::read_to_string; +use url::Url; #[derive(Debug, Deserialize)] pub struct ConfigBuilder { database: Database, cache_database: CacheDatabase, web: Option, + bunny: BunnyBuilder, } #[derive(Debug, Deserialize, Clone)] @@ -36,6 +39,14 @@ struct WebBuilder { _ssl: Option, } +#[derive(Debug, Deserialize)] +struct BunnyBuilder { + api_key: String, + endpoint: String, + storage_zone: String, + cdn_url: Url, +} + impl ConfigBuilder { pub async fn load(path: String) -> Result { 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 { database: self.database, cache_database: self.cache_database, web, + bunny, } } } @@ -72,6 +104,7 @@ pub struct Config { pub database: Database, pub cache_database: CacheDatabase, pub web: Web, + pub bunny: Bunny, } #[derive(Debug, Clone)] @@ -80,6 +113,14 @@ pub struct Web { 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 { pub fn connect_options(&self) -> PgConnectOptions { PgConnectOptions::new() diff --git a/src/main.rs b/src/main.rs index fbad594..bf918dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,9 +25,10 @@ struct Args { pub struct Data { pub pool: Pool, pub cache_pool: redis::Client, - pub _config: Config, + pub config: Config, pub argon2: Argon2<'static>, pub start_time: SystemTime, + pub bunny_cdn: bunny_api_tokio::Client, } #[tokio::main] @@ -48,6 +49,10 @@ async fn main() -> Result<(), Error> { 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. 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, owner_uuid uuid NOT NULL REFERENCES users(uuid), name VARCHAR(100) NOT NULL, - description VARCHAR(300) + description VARCHAR(300), + icon VARCHAR(100) DEFAULT NULL ); CREATE TABLE IF NOT EXISTS guild_members ( uuid uuid PRIMARY KEY NOT NULL, @@ -164,10 +170,11 @@ async fn main() -> Result<(), Error> { let data = Data { 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) argon2: Argon2::default(), start_time: SystemTime::now(), + bunny_cdn, }; HttpServer::new(move || {