datatrash/src/main.rs

116 lines
3.9 KiB
Rust
Raw Normal View History

mod config;
mod db;
2020-07-09 17:27:24 +00:00
mod deleter;
mod download;
mod mime_relations;
2020-07-08 19:26:46 +00:00
mod multipart;
2022-08-21 16:44:12 +00:00
mod rate_limit;
mod template;
mod upload;
2020-07-08 19:26:46 +00:00
2022-08-21 16:44:12 +00:00
use crate::rate_limit::ForwardedPeerIpKeyExtractor;
use actix_files::Files;
2022-08-21 16:44:12 +00:00
use actix_governor::{Governor, GovernorConfigBuilder};
2022-02-26 23:34:57 +00:00
use actix_web::{
http::header::{HeaderName, HeaderValue, CONTENT_SECURITY_POLICY, X_CONTENT_TYPE_OPTIONS},
middleware::{self, DefaultHeaders, Logger},
2022-02-26 23:34:57 +00:00
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
const DEFAULT_CSP: (HeaderName, &str) = (
CONTENT_SECURITY_POLICY,
"default-src 'none'; connect-src 'self'; img-src 'self'; media-src 'self'; font-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'; base-uri 'self'; frame-src 'none'; frame-ancestors 'none'; form-action 'self';"
);
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
2022-11-04 10:37:10 +00:00
let pool: PgPool = db::setup().await;
let config = config::from_env().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);
2022-08-21 16:44:12 +00:00
let governor_conf = GovernorConfigBuilder::default()
.per_second(config.rate_limit_replenish_seconds)
2022-08-21 16:44:12 +00:00
.burst_size(config.rate_limit_burst)
.key_extractor(ForwardedPeerIpKeyExtractor {
proxied: config.proxied,
})
.use_headers()
.finish()
.unwrap();
2020-07-11 21:27:15 +00:00
HttpServer::new({
move || {
2022-08-21 16:44:12 +00:00
let app = App::new()
2021-08-18 21:22:50 +00:00
.wrap(Logger::new(r#"%{r}a "%r" =%s %bbytes %Tsec"#))
.wrap(
DefaultHeaders::new()
.add(DEFAULT_CSP)
.add((X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"))),
)
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())
2022-08-21 16:44:12 +00:00
.default_service(web::route().to(not_found));
if config.enable_rate_limit {
app.service(
web::resource([
"/{id:[a-z0-9]{5}}",
"/{id:[a-z0-9]{5}}/",
"/{id:[a-z0-9]{5}}/{name}",
])
.wrap(Governor::new(&governor_conf))
.route(web::get().to(download::download)),
)
} else {
app.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
)
2022-08-21 16:44:12 +00:00
}
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
}