Files
madbase/storage/src/lib.rs

73 lines
2.4 KiB
Rust

pub mod handlers;
pub mod tus;
use aws_config::BehaviorVersion;
use aws_sdk_s3::config::Credentials;
use aws_sdk_s3::{config::Region, Client};
use axum::{extract::DefaultBodyLimit, routing::{get, post, patch}, Router};
use common::Config;
use handlers::StorageState;
use sqlx::PgPool;
pub async fn init(db: PgPool, config: Config) -> Router {
// Initialize S3 Client (MinIO)
let s3_endpoint =
std::env::var("S3_ENDPOINT").unwrap_or_else(|_| "http://localhost:9000".to_string());
let s3_access_key =
std::env::var("MINIO_ROOT_USER").unwrap_or_else(|_| "minioadmin".to_string());
let s3_secret_key =
std::env::var("MINIO_ROOT_PASSWORD").unwrap_or_else(|_| "minioadmin".to_string());
let s3_bucket = std::env::var("S3_BUCKET").unwrap_or_else(|_| "madbase".to_string());
let aws_config = aws_config::defaults(BehaviorVersion::latest())
.region(Region::new("us-east-1"))
.endpoint_url(&s3_endpoint)
.credentials_provider(Credentials::new(
s3_access_key,
s3_secret_key,
None,
None,
"static",
))
.load()
.await;
let s3_config = aws_sdk_s3::config::Builder::from(&aws_config)
.endpoint_url(&s3_endpoint)
.force_path_style(true)
.build();
let s3_client = Client::from_conf(s3_config);
// Create bucket if not exists
let _ = s3_client.create_bucket().bucket(&s3_bucket).send().await;
let state = StorageState {
db,
s3_client,
config,
bucket_name: s3_bucket,
};
Router::new()
.route("/bucket", get(handlers::list_buckets))
.route("/object/list/:bucket_id", post(handlers::list_objects))
.route(
"/object/sign/:bucket_id/*filename",
post(handlers::sign_object).get(handlers::get_signed_object),
)
.route(
"/object/:bucket_id/*filename",
get(handlers::download_object).post(handlers::upload_object),
)
// TUS Resumable Uploads
.route("/upload/resumable", post(tus::tus_create_upload).options(tus::tus_options))
.route("/upload/resumable/:upload_id",
patch(tus::tus_patch_upload)
.head(tus::tus_head_upload)
.options(tus::tus_options)
)
.layer(DefaultBodyLimit::max(1024 * 1024 * 1024)) // 1GB limit for TUS
.with_state(state)
}