Skip to content

Commit

Permalink
chore: remove Arc<RwLock> when initializing arguments (#33)
Browse files Browse the repository at this point in the history
* chore: remove RwLock and Arc for global initialization
* chore: apply clippy suggestions

Signed-off-by: Rafael Fernández López <[email protected]>
  • Loading branch information
ereslibre authored Nov 18, 2022
1 parent dea0581 commit e2665ff
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ use clap::Parser;
use data::kv::KV;
use runner::WasmOutput;
use std::io::Error;
use std::path::Path;
use std::path::PathBuf;
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use std::{collections::HashMap, sync::RwLock};

// Provide a static root_path so it can be used in the default_handler to manage
// static assets.
lazy_static! {
static ref ROOT_PATH: Arc<RwLock<PathBuf>> = Arc::new(RwLock::new(PathBuf::new()));
static ref ARGS: Args = Args::parse();
static ref ROOT_PATH: PathBuf = ARGS.path.clone();
}

// Arguments
Expand Down Expand Up @@ -68,20 +65,17 @@ struct DataConnectors {
async fn find_static_html(uri_path: &str) -> Result<NamedFile, Error> {
// Avoid dots in the URI. If they are present, the extension
// was passed so the file should be properly rendered.
let clean_path = uri_path.replace(".", "");
let clean_path = uri_path.replace('.', "");
let file;

let rw_path = ROOT_PATH.read().unwrap();
let base_path = rw_path.as_os_str();

// Possible paths
let index_folder_path = Path::new(base_path).join(format!("public{}/index.html", clean_path));
let html_ext_path = Path::new(base_path).join(format!("public{}.html", clean_path));
let public_404_path = Path::new(base_path).join("public").join("404.html");
let index_folder_path = ROOT_PATH.join(format!("public{}/index.html", clean_path));
let html_ext_path = ROOT_PATH.join(format!("public{}.html", clean_path));
let public_404_path = ROOT_PATH.join("public").join("404.html");

if uri_path.ends_with("/") && index_folder_path.exists() {
if uri_path.ends_with('/') && index_folder_path.exists() {
file = NamedFile::open_async(index_folder_path).await;
} else if !uri_path.ends_with("/") && html_ext_path.exists() {
} else if !uri_path.ends_with('/') && html_ext_path.exists() {
file = NamedFile::open_async(html_ext_path).await;
} else {
file = NamedFile::open_async(public_404_path).await;
Expand Down Expand Up @@ -164,10 +158,7 @@ async fn debug(req: HttpRequest) -> impl Responder {

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let args = Args::parse();

// Store the root path so it can be used later
*ROOT_PATH.write().expect("Cannot set the root path") = args.path.clone();
let args = &*ARGS;

std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
Expand Down

0 comments on commit e2665ff

Please sign in to comment.