do some code cleanup

This commit is contained in:
neri 2020-07-14 17:53:43 +02:00
parent 594ca9d253
commit a92d7fe397
1 changed files with 10 additions and 13 deletions

View File

@ -28,7 +28,7 @@ const UPLOAD_HTML: &str = include_str!("../template/upload.html");
const VIEW_HTML: &str = include_str!("../template/view.html"); const VIEW_HTML: &str = include_str!("../template/view.html");
async fn index() -> Result<NamedFile, Error> { async fn index() -> Result<NamedFile, Error> {
Ok(NamedFile::open("static/index.html") Ok(NamedFile::open("./static/index.html")
.map_err(|_| error::ErrorNotFound(""))? .map_err(|_| error::ErrorNotFound(""))?
.disable_content_disposition()) .disable_content_disposition())
} }
@ -154,35 +154,32 @@ struct Config {
#[actix_rt::main] #[actix_rt::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
env::set_var( if env::var("RUST_LOG").is_err() {
"RUST_LOG", env::set_var("RUST_LOG", "info");
env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()), }
);
env_logger::init(); env_logger::init();
let pool: PgPool = setup_db().await; let pool: PgPool = setup_db().await;
let config = Config { let config = Config {
server_url: env::var("SERVER_URL").unwrap_or_else(|_| "http://localhost:8000".to_owned()), server_url: env::var("SERVER_URL").unwrap_or_else(|_| "http://localhost:8000".to_owned()),
files_dir: PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned())), files_dir: PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned())),
}; };
fs::create_dir_all(&config.files_dir) fs::create_dir_all(&config.files_dir)
.await .await
.expect("could not create directory for storing files"); .expect("could not create directory for storing files");
let (sender, receiver) = channel(8);
log::info!("omnomnom"); log::info!("omnomnom");
let (send, recv) = channel(1);
task::spawn(deleter::delete_old_files( task::spawn(deleter::delete_old_files(
recv, receiver,
pool.clone(), pool.clone(),
config.files_dir.clone(), config.files_dir.clone(),
)); ));
let db = web::Data::new(pool); let db = web::Data::new(pool);
let send = web::Data::new(send); let sender = web::Data::new(sender);
let max_bytes: usize = env::var("UPLOAD_MAX_BYTES") let upload_max_bytes: usize = env::var("UPLOAD_MAX_BYTES")
.ok() .ok()
.and_then(|variable| variable.parse().ok()) .and_then(|variable| variable.parse().ok())
.unwrap_or(8_388_608); .unwrap_or(8_388_608);
@ -193,8 +190,8 @@ async fn main() -> std::io::Result<()> {
App::new() App::new()
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.app_data(db.clone()) .app_data(db.clone())
.app_data(send.clone()) .app_data(sender.clone())
.app_data(Bytes::configure(|cfg| cfg.limit(max_bytes))) .app_data(Bytes::configure(|cfg| cfg.limit(upload_max_bytes)))
.data(config.clone()) .data(config.clone())
.service(web::resource("/").route(web::get().to(index))) .service(web::resource("/").route(web::get().to(index)))
.service(web::resource("/upload").route(web::post().to(upload))) .service(web::resource("/upload").route(web::post().to(upload)))