Skip to content

Commit 2c431f4

Browse files
🐛 server does not use tera template (project-openubl#442)
Signed-off-by: Carlos Feria <[email protected]>
1 parent b11a2f2 commit 2c431f4

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

server/ui/client/rsbuild.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ export default defineConfig({
109109
to: ".",
110110
},
111111
{
112-
from: brandingPath,
112+
from: faviconPath,
113113
to: ".",
114114
},
115115
{
116-
from: faviconPath,
117-
to: ".",
116+
from: brandingPath,
117+
to: "branding",
118118
},
119119
],
120120
},

server/ui/crate/src/lib.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use anyhow::{anyhow, bail, Context};
44
use base64::prelude::BASE64_STANDARD;
55
use base64::Engine;
66
use serde::Serialize;
7+
use serde_json::Value;
78
use static_files::resource::new_resource;
89
use static_files::Resource;
10+
use std::cmp::max;
911
use std::collections::HashMap;
1012
use std::str::from_utf8;
1113
use std::sync::OnceLock;
@@ -28,14 +30,31 @@ pub fn openubl_ui_resources() -> HashMap<&'static str, Resource> {
2830
resources
2931
}
3032

31-
pub fn generate_index_html(ui: &UI, template_file: String) -> tera::Result<String> {
32-
let template = template_file.replace("<%=", "{{").replace("%>", "}}");
33+
pub fn generate_index_html(
34+
ui: &UI,
35+
template_file: String,
36+
branding_file_content: String,
37+
) -> tera::Result<String> {
38+
let template = template_file
39+
.replace("<%=", "{{")
40+
.replace("%>", "}}")
41+
.replace(
42+
"?? branding.application.title",
43+
"| default(value=branding.application.title)",
44+
)
45+
.replace(
46+
"?? branding.application.title",
47+
"| default(value=branding.application.title)",
48+
);
3349

3450
let env_json = serde_json::to_string(&ui)?;
3551
let env_base64 = BASE64_STANDARD.encode(env_json.as_bytes());
3652

53+
let branding: Value = serde_json::from_str(&branding_file_content)?;
54+
3755
let mut context = tera::Context::new();
3856
context.insert("_env", &env_base64);
57+
context.insert("branding", &branding);
3958

4059
tera::Tera::one_off(&template, &context, true)
4160
}
@@ -44,19 +63,28 @@ pub fn openubl_ui(ui: &UI) -> anyhow::Result<HashMap<&'static str, Resource>> {
4463
let mut resources = generate();
4564

4665
let template_file = resources.get("index.html.ejs");
66+
let branding_file_content = resources.get("branding/strings.json");
4767

4868
let result = INDEX_HTML.get_or_init(|| {
49-
if let Some(template_file) = template_file {
50-
let modified = template_file.modified;
69+
if let (Some(template_file), Some(branding_file_content)) =
70+
(template_file, branding_file_content)
71+
{
72+
let modified = max(template_file.modified, branding_file_content.modified);
5173
let template_file =
5274
from_utf8(template_file.data).context("cannot interpret template as UTF-8")?;
75+
let branding_file_content = from_utf8(branding_file_content.data)
76+
.context("cannot interpret branding as UTF-8")?;
5377
Ok((
54-
generate_index_html(ui, template_file.to_string())
55-
.expect("cannot generate index.html"),
78+
generate_index_html(
79+
ui,
80+
template_file.to_string(),
81+
branding_file_content.to_string(),
82+
)
83+
.expect("cannot generate index.html"),
5684
modified,
5785
))
5886
} else {
59-
bail!("Missing template");
87+
bail!("Missing template or branding");
6088
}
6189
});
6290

0 commit comments

Comments
 (0)