Skip to content

Commit c389e03

Browse files
committed
feat: Implement dynamic theming based on COMPANY_NAME environment variable
- Added support for dynamic theming in the store front application. - Introduced COMPANY_NAME and PRODUCT_SERVICE_URL environment variables. - Updated Dockerfile to substitute environment variables in nginx config and HTML. - Created a new useTheme composable to manage theme application and initialization. - Defined themes for Contoso and Zava in a new themes configuration file. - Updated TopNav component to use dynamic logo based on the selected theme. - Modified styles to utilize CSS custom properties for theming. - Enhanced ShoppingCartView with theme-aware styles. - Added runtime configuration endpoint in Vite for fetching company name. - Updated README with usage instructions for dynamic theming.
1 parent ca7f6fa commit c389e03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1042
-287
lines changed

azure.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ services:
5151
dir: ../../kustomize/overlays/azd/product-service
5252
edits:
5353
- set image product-service=${SERVICE_PRODUCT_SERVICE_IMAGE_NAME}
54+
env:
55+
AI_SERVICE_URL: "http://ai-service:5001/"
56+
COMPANY_NAME: ${COMPANY_NAME}
5457
hooks:
5558
postdeploy:
5659
posix:
@@ -141,6 +144,8 @@ services:
141144
dir: ../../kustomize/overlays/azd/store-front
142145
edits:
143146
- set image store-front=${SERVICE_STORE_FRONT_IMAGE_NAME}
147+
env:
148+
COMPANY_NAME: ${COMPANY_NAME}
144149
hooks:
145150
postdeploy:
146151
posix:
@@ -166,6 +171,8 @@ services:
166171
dir: ../../kustomize/overlays/azd/store-admin
167172
edits:
168173
- set image store-admin=${SERVICE_STORE_ADMIN_IMAGE_NAME}
174+
env:
175+
COMPANY_NAME: ${COMPANY_NAME}
169176
hooks:
170177
postdeploy:
171178
posix:

kustomize/overlays/azd/product-service/deployment.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ spec:
1919
image: product-service:latest
2020
ports:
2121
- containerPort: 3002
22-
env:
23-
- name: AI_SERVICE_URL
24-
value: "http://ai-service:5001/"
22+
envFrom:
23+
- configMapRef:
24+
name: product-service
2525
resources:
2626
requests:
2727
cpu: 1m

kustomize/overlays/azd/product-service/kustomization.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ kind: Kustomization
33
resources:
44
- deployment.yaml
55
- service.yaml
6+
configMapGenerator:
7+
- envs:
8+
- .env
9+
name: product-service

kustomize/overlays/azd/store-admin/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ spec:
2020
ports:
2121
- containerPort: 8081
2222
name: store-admin # container images hosted on ghcr.io and will be removed in future releases
23+
envFrom:
24+
- configMapRef:
25+
name: store-admin
2326
resources:
2427
requests:
2528
cpu: 1m

kustomize/overlays/azd/store-admin/kustomization.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ kind: Kustomization
33
resources:
44
- deployment.yaml
55
- service.yaml
6+
configMapGenerator:
7+
- envs:
8+
- .env
9+
name: store-admin

kustomize/overlays/azd/store-front/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ spec:
2020
ports:
2121
- containerPort: 8080
2222
name: store-front
23+
envFrom:
24+
- configMapRef:
25+
name: store-front
2326
resources:
2427
requests:
2528
cpu: 1m

kustomize/overlays/azd/store-front/kustomization.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ kind: Kustomization
33
resources:
44
- deployment.yaml
55
- service.yaml
6+
configMapGenerator:
7+
- envs:
8+
- .env
9+
name: store-front

src/product-service/src/configuration.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct Settings {
99
pub wasm_bin_path: PathBuf,
1010
tcp_listener: Option<TcpListener>,
1111
pub ai_service_url: String,
12+
pub company_name: String,
1213
}
1314

1415
impl Settings {
@@ -17,6 +18,8 @@ impl Settings {
1718
var("WASM_RULE_ENGINE_PATH").unwrap_or_else(|_| "./tests/rule_engine.wasm".to_string());
1819
let ai_service_url =
1920
std::env::var("AI_SERVICE_URL").unwrap_or_else(|_| "http://127.0.0.1:5001".to_string());
21+
let company_name =
22+
std::env::var("COMPANY_NAME").unwrap_or_else(|_| "Contoso".to_string());
2023
Settings {
2124
max_size: 262_144,
2225
log_level: "info".to_string(),
@@ -25,6 +28,7 @@ impl Settings {
2528
wasm_bin_path: PathBuf::from(wasm_bin_path_env),
2629
tcp_listener: None,
2730
ai_service_url: ai_service_url.trim_end_matches('/').to_string(),
31+
company_name,
2832
}
2933
}
3034

src/product-service/src/data.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::configuration::Settings;
22
use crate::model::Product;
33

4-
pub fn fetch_products(_settings: &Settings) -> Vec<Product> {
4+
pub fn fetch_products(settings: &Settings) -> Vec<Product> {
55
vec![
66
Product {
77
id: 1,
8-
name: "Contoso Catnip's Friend".to_string(),
8+
name: format!("{} Catnip's Friend", settings.company_name),
99
price: 9.99,
10-
description: "Watch your feline friend embark on a fishing adventure with Contoso Catnip's Friend toy. Packed with irresistible catnip and dangling fish lure.".to_string(),
10+
description: format!("Watch your feline friend embark on a fishing adventure with {} Catnip's Friend toy. Packed with irresistible catnip and dangling fish lure.", settings.company_name),
1111
image: "/catnip.jpg".to_string()
1212
},
1313
Product {
@@ -61,9 +61,9 @@ pub fn fetch_products(_settings: &Settings) -> Vec<Product> {
6161
},
6262
Product {
6363
id: 9,
64-
name: "Contoso Claw's Crabby Cat Toy".to_string(),
64+
name: format!("{} Claw's Crabby Cat Toy", settings.company_name),
6565
price: 3.99,
66-
description: "Watch your cat go crazy for Contoso Claw's Crabby Cat Toy. This crinkly and catnip-filled toy will awaken their hunting instincts and provide endless entertainment.".to_string(),
66+
description: format!("Watch your cat go crazy for {} Claw's Crabby Cat Toy. This crinkly and catnip-filled toy will awaken their hunting instincts and provide endless entertainment.", settings.company_name),
6767
image: "/crabby.jpg".to_string()
6868
},
6969
Product {

src/store-admin/Dockerfile

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,25 @@ EXPOSE 8081
2727
# Set the build argument for the app version number
2828
ARG APP_VERSION=0.1.0
2929

30-
# Set the environment variable for the app version number
30+
# Set the environment variables
3131
ENV APP_VERSION=$APP_VERSION
32+
ENV COMPANY_NAME=Contoso
33+
ENV PRODUCT_SERVICE_URL=http://product-service:3002/
34+
ENV MAKELINE_SERVICE_URL=http://makeline-service:3001/
3235

3336
# Copy the nginx configuration template to the container
3437
COPY nginx.conf /etc/nginx/conf.d/nginx.conf.template
3538

36-
# Update the nginx configuration to use the app version number
37-
# and Copy the nginx configuration template to the container
38-
RUN envsubst '${APP_VERSION}' < /etc/nginx/conf.d/nginx.conf.template > /etc/nginx/conf.d/default.conf
39+
# Create a startup script to substitute environment variables in both nginx config and HTML
40+
RUN echo '#!/bin/sh' > /docker-entrypoint.d/30-substitute-env.sh && \
41+
echo 'envsubst '"'"'${APP_VERSION} ${COMPANY_NAME} ${PRODUCT_SERVICE_URL} ${MAKELINE_SERVICE_URL}'"'"' < /etc/nginx/conf.d/nginx.conf.template > /etc/nginx/conf.d/default.conf' >> /docker-entrypoint.d/30-substitute-env.sh && \
42+
echo '# Update the page title based on company name' >> /docker-entrypoint.d/30-substitute-env.sh && \
43+
echo 'if [ "$COMPANY_NAME" = "Zava" ]; then' >> /docker-entrypoint.d/30-substitute-env.sh && \
44+
echo ' sed -i "s/<title>Pet Store Admin Portal<\/title>/<title>Zava Pet Store Admin Portal<\/title>/g" /usr/share/nginx/html/index.html' >> /docker-entrypoint.d/30-substitute-env.sh && \
45+
echo 'elif [ "$COMPANY_NAME" = "Contoso" ]; then' >> /docker-entrypoint.d/30-substitute-env.sh && \
46+
echo ' sed -i "s/<title>Pet Store Admin Portal<\/title>/<title>Contoso Pet Store Admin Portal<\/title>/g" /usr/share/nginx/html/index.html' >> /docker-entrypoint.d/30-substitute-env.sh && \
47+
echo 'fi' >> /docker-entrypoint.d/30-substitute-env.sh && \
48+
chmod +x /docker-entrypoint.d/30-substitute-env.sh
3949

4050
# Start the app
4151
CMD ["nginx", "-g", "daemon off;"]

0 commit comments

Comments
 (0)