Skip to content

Commit 18213c8

Browse files
simbleauctron
authored andcommitted
feat: serve alias
1 parent af68b84 commit 18213c8

File tree

7 files changed

+35
-2
lines changed

7 files changed

+35
-2
lines changed

Trunk.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ ignore = []
4242
addresses = ["127.0.0.1"]
4343
# The port to serve on.
4444
port = 8080
45+
# Aliases to serve, typically found in an /etc/hosts file.
46+
aliases = ["http://localhost.mywebsite.com"]
4547
# Open a browser tab once the initial build is complete.
4648
open = false
4749
# Whether to disable fallback to index.html for missing files.

schemas/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@
454454
"format": "uint16",
455455
"minimum": 0.0
456456
},
457+
"aliases": {
458+
"description": "The aliases to serve on.",
459+
"default": [],
460+
"type": "array",
461+
"items": {
462+
"type": "string"
463+
}
464+
},
457465
"prefer_address_family": {
458466
"anyOf": [
459467
{

site/content/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ This section lets you override how this works.
7878
[serve]
7979
addresses = ["127.0.0.1"] # The address to serve on.
8080
port = 8080 # The port to serve on.
81+
aliases = ["http://localhost.mywebsite.com"] # The aliases to serve on.
8182
open = false # Open a browser tab once the initial build is complete.
8283
no_spa = false # Whether to disable fallback to index.html for missing files.
8384
no_autoreload = false # Disable auto-reload of the web app.

src/cmd/serve.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub struct Serve {
2727
/// The port to serve on [default: 8080]
2828
#[arg(long, env = "TRUNK_SERVE_PORT")]
2929
pub port: Option<u16>,
30+
/// The aliases to serve on
31+
#[arg(short, long, env = "TRUNK_SERVE_ALIAS")]
32+
pub aliases: Option<Vec<String>>,
3033
/// Open a browser tab once the initial build is complete [default: false]
3134
#[arg(long, env = "TRUNK_SERVE_OPEN")]
3235
#[arg(default_missing_value="true", num_args=0..=1)]
@@ -106,6 +109,7 @@ impl Serve {
106109
address,
107110
prefer_address_family,
108111
port,
112+
aliases,
109113
open,
110114
proxy:
111115
ProxyArgs {
@@ -131,6 +135,7 @@ impl Serve {
131135

132136
config.serve.addresses = address.unwrap_or(config.serve.addresses);
133137
config.serve.port = port.unwrap_or(config.serve.port);
138+
config.serve.aliases = aliases.unwrap_or(config.serve.aliases);
134139
config.serve.open = open.unwrap_or(config.serve.open);
135140
config.serve.prefer_address_family =
136141
prefer_address_family.or(config.serve.prefer_address_family);

src/config/models/serve.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub struct Serve {
2323
/// The port to serve on [default: 8080]
2424
#[serde(default = "default::port")]
2525
pub port: u16,
26+
/// The aliases to serve on.
27+
#[serde(default)]
28+
pub aliases: Vec<String>,
2629
/// Open a browser tab once the initial build is complete [default: false]
2730
#[serde(default)]
2831
pub open: bool,
@@ -84,6 +87,7 @@ impl Default for Serve {
8487
Self {
8588
address: None,
8689
addresses: vec![],
90+
aliases: vec![],
8791
prefer_address_family: None,
8892
port: default::port(),
8993
open: false,

src/config/rt/serve.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub struct RtcServe {
2828
pub addresses: Vec<IpAddr>,
2929
/// The port to serve on.
3030
pub port: u16,
31+
/// The aliases to serve on.
32+
pub aliases: Vec<String>,
3133
/// Open a browser tab once the initial build is complete.
3234
pub open: bool,
3335
/// Any proxies configured to run along with the server.
@@ -76,6 +78,7 @@ impl RtcServe {
7678
addresses,
7779
prefer_address_family,
7880
port,
81+
aliases,
7982
open: _,
8083
// auto-reload is handle by the builder options
8184
no_autoreload: _,
@@ -106,6 +109,7 @@ impl RtcServe {
106109
watch,
107110
addresses: build_address_list(prefer_address_family, addresses),
108111
port,
112+
aliases,
109113
open,
110114
proxies: config.proxies.0,
111115
no_spa,

src/serve/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ impl ServeSystem {
138138
.map(|addr| (*addr, cfg.port).into())
139139
.collect::<Vec<_>>();
140140

141-
show_listening(&cfg, &addr, &serve_base_url);
141+
let aliases = cfg
142+
.aliases
143+
.iter()
144+
.map(|alias| format!("{alias}:{}", cfg.port))
145+
.collect::<Vec<_>>();
146+
147+
show_listening(&cfg, &addr, &aliases, &serve_base_url);
142148

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

@@ -155,7 +161,7 @@ impl ServeSystem {
155161
}
156162

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

161167
// prepare interface addresses
@@ -198,6 +204,9 @@ fn show_listening(cfg: &RtcServe, addr: &[SocketAddr], base: &str) {
198204
if is_loopback(address) { LOCAL } else { NETWORK },
199205
);
200206
}
207+
for alias in aliases {
208+
tracing::info!(" {alias}");
209+
}
201210
}
202211

203212
async fn run_server(

0 commit comments

Comments
 (0)