litecloud/api/src/main.rs
2025-06-05 23:33:32 +02:00

91 lines
2 KiB
Rust

#[macro_use]
extern crate rocket;
use rocket::{fs::FileServer, Request};
use rocket::serde::json::Json;
use rocket::http::Status;
use rocket::response::status::Custom;
use rocket::data::ByteUnit;
use sqlx::postgres::PgPoolOptions;
use std::env;
use dotenv::dotenv;
use rocket_cors::{CorsOptions};
mod auth;
mod encryption;
mod file;
mod user;
mod admin;
mod models;
use crate::file::*;
use crate::user::*;
use crate::admin::*;
#[catch(404)]
fn not_found(_: &Request) -> Custom<Json<&'static str>> {
Custom(Status::NotFound, Json("Not Found"))
}
#[options("/<_..>")]
fn all_options() -> &'static str {
""
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
dotenv().ok();
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL not set");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&db_url)
.await
.expect("Failed to connect to database");
let figment = rocket::Config::figment()
.merge(("port", 8082))
.merge(("address", "0.0.0.0"))
.merge(("limits", rocket::data::Limits::new()
.limit("form", ByteUnit::Gigabyte(5)) // 5 GB
.limit("file", ByteUnit::Gigabyte(1)) // 1 GB
.limit("data-form", ByteUnit::Gigabyte(5)) // 5 GB for multipart/form-data
));
let cors = CorsOptions::default()
.to_cors()
.expect("Failed to create CORS fairing");
rocket::custom(figment)
.attach(cors)
.manage(pool)
.mount("/api", routes![
register,
login,
get_user_info,
delete_user,
upload_file,
download_file,
download_shared,
list_user_files,
list_user_shares,
delete_file,
delete_share,
share_file,
update_role,
update_quota,
all_options,
])
//.mount("/", FileServer::from("/app/static").rank(10))
//.register("/", catchers![not_found])
.launch()
.await?;
Ok(())
}