make static dir and port configurable

This commit is contained in:
xenofem 2022-04-27 00:53:32 -04:00
parent c41430bcdc
commit 779337de5e

View file

@ -62,18 +62,19 @@ async fn upload_socket(
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
env_logger::init(); env_logger::init();
let ip = "0.0.0.0:3000";
let data: AppData = web::Data::new(RwLock::new(HashMap::new())); let data: AppData = web::Data::new(RwLock::new(HashMap::new()));
let static_dir = PathBuf::from(std::env::var("STATIC_DIR").unwrap_or_else(|_| String::from("static")));
let port = std::env::var("PORT").ok().and_then(|p| p.parse::<u16>().ok()).unwrap_or(8080);
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.app_data(data.clone()) .app_data(data.clone())
.wrap(Logger::default()) .wrap(Logger::default())
.service(upload_socket) .service(upload_socket)
.service(actix_files::Files::new("/", "./static").index_file("index.html")) .service(actix_files::Files::new("/", static_dir.clone()).index_file("index.html"))
}) })
.bind(ip)? .bind(("127.0.0.1", port))?
.run() .run()
.await?; .await?;
Ok(()) Ok(())