Skip to content

Commit

Permalink
feat(cli): add --port, closes #6186 (#6283)
Browse files Browse the repository at this point in the history
* feat(cli): add --dev-server-port, closes #6186

* add http:// prefix

* name it to `--port`

* rename in all places
  • Loading branch information
amrbashir authored Mar 16, 2023
1 parent 3536aa0 commit b7a2ce2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changes/cli-dev-server-port.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cli.rs': 'patch'
---

Add `--port` to specify the port used for static files dev server. It can also be specified through `TAURI_DEV_SERVER_PORT` env var.
13 changes: 9 additions & 4 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub struct Options {
/// Disable the dev server for static files.
#[clap(long)]
pub no_dev_server: bool,
/// Specify port for the dev server for static files. Defaults to 1430
/// Can also be set using `TAURI_DEV_SERVER_PORT` env var.
#[clap(long)]
pub port: Option<u16>,
}

pub fn command(options: Options) -> Result<()> {
Expand Down Expand Up @@ -223,11 +227,12 @@ fn command_internal(mut options: Options) -> Result<()> {
.clone();
if !options.no_dev_server {
if let AppUrl::Url(WindowUrl::App(path)) = &dev_path {
use crate::helpers::web_dev_server::{start_dev_server, SERVER_URL};
use crate::helpers::web_dev_server::start_dev_server;
if path.exists() {
let path = path.canonicalize()?;
start_dev_server(path);
dev_path = AppUrl::Url(WindowUrl::External(SERVER_URL.parse().unwrap()));
let server_url = start_dev_server(path, options.port);
let server_url = format!("http://{server_url}");
dev_path = AppUrl::Url(WindowUrl::External(server_url.parse().unwrap()));

// TODO: in v2, use an env var to pass the url to the app context
// or better separate the config passed from the cli internally and
Expand All @@ -238,7 +243,7 @@ fn command_internal(mut options: Options) -> Result<()> {
c.build.dev_path = dev_path.clone();
options.config = Some(serde_json::to_string(&c).unwrap());
} else {
options.config = Some(format!(r#"{{ "build": {{ "devPath": "{SERVER_URL}" }} }}"#))
options.config = Some(format!(r#"{{ "build": {{ "devPath": "{server_url}" }} }}"#))
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions tooling/cli/src/helpers/web_dev_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use kuchiki::{traits::TendrilSink, NodeRef};
use notify::RecursiveMode;
use notify_debouncer_mini::new_debouncer;
use std::{
net::SocketAddr,
net::{Ipv4Addr, SocketAddr},
path::{Path, PathBuf},
str::FromStr,
sync::{mpsc::sync_channel, Arc},
thread,
time::Duration,
Expand All @@ -25,15 +24,23 @@ use tauri_utils::mime_type::MimeType;
use tokio::sync::broadcast::{channel, Sender};

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

struct State {
serve_dir: PathBuf,
tx: Sender<()>,
}

pub fn start_dev_server<P: AsRef<Path>>(path: P) {
pub fn start_dev_server<P: AsRef<Path>>(path: P, port: Option<u16>) -> SocketAddr {
let serve_dir = path.as_ref().to_path_buf();
let server_url = SocketAddr::new(
Ipv4Addr::new(127, 0, 0, 1).into(),
port.unwrap_or_else(|| {
std::env::var("TAURI_DEV_SERVER_PORT")
.unwrap_or_else(|_| "1430".to_string())
.parse()
.unwrap()
}),
);

std::thread::spawn(move || {
tokio::runtime::Builder::new_current_thread()
Expand Down Expand Up @@ -84,12 +91,14 @@ pub fn start_dev_server<P: AsRef<Path>>(path: P) {
ws.on_upgrade(|socket| async move { ws_handler(socket, state).await })
}),
);
Server::bind(&SocketAddr::from_str(SERVER_URL.split('/').nth(2).unwrap()).unwrap())
Server::bind(&server_url)
.serve(router.into_make_service())
.await
.unwrap();
})
});

server_url
}

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

0 comments on commit b7a2ce2

Please sign in to comment.