Skip to content

Commit b7a2ce2

Browse files
authored
feat(cli): add --port, closes #6186 (#6283)
* feat(cli): add --dev-server-port, closes #6186 * add http:// prefix * name it to `--port` * rename in all places
1 parent 3536aa0 commit b7a2ce2

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

.changes/cli-dev-server-port.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'cli.rs': 'patch'
3+
---
4+
5+
Add `--port` to specify the port used for static files dev server. It can also be specified through `TAURI_DEV_SERVER_PORT` env var.

tooling/cli/src/dev.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ pub struct Options {
6464
/// Disable the dev server for static files.
6565
#[clap(long)]
6666
pub no_dev_server: bool,
67+
/// Specify port for the dev server for static files. Defaults to 1430
68+
/// Can also be set using `TAURI_DEV_SERVER_PORT` env var.
69+
#[clap(long)]
70+
pub port: Option<u16>,
6771
}
6872

6973
pub fn command(options: Options) -> Result<()> {
@@ -223,11 +227,12 @@ fn command_internal(mut options: Options) -> Result<()> {
223227
.clone();
224228
if !options.no_dev_server {
225229
if let AppUrl::Url(WindowUrl::App(path)) = &dev_path {
226-
use crate::helpers::web_dev_server::{start_dev_server, SERVER_URL};
230+
use crate::helpers::web_dev_server::start_dev_server;
227231
if path.exists() {
228232
let path = path.canonicalize()?;
229-
start_dev_server(path);
230-
dev_path = AppUrl::Url(WindowUrl::External(SERVER_URL.parse().unwrap()));
233+
let server_url = start_dev_server(path, options.port);
234+
let server_url = format!("http://{server_url}");
235+
dev_path = AppUrl::Url(WindowUrl::External(server_url.parse().unwrap()));
231236

232237
// TODO: in v2, use an env var to pass the url to the app context
233238
// or better separate the config passed from the cli internally and
@@ -238,7 +243,7 @@ fn command_internal(mut options: Options) -> Result<()> {
238243
c.build.dev_path = dev_path.clone();
239244
options.config = Some(serde_json::to_string(&c).unwrap());
240245
} else {
241-
options.config = Some(format!(r#"{{ "build": {{ "devPath": "{SERVER_URL}" }} }}"#))
246+
options.config = Some(format!(r#"{{ "build": {{ "devPath": "{server_url}" }} }}"#))
242247
}
243248
}
244249
}

tooling/cli/src/helpers/web_dev_server.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use kuchiki::{traits::TendrilSink, NodeRef};
1414
use notify::RecursiveMode;
1515
use notify_debouncer_mini::new_debouncer;
1616
use std::{
17-
net::SocketAddr,
17+
net::{Ipv4Addr, SocketAddr},
1818
path::{Path, PathBuf},
19-
str::FromStr,
2019
sync::{mpsc::sync_channel, Arc},
2120
thread,
2221
time::Duration,
@@ -25,15 +24,23 @@ use tauri_utils::mime_type::MimeType;
2524
use tokio::sync::broadcast::{channel, Sender};
2625

2726
const AUTO_RELOAD_SCRIPT: &str = include_str!("./auto-reload.js");
28-
pub const SERVER_URL: &str = "http://127.0.0.1:1430";
2927

3028
struct State {
3129
serve_dir: PathBuf,
3230
tx: Sender<()>,
3331
}
3432

35-
pub fn start_dev_server<P: AsRef<Path>>(path: P) {
33+
pub fn start_dev_server<P: AsRef<Path>>(path: P, port: Option<u16>) -> SocketAddr {
3634
let serve_dir = path.as_ref().to_path_buf();
35+
let server_url = SocketAddr::new(
36+
Ipv4Addr::new(127, 0, 0, 1).into(),
37+
port.unwrap_or_else(|| {
38+
std::env::var("TAURI_DEV_SERVER_PORT")
39+
.unwrap_or_else(|_| "1430".to_string())
40+
.parse()
41+
.unwrap()
42+
}),
43+
);
3744

3845
std::thread::spawn(move || {
3946
tokio::runtime::Builder::new_current_thread()
@@ -84,12 +91,14 @@ pub fn start_dev_server<P: AsRef<Path>>(path: P) {
8491
ws.on_upgrade(|socket| async move { ws_handler(socket, state).await })
8592
}),
8693
);
87-
Server::bind(&SocketAddr::from_str(SERVER_URL.split('/').nth(2).unwrap()).unwrap())
94+
Server::bind(&server_url)
8895
.serve(router.into_make_service())
8996
.await
9097
.unwrap();
9198
})
9299
});
100+
101+
server_url
93102
}
94103

95104
async fn handler<T>(req: Request<T>, state: Arc<State>) -> impl IntoResponse {

0 commit comments

Comments
 (0)