Skip to content

Commit

Permalink
fix: cannot server static assets if not in current directory
Browse files Browse the repository at this point in the history
  • Loading branch information
lomirus committed Jul 7, 2022
1 parent 33c30ad commit 67fa9b8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use async_std::{sync::Mutex, task::block_on};
use clap::Parser;
use local_ip_address::local_ip;
use once_cell::sync::{Lazy, OnceCell};
use std::{collections::HashMap, thread};
use std::{collections::HashMap, path::PathBuf, thread};
use tide_websockets::WebSocketConnection;
use uuid::Uuid;

Expand All @@ -26,6 +26,7 @@ struct Args {
}

pub(crate) static HOST: OnceCell<String> = OnceCell::new();
pub(crate) static PATH: OnceCell<PathBuf> = OnceCell::new();
pub(crate) static WS_CLIENTS: Lazy<Mutex<HashMap<Uuid, WebSocketConnection>>> =
Lazy::new(|| Mutex::new(HashMap::new()));

Expand Down
6 changes: 3 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tide::{listener::Listener, Request, Response, StatusCode};
use tide_websockets::WebSocket;
use uuid::Uuid;

use crate::{log, HOST, WS_CLIENTS};
use crate::{log, HOST, PATH, WS_CLIENTS};

pub static SCRIPT: OnceCell<Node> = OnceCell::new();

Expand Down Expand Up @@ -79,9 +79,9 @@ async fn static_assets(req: Request<()>) -> tide::Result {
// Get the path and mime of the static file.
let mut path = req.url().path().to_string();
path = if path.ends_with('/') {
format!(".{}index.html", path)
format!("{}{}index.html", PATH.get().unwrap().display(), path)
} else {
format!(".{}", path)
format!("{}{}", PATH.get().unwrap().display(), path)
};
let mime = mime_guess::from_path(&path).first_or_text_plain();

Expand Down
3 changes: 2 additions & 1 deletion src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};
use tide_websockets::Message;

use crate::{log, WS_CLIENTS};
use crate::{log, PATH, WS_CLIENTS};

async fn broadcast() {
for (_, conn) in WS_CLIENTS.lock().await.iter() {
Expand All @@ -21,6 +21,7 @@ pub async fn watch(path: String) {
Ok(path) => path,
Err(err) => log::panic!("Failed to get absolute path of `{}`: {}", path, err),
};
PATH.set(abs_path.clone()).unwrap();
let abs_path_str = match abs_path.clone().into_os_string().into_string() {
Ok(path_str) => path_str,
Err(_) => log::panic!(
Expand Down

0 comments on commit 67fa9b8

Please sign in to comment.