Skip to content

Commit

Permalink
feat: serve alias
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau authored and ctron committed Sep 19, 2024
1 parent af68b84 commit 18213c8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ ignore = []
addresses = ["127.0.0.1"]
# The port to serve on.
port = 8080
# Aliases to serve, typically found in an /etc/hosts file.
aliases = ["http://localhost.mywebsite.com"]
# Open a browser tab once the initial build is complete.
open = false
# Whether to disable fallback to index.html for missing files.
Expand Down
8 changes: 8 additions & 0 deletions schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@
"format": "uint16",
"minimum": 0.0
},
"aliases": {
"description": "The aliases to serve on.",
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"prefer_address_family": {
"anyOf": [
{
Expand Down
1 change: 1 addition & 0 deletions site/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ This section lets you override how this works.
[serve]
addresses = ["127.0.0.1"] # The address to serve on.
port = 8080 # The port to serve on.
aliases = ["http://localhost.mywebsite.com"] # The aliases to serve on.
open = false # Open a browser tab once the initial build is complete.
no_spa = false # Whether to disable fallback to index.html for missing files.
no_autoreload = false # Disable auto-reload of the web app.
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct Serve {
/// The port to serve on [default: 8080]
#[arg(long, env = "TRUNK_SERVE_PORT")]
pub port: Option<u16>,
/// The aliases to serve on
#[arg(short, long, env = "TRUNK_SERVE_ALIAS")]
pub aliases: Option<Vec<String>>,
/// Open a browser tab once the initial build is complete [default: false]
#[arg(long, env = "TRUNK_SERVE_OPEN")]
#[arg(default_missing_value="true", num_args=0..=1)]
Expand Down Expand Up @@ -106,6 +109,7 @@ impl Serve {
address,
prefer_address_family,
port,
aliases,
open,
proxy:
ProxyArgs {
Expand All @@ -131,6 +135,7 @@ impl Serve {

config.serve.addresses = address.unwrap_or(config.serve.addresses);
config.serve.port = port.unwrap_or(config.serve.port);
config.serve.aliases = aliases.unwrap_or(config.serve.aliases);
config.serve.open = open.unwrap_or(config.serve.open);
config.serve.prefer_address_family =
prefer_address_family.or(config.serve.prefer_address_family);
Expand Down
4 changes: 4 additions & 0 deletions src/config/models/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub struct Serve {
/// The port to serve on [default: 8080]
#[serde(default = "default::port")]
pub port: u16,
/// The aliases to serve on.
#[serde(default)]
pub aliases: Vec<String>,
/// Open a browser tab once the initial build is complete [default: false]
#[serde(default)]
pub open: bool,
Expand Down Expand Up @@ -84,6 +87,7 @@ impl Default for Serve {
Self {
address: None,
addresses: vec![],
aliases: vec![],
prefer_address_family: None,
port: default::port(),
open: false,
Expand Down
4 changes: 4 additions & 0 deletions src/config/rt/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct RtcServe {
pub addresses: Vec<IpAddr>,
/// The port to serve on.
pub port: u16,
/// The aliases to serve on.
pub aliases: Vec<String>,
/// Open a browser tab once the initial build is complete.
pub open: bool,
/// Any proxies configured to run along with the server.
Expand Down Expand Up @@ -76,6 +78,7 @@ impl RtcServe {
addresses,
prefer_address_family,
port,
aliases,
open: _,
// auto-reload is handle by the builder options
no_autoreload: _,
Expand Down Expand Up @@ -106,6 +109,7 @@ impl RtcServe {
watch,
addresses: build_address_list(prefer_address_family, addresses),
port,
aliases,
open,
proxies: config.proxies.0,
no_spa,
Expand Down
13 changes: 11 additions & 2 deletions src/serve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ impl ServeSystem {
.map(|addr| (*addr, cfg.port).into())
.collect::<Vec<_>>();

show_listening(&cfg, &addr, &serve_base_url);
let aliases = cfg
.aliases
.iter()
.map(|alias| format!("{alias}:{}", cfg.port))
.collect::<Vec<_>>();

show_listening(&cfg, &addr, &aliases, &serve_base_url);

let server = run_server(addr, cfg.tls.clone(), router, shutdown_rx);

Expand All @@ -155,7 +161,7 @@ impl ServeSystem {
}

/// show where `serve` is listening
fn show_listening(cfg: &RtcServe, addr: &[SocketAddr], base: &str) {
fn show_listening(cfg: &RtcServe, addr: &[SocketAddr], aliases: &[String], base: &str) {
let prefix = if cfg.tls.is_some() { "https" } else { "http" };

// prepare interface addresses
Expand Down Expand Up @@ -198,6 +204,9 @@ fn show_listening(cfg: &RtcServe, addr: &[SocketAddr], base: &str) {
if is_loopback(address) { LOCAL } else { NETWORK },
);
}
for alias in aliases {
tracing::info!(" {alias}");
}
}

async fn run_server(
Expand Down

0 comments on commit 18213c8

Please sign in to comment.