kanzashi-cdn/src/main.rs
2026-07-24 01:36:58 +02:00

81 lines
No EOL
2.7 KiB
Rust

#[macro_use]
extern crate rocket;
mod db;
mod guards;
mod models;
mod routes;
use std::collections::HashSet;
use aws_config::BehaviorVersion;
use aws_credential_types::Credentials;
use aws_sdk_s3::config::RequestChecksumCalculation;
use aws_sdk_s3::Client;
use rand::RngExt;
use rocket_dyn_templates::Template;
use models::AdminConsoleKey;
#[derive(Clone, Debug)]
pub struct AllowedExtensions(pub HashSet<String>);
#[launch]
async fn rocket() -> _ {
dotenvy::dotenv().ok();
let endpoint = std::env::var("MINIO_ENDPOINT").expect("MINIO_ENDPOINT missing");
let access_key = std::env::var("MINIO_ACCESS_KEY").expect("MINIO_ACCESS_KEY missing");
let secret_key = std::env::var("MINIO_SECRET_KEY").expect("MINIO_SECRET_KEY missing");
let region = std::env::var("MINIO_REGION").unwrap_or_else(|_| "us-east-1".to_string());
let allowed_bucket = std::env::var("ALLOWED_BUCKET").expect("ALLOWED_BUCKET missing");
let allowed_ext_str = std::env::var("ALLOWED_EXTENSIONS")
.unwrap_or_else(|_| "jpg,jpeg,png,gif,webp".to_string());
let allowed_extensions: HashSet<String> = allowed_ext_str
.split(',')
.map(|s| s.trim().to_lowercase())
.filter(|s| !s.is_empty())
.collect();
let db_pool = db::init_db();
let mut random_bytes = [0u8; 16];
rand::rng().fill(&mut random_bytes);
let console_admin_key = hex::encode(random_bytes);
println!("\n==================================================================");
println!("key: {}", console_admin_key);
println!("Panel located at /admin?key={}", console_admin_key);
println!("==================================================================\n");
let credentials = Credentials::new(access_key, secret_key, None, None, "static");
let s3_config = aws_sdk_s3::Config::builder()
.behavior_version(BehaviorVersion::latest())
.endpoint_url(endpoint)
.credentials_provider(credentials)
.region(aws_sdk_s3::config::Region::new(region))
.force_path_style(true)
.request_checksum_calculation(RequestChecksumCalculation::WhenRequired)
.build();
let client = Client::from_conf(s3_config);
rocket::build()
.manage(client)
.manage(allowed_bucket)
.manage(AllowedExtensions(allowed_extensions))
.manage(db_pool)
.manage(AdminConsoleKey(console_admin_key))
.attach(Template::fairing())
.mount(
"/",
routes![
routes::i::get_asset,
routes::upload::upload_asset,
routes::admin::index::admin_dashboard,
routes::admin::keys::create::create_key,
routes::admin::keys::revoke::revoke_key,
],
)
}