2021-04-04 12:30:31 +00:00
|
|
|
mod config;
|
|
|
|
mod db;
|
2020-07-09 17:27:24 +00:00
|
|
|
mod deleter;
|
2021-04-04 12:30:31 +00:00
|
|
|
mod download;
|
2020-07-09 17:27:24 +00:00
|
|
|
mod file_kind;
|
2020-07-08 19:26:46 +00:00
|
|
|
mod multipart;
|
2021-12-08 17:54:55 +00:00
|
|
|
mod template;
|
2021-04-04 12:30:31 +00:00
|
|
|
mod upload;
|
2020-07-08 19:26:46 +00:00
|
|
|
|
2021-04-04 12:30:31 +00:00
|
|
|
use actix_files::Files;
|
2021-09-09 20:18:42 +00:00
|
|
|
use actix_web::{middleware::Logger, web, App, Error, HttpResponse, HttpServer};
|
2021-04-04 12:30:31 +00:00
|
|
|
use async_std::{channel, task};
|
2021-04-04 01:38:29 +00:00
|
|
|
use env_logger::Env;
|
2021-04-04 12:30:31 +00:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2021-03-09 18:59:10 +00:00
|
|
|
#[actix_web::main]
|
2020-07-08 19:26:46 +00:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2021-04-04 01:38:29 +00:00
|
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("info,sqlx=warn")).init();
|
2020-07-08 19:26:46 +00:00
|
|
|
|
2021-04-04 12:30:31 +00:00
|
|
|
let pool: PgPool = db::setup_db().await;
|
|
|
|
let config = config::get_config().await;
|
2021-03-09 18:59:10 +00:00
|
|
|
let (sender, receiver) = channel::bounded(8);
|
2020-07-13 13:22:33 +00:00
|
|
|
|
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
|
|
|
|
2021-12-08 17:54:55 +00:00
|
|
|
template::write_prefillable_templates(&config).await;
|
|
|
|
|
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())
|
2021-04-04 12:30:31 +00:00
|
|
|
.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}"])
|
2021-04-04 12:30:31 +00:00
|
|
|
.route(web::get().to(upload::uploaded)),
|
2020-08-03 00:42:27 +00:00
|
|
|
)
|
2021-03-09 21:19:06 +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
|
|
|
|
}
|