Skip to content

Commit 19bd87e

Browse files
committed
feat: modernized host/port setting for perseus serve
Added `--host`/`--port` and renamed environment variables to `PERSEUS_HOST` and `PERSEUS_PORT`. Note that the originals are still supported in deployment binaries. Closes #107.
1 parent 4139df9 commit 19bd87e

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

examples/basic/.perseus/server/src/main.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,16 @@ fn get_standalone_and_act() -> bool {
6868

6969
/// Gets the host and port to serve on.
7070
fn get_host_and_port() -> (String, u16) {
71-
let host = env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
72-
let port = env::var("PORT")
73-
.unwrap_or_else(|_| "8080".to_string())
71+
// We have to use two sets of environment variables until v0.4.0
72+
// TODO Remove the old environment variables in v0.4.0
73+
let host_old = env::var("HOST");
74+
let port_old = env::var("PORT");
75+
let host = env::var("PERSEUS_HOST");
76+
let port = env::var("PERSEUS_PORT");
77+
78+
let host = host.unwrap_or_else(|_| host_old.unwrap_or_else(|_| "127.0.0.1".to_string()));
79+
let port = port
80+
.unwrap_or_else(|_| port_old.unwrap_or_else(|_| "8080".to_string()))
7481
.parse::<u16>()
7582
.expect("Port must be a number.");
7683

packages/perseus-cli/src/deploy.rs

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ fn deploy_full(dir: PathBuf, output: String, integration: Integration) -> Result
3535
standalone: true,
3636
integration,
3737
watch: false,
38+
// These have no impact if `no_run` is `true` (which it is), so we can use the defaults here
39+
host: "127.0.0.1".to_string(),
40+
port: 8080,
3841
},
3942
)?;
4043
if serve_exit_code != 0 {

packages/perseus-cli/src/parse.rs

+12
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ pub struct ServeOpts {
105105
/// Watch the files in your working directory for changes (exluding `target/` and `.perseus/`)
106106
#[clap(short, long)]
107107
pub watch: bool,
108+
/// Where to host your exported app
109+
#[clap(long, default_value = "127.0.0.1")]
110+
pub host: String,
111+
/// The port to host your exported app on
112+
#[clap(long, default_value = "8080")]
113+
pub port: u16,
108114
}
109115
/// Removes `.perseus/` entirely for updates or to fix corruptions
110116
#[derive(Parser)]
@@ -162,4 +168,10 @@ pub struct SnoopServeOpts {
162168
/// The server integration to use
163169
#[clap(short, long, default_value = "warp")]
164170
pub integration: Integration,
171+
/// Where to host your exported app
172+
#[clap(long, default_value = "127.0.0.1")]
173+
pub host: String,
174+
/// The port to host your exported app on
175+
#[clap(long, default_value = "8080")]
176+
pub port: u16,
165177
}

packages/perseus-cli/src/serve.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ fn run_server(
163163
cmd: server_exec_path,
164164
source: err,
165165
})?;
166-
// Figure out what host/port the app will be live on
167-
let host = env::var("HOST").unwrap_or_else(|_| "localhost".to_string());
168-
let port = env::var("PORT")
166+
// Figure out what host/port the app will be live on (these have been set by the system)
167+
let host = env::var("PERSEUS_HOST").unwrap_or_else(|_| "localhost".to_string());
168+
let port = env::var("PERSEUS_PORT")
169169
.unwrap_or_else(|_| "8080".to_string())
170170
.parse::<u16>()
171171
.map_err(|err| ExecutionError::PortNotNumber { source: err })?;
172172
// Give the user a nice informational message
173173
println!(
174-
" {} {} Your app is now live on <http://{host}:{port}>! To change this, re-run this command with different settings of the HOST/PORT environment variables.",
174+
" {} {} Your app is now live on <http://{host}:{port}>! To change this, re-run this command with different settings for `--host` and `--port`.",
175175
style(format!("[{}/{}]", num_steps, num_steps)).bold().dim(),
176176
SERVING,
177177
host=host,
@@ -199,6 +199,10 @@ fn run_server(
199199
/// Builds the subcrates to get a directory that we can serve and then serves it. If possible, this will return the path to the server
200200
/// executable so that it can be used in deployment.
201201
pub fn serve(dir: PathBuf, opts: ServeOpts) -> Result<(i32, Option<String>), ExecutionError> {
202+
// Set the environment variables for the host and port
203+
env::set_var("PERSEUS_HOST", opts.host);
204+
env::set_var("PERSEUS_PORT", opts.port.to_string());
205+
202206
let spinners = MultiProgress::new();
203207
let did_build = !opts.no_build;
204208
let should_run = !opts.no_run;

packages/perseus-cli/src/snoop.rs

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ pub fn snoop_wasm_build(dir: PathBuf, opts: SnoopWasmOpts) -> Result<i32, Execut
6666

6767
/// Runs the commands to run the server directly so the user can see detailed logs.
6868
pub fn snoop_server(dir: PathBuf, opts: SnoopServeOpts) -> Result<i32, ExecutionError> {
69+
// Set the environment variables for the host and port
70+
env::set_var("PERSEUS_HOST", opts.host);
71+
env::set_var("PERSEUS_PORT", opts.port.to_string());
72+
6973
let target = dir.join(".perseus/server");
7074
run_cmd_directly(
7175
format!(

0 commit comments

Comments
 (0)