Skip to content

Commit 7aa156c

Browse files
committed
fix(server): http server didn't have frontend assets to access in production
1 parent c120605 commit 7aa156c

3 files changed

Lines changed: 16 additions & 20 deletions

File tree

src-tauri/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ repository = ""
88
edition = "2021"
99
rust-version = "1.77.2"
1010

11+
[package.metadata.dist]
12+
include = ["dist/"]
13+
1114
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1215

1316
[lib]
@@ -38,4 +41,5 @@ uuid = { version = "1.18.0", features = ["v4"] }
3841
futures-util = "0.3.31"
3942
sha2 = "0.10.9"
4043
base64 = "0.22.1"
44+
include_dir = "0.7.4"
4145

src-tauri/src/server.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::HashMap;
2-
use std::fs;
3-
use std::path::{Path, PathBuf};
2+
use include_dir::{include_dir, Dir};
43
use std::sync::{Arc, Mutex};
54
use std::thread;
65

@@ -12,7 +11,7 @@ use tokio::sync::mpsc;
1211
use tokio_tungstenite::tungstenite::Message;
1312
use uuid::Uuid;
1413

15-
use crate::commands::refresh_obs_browser_source;
14+
static DIST_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../dist");
1615

1716
pub struct Client {
1817
pub id: String,
@@ -36,31 +35,22 @@ impl OverlayServer {
3635
pub fn start_http_server(&self) {
3736
let addr = "127.0.0.1:4000";
3837
let server = TinyHttpServer::http(addr).expect("Failed to start HTTP server");
39-
let dist_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
40-
.join("../dist")
41-
.canonicalize()
42-
.expect("Failed to canonicalize dist path");
4338

4439
println!("HTTP overlay server running at http://{}", addr);
4540

4641
thread::spawn(move || {
4742
for request in server.incoming_requests() {
4843
let url_path = request.url().trim_start_matches('/');
4944

50-
let file_path: PathBuf = if url_path == "overlay" || url_path.is_empty() {
51-
dist_dir.join("src/overlay.html")
45+
let file = if url_path == "overlay" || url_path.is_empty() {
46+
DIST_DIR.get_file("src/overlay.html")
5247
} else {
53-
let candidate = dist_dir.join(url_path);
54-
if candidate.exists() && candidate.is_file() {
55-
candidate
56-
} else {
57-
PathBuf::new()
58-
}
48+
DIST_DIR.get_file(url_path)
5949
};
6050

61-
let response = if file_path.exists() && file_path.is_file() {
62-
let content = fs::read(&file_path).unwrap_or_default();
63-
let mime = match file_path.extension().and_then(|s| s.to_str()) {
51+
let response = if let Some(file) = file {
52+
let content = file.contents();
53+
let mime = match file.path().extension().and_then(|s| s.to_str()) {
6454
Some("css") => "text/css",
6555
Some("js") => "application/javascript",
6656
Some("html") => "text/html",
@@ -80,7 +70,7 @@ impl OverlayServer {
8070
}
8171

8272
pub async fn start_ws_server(
83-
&self,
73+
&self,
8474
ws_addr: &str,
8575
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
8676
println!("WebSocket server running on ws://{}", ws_addr);

src-tauri/tauri.conf.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
"icons/icon.ico"
3737
],
3838
"resources": {
39-
"../resources/config/*": "resources/config"
39+
"../resources/config/*": "resources/config",
40+
"../dist/src/*": "dist/src",
41+
"../dist/assets/*": "dist/assets"
4042
}
4143
}
4244
}

0 commit comments

Comments
 (0)