datatrash/src/main.rs

80 lines
2.4 KiB
Rust
Raw Normal View History

mod config;
mod db;
2020-07-09 17:27:24 +00:00
mod deleter;
mod download;
2020-07-09 17:27:24 +00:00
mod file_kind;
2020-07-08 19:26:46 +00:00
mod multipart;
mod template;
mod upload;
2020-07-08 19:26:46 +00:00
use actix_files::Files;
2022-02-26 23:34:57 +00:00
use actix_web::{
middleware::{self, Logger},
web::{self, Data},
App, Error, HttpResponse, HttpServer,
};
use env_logger::Env;
use sqlx::postgres::PgPool;
2020-07-08 19:26:46 +00:00
use std::env;
2022-02-26 23:34:57 +00:00
use tokio::{sync::mpsc::channel, task};
2020-07-08 19:26:46 +00:00
2020-08-19 14:24:42 +00:00
async fn not_found() -> Result<HttpResponse, Error> {
Ok(HttpResponse::NotFound()
.content_type("text/plain")
.body("not found"))
}
2022-02-26 23:34:57 +00:00
#[tokio::main]
2020-07-08 19:26:46 +00:00
async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info,sqlx=warn")).init();
2020-07-08 19:26:46 +00:00
let pool: PgPool = db::setup_db().await;
let config = config::get_config().await;
2022-02-26 23:34:57 +00:00
let (sender, receiver) = channel(8);
2020-07-13 13:29:40 +00:00
log::info!("omnomnom");
2021-08-18 21:22:50 +00:00
let db = web::Data::new(pool.clone());
let expiry_watch_sender = web::Data::new(sender);
let bind_address = env::var("BIND_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_owned());
2020-07-11 21:27:15 +00:00
task::spawn(deleter::delete_old_files(
2020-07-14 15:53:43 +00:00
receiver,
2021-08-18 21:22:50 +00:00
pool,
2020-07-11 21:27:15 +00:00
config.files_dir.clone(),
));
2020-07-09 17:27:24 +00:00
template::write_prefillable_templates(&config).await;
2022-02-26 23:34:57 +00:00
let config = Data::new(config);
2020-07-11 21:27:15 +00:00
HttpServer::new({
move || {
App::new()
2021-08-18 21:22:50 +00:00
.wrap(Logger::new(r#"%{r}a "%r" =%s %bbytes %Tsec"#))
2022-02-26 23:34:57 +00:00
.wrap(middleware::Compress::default())
2020-07-11 21:27:15 +00:00
.app_data(db.clone())
2020-12-03 22:30:37 +00:00
.app_data(expiry_watch_sender.clone())
2022-02-26 23:34:57 +00:00
.app_data(config.clone())
.service(web::resource("/").route(web::get().to(upload::index)))
.service(web::resource("/upload").route(web::post().to(upload::upload)))
2020-08-02 23:12:42 +00:00
.service(
2020-08-03 00:42:27 +00:00
web::resource(["/upload/{id}", "/upload/{id}/{name}"])
.route(web::get().to(upload::uploaded)),
2020-08-03 00:42:27 +00:00
)
.service(Files::new("/static", "static").disable_content_disposition())
2020-08-03 00:42:27 +00:00
.service(
2021-04-07 11:02:46 +00:00
web::resource([
"/{id:[a-z0-9]{5}}",
"/{id:[a-z0-9]{5}}/",
"/{id:[a-z0-9]{5}}/{name}",
])
.route(web::get().to(download::download)),
2020-08-02 23:12:42 +00:00
)
2020-08-19 14:24:42 +00:00
.default_service(web::route().to(not_found))
2020-07-11 21:27:15 +00:00
}
2020-07-08 19:26:46 +00:00
})
2020-07-11 21:27:15 +00:00
.bind(bind_address)?
2020-07-08 19:26:46 +00:00
.run()
.await
}