-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): rewrite storage system for easy expandability
- Loading branch information
1 parent
c486cc3
commit ddeb947
Showing
15 changed files
with
276 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE files RENAME COLUMN bucket_id TO storage_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub mod bucket; | ||
pub mod database; | ||
pub mod storage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::ops::Deref; | ||
|
||
use aws_credential_types::Credentials; | ||
use rocket::{ | ||
fairing::{self, Fairing, Info, Kind}, | ||
request::{FromRequest, Outcome}, | ||
Build, Request, Rocket, | ||
}; | ||
use serde::Deserialize; | ||
|
||
use crate::{ | ||
s3::{bucket::Bucket, credentials::BucketCredentials}, | ||
storage::driver::StorageDriver, | ||
}; | ||
|
||
pub struct StorageDriverGuard(pub StorageDriver); | ||
|
||
pub struct StorageDriverFairing; | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum StorageDriverType { | ||
ObjectStorage, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct StorageDriverFairingConfig { | ||
storage_type: StorageDriverType, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct ObjectStorageConfig { | ||
url: String, | ||
name: String, | ||
access_key: String, | ||
access_key_secret: String, | ||
region: Option<String>, | ||
} | ||
|
||
impl Deref for StorageDriverGuard { | ||
type Target = StorageDriver; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl StorageDriverFairing { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
#[rocket::async_trait] | ||
impl Fairing for StorageDriverFairing { | ||
fn info(&self) -> Info { | ||
Info { | ||
name: "Storage Driver Fairing", | ||
kind: Kind::Ignite, | ||
} | ||
} | ||
|
||
async fn on_ignite(&self, rocket: Rocket<Build>) -> fairing::Result { | ||
let driver = match rocket | ||
.figment() | ||
.focus("storage") | ||
.extract::<StorageDriverFairingConfig>() | ||
.expect("Unable to load storage config, is it defined in Rocket.toml?") | ||
.storage_type | ||
{ | ||
StorageDriverType::ObjectStorage => { | ||
let config: ObjectStorageConfig = | ||
rocket.figment().focus("storage.object").extract().expect( | ||
"Unable to load object storage config, is it defined in Rocket.toml?", | ||
); | ||
StorageDriver::object(Bucket::new( | ||
config.name, | ||
config.url, | ||
BucketCredentials::new( | ||
Credentials::from_keys(config.access_key, config.access_key_secret, None), | ||
config.region, | ||
), | ||
)) | ||
} | ||
}; | ||
Ok(rocket.manage(driver)) | ||
} | ||
} | ||
|
||
#[rocket::async_trait] | ||
impl<'r> FromRequest<'r> for StorageDriverGuard { | ||
type Error = crate::endpoint::v1::error::Error; | ||
|
||
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> { | ||
if let Some(driver) = request.rocket().state::<StorageDriver>() { | ||
Outcome::Success(StorageDriverGuard(driver.clone())) | ||
} else { | ||
Outcome::Error(( | ||
rocket::http::Status::InternalServerError, | ||
Self::Error::StorageUnavailableError, | ||
)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.