Skip to content

Commit 31057d3

Browse files
committed
Added ability to send variables to the application and proxying options
1 parent dc7dbd6 commit 31057d3

38 files changed

+636
-375
lines changed

Cargo.lock

Lines changed: 50 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ script = [
3333

3434
[tasks.demo-watch]
3535
script = [
36-
'cargo run --bin vertigo -- watch vertigo-demo --dest-dir=demo_build'
36+
'cargo run --bin vertigo -- watch vertigo-demo --dest-dir=demo_build --env ws_chat=ws://127.0.0.1:3333/ws'
3737
]
3838

3939
[tasks.demo-start]
4040
script = [
4141
"cargo run --bin vertigo -- build vertigo-demo --dest-dir=demo_build",
42-
"cargo run --bin vertigo -- serve --dest-dir=demo_build"
42+
"cargo run --bin vertigo -- serve --dest-dir=demo_build --env ws_chat=ws://127.0.0.1:3333/ws"
4343
]
4444

4545
[tasks.demo]

crates/vertigo-cli/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ log = "0.4"
2929
env_logger = "0.10"
3030
tokio = { version = "1.25", features = ["full"] }
3131
tokio-stream = "0.1"
32-
axum = "0.6"
33-
axum-extra = "0.7"
32+
axum = { version = "0.6.12", features = ["macros"] }
33+
axum-extra = "0.7.2"
3434
tower-http = { version = "0.4", features = ["fs"] }
3535
reqwest = "0.11.14"
3636
notify = "5.1.0"

crates/vertigo-cli/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ mod check_env;
66
mod command;
77
mod watch;
88
mod spawn;
9+
mod utils;
10+
911
use clap::{Parser, Subcommand};
1012

1113
use build::BuildOpts;
@@ -46,7 +48,7 @@ pub async fn main() -> Result<(), i32> {
4648
new::run(opts)
4749
}
4850
Command::Serve(opts) => {
49-
serve::run(opts).await
51+
serve::run(opts, None).await
5052
}
5153
Command::Watch(opts) => {
5254
watch::run(opts).await

crates/vertigo-cli/src/serve/html/html_element.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,15 @@ impl HtmlElement {
307307
}
308308
}
309309

310-
pub fn attr(mut self, name: &'static str, value: impl Into<String>) -> Self {
310+
pub fn attr(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
311311
self.attr.set(name, value);
312312
self
313313
}
314314

315+
pub fn add_attr(&mut self, name: impl Into<String>, value: impl Into<String>) {
316+
self.attr.set(name, value);
317+
}
318+
315319
pub fn add_first_child(&mut self, child: HtmlNode) {
316320
self.children.push_front(child);
317321
}

crates/vertigo-cli/src/serve/html/html_response.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::{
2121
html_element::{
2222
HtmlElement,
2323
HtmlDocument
24-
},
24+
}, HtmlNode,
2525
};
2626

2727
enum FetchStatus {
@@ -45,16 +45,18 @@ pub struct HtmlResponse {
4545
inst: WasmInstance,
4646
all_elements: AllElements,
4747
fetch: HashMap<Arc<FetchRequest>, FetchStatus>,
48+
env: HashMap<String, String>
4849
}
4950

5051
impl HtmlResponse {
51-
pub fn new(sender: UnboundedSender<Message>, mount_path: &MountPathConfig, inst: WasmInstance) -> Self {
52+
pub fn new(sender: UnboundedSender<Message>, mount_path: &MountPathConfig, inst: WasmInstance, env: HashMap<String, String>) -> Self {
5253
Self {
5354
sender,
5455
mount_path: mount_path.clone(),
5556
inst,
5657
all_elements: AllElements::new(),
5758
fetch: HashMap::new(),
59+
env,
5860
}
5961
}
6062

@@ -79,15 +81,16 @@ impl HtmlResponse {
7981

8082
let css = css.into_iter().collect::<VecDeque<_>>();
8183

82-
let root_ok = if let Some(element) = root_html.get_element() {
83-
element.name == "html"
84-
} else {
85-
false
86-
};
84+
if let HtmlNode::Element(html) = &mut root_html {
85+
if html.name != "html" {
86+
return (StatusCode::INTERNAL_SERVER_ERROR, "root: the html element was expected".into());
87+
}
8788

88-
if !root_ok {
89-
let message = "root: the html element was expected".into();
90-
return (StatusCode::INTERNAL_SERVER_ERROR, message);
89+
for (env_name, env_value) in &self.env {
90+
html.add_attr(format!("data-env-{env_name}"), env_value);
91+
}
92+
} else {
93+
return (StatusCode::INTERNAL_SERVER_ERROR, "root: the html element was expected".into());
9194
}
9295

9396
let is_exist_head = root_html.modify(&[("head", 0)], move |_head| {});

crates/vertigo-cli/src/serve/mount_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn read_index(dest_dir: &str) -> Result<IndexModel, i32> {
5353
let index_html = match std::fs::read_to_string(&index_path) {
5454
Ok(data) => data,
5555
Err(err) => {
56-
log::error!("File read error: file={index_path:?}, error={err}");
56+
log::error!("File read error: file={index_path:?}, error={err}, dest_dir={dest_dir}");
5757
return Err(-1);
5858
}
5959
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
use std::collections::HashMap;
2+
3+
#[derive(Clone)]
14
pub struct RequestState {
25
pub url: String,
6+
pub env: HashMap<String, String>,
37
}
8+
9+
impl RequestState {
10+
pub fn env(&self, name: impl Into<String>) -> Option<String> {
11+
let name = name.into();
12+
let Some(value) = self.env.get(&name) else {
13+
return None;
14+
};
15+
16+
Some(value.clone())
17+
}
18+
}
19+

0 commit comments

Comments
 (0)