datatrash/src/main.rs

74 lines
2.3 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 upload;
2020-07-08 19:26:46 +00:00
use actix_files::Files;
2021-09-09 20:18:42 +00:00
use actix_web::{middleware::Logger, web, App, Error, HttpResponse, HttpServer};
use async_std::{channel, task};
use env_logger::Env;
use sqlx::postgres::PgPool;
2020-07-08 19:26:46 +00:00
use std::env;
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"))
}
#[actix_web::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;
let (sender, receiver) = channel::bounded(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
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"#))
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())
2020-07-11 21:27:15 +00:00
.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(
web::resource("/assets/auth-hide.js").route(web::get().to(upload::auth_hide)),
)
.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
}