Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for baseUrl #2609

Merged
merged 12 commits into from
Jul 22, 2024
9 changes: 5 additions & 4 deletions src/frontend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (fe *frontendServer) addToCartHandler(w http.ResponseWriter, r *http.Reques
renderHTTPError(log, r, w, errors.Wrap(err, "failed to add to cart"), http.StatusInternalServerError)
return
}
w.Header().Set("location", "/cart")
w.Header().Set("location", baseUrl + "/cart")
w.WriteHeader(http.StatusFound)
}

Expand All @@ -244,7 +244,7 @@ func (fe *frontendServer) emptyCartHandler(w http.ResponseWriter, r *http.Reques
renderHTTPError(log, r, w, errors.Wrap(err, "failed to empty cart"), http.StatusInternalServerError)
return
}
w.Header().Set("location", "/")
w.Header().Set("location", baseUrl + "/")
w.WriteHeader(http.StatusFound)
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func (fe *frontendServer) logoutHandler(w http.ResponseWriter, r *http.Request)
c.MaxAge = -1
http.SetCookie(w, c)
}
w.Header().Set("Location", "/")
w.Header().Set("Location", baseUrl + "/")
w.WriteHeader(http.StatusFound)
}

Expand Down Expand Up @@ -516,7 +516,7 @@ func (fe *frontendServer) setCurrencyHandler(w http.ResponseWriter, r *http.Requ
}
referer := r.Header.Get("referer")
if referer == "" {
referer = "/"
referer = baseUrl + "/"
}
w.Header().Set("Location", referer)
w.WriteHeader(http.StatusFound)
Expand Down Expand Up @@ -560,6 +560,7 @@ func injectCommonTemplateData(r *http.Request, payload map[string]interface{}) m
"deploymentDetails": deploymentDetailsMap,
"frontendMessage": frontendMessage,
"currentYear": time.Now().Year(),
"baseUrl": baseUrl,
}

for k, v := range payload {
Expand Down
35 changes: 20 additions & 15 deletions src/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ var (
"CAD": true,
"JPY": true,
"GBP": true,
"TRY": true}
"TRY": true,
}

baseUrl = ""
)

type ctxKeySessionID struct{}
Expand Down Expand Up @@ -104,6 +107,8 @@ func main() {
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{}, propagation.Baggage{}))

baseUrl = os.Getenv("BASE_URL")

if os.Getenv("ENABLE_TRACING") == "1" {
log.Info("Tracing enabled.")
initTracing(log, ctx, svc)
Expand Down Expand Up @@ -141,20 +146,20 @@ func main() {
mustConnGRPC(ctx, &svc.adSvcConn, svc.adSvcAddr)

r := mux.NewRouter()
r.HandleFunc("/", svc.homeHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/product/{id}", svc.productHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", svc.viewCartHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", svc.addToCartHandler).Methods(http.MethodPost)
r.HandleFunc("/cart/empty", svc.emptyCartHandler).Methods(http.MethodPost)
r.HandleFunc("/setCurrency", svc.setCurrencyHandler).Methods(http.MethodPost)
r.HandleFunc("/logout", svc.logoutHandler).Methods(http.MethodGet)
r.HandleFunc("/cart/checkout", svc.placeOrderHandler).Methods(http.MethodPost)
r.HandleFunc("/assistant", svc.assistantHandler).Methods(http.MethodGet)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
r.HandleFunc("/robots.txt", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "User-agent: *\nDisallow: /") })
r.HandleFunc("/_healthz", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "ok") })
r.HandleFunc("/product-meta/{ids}", svc.getProductByID).Methods(http.MethodGet)
r.HandleFunc("/bot", svc.chatBotHandler).Methods(http.MethodPost)
r.HandleFunc(baseUrl + "/", svc.homeHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(baseUrl + "/product/{id}", svc.productHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(baseUrl + "/cart", svc.viewCartHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(baseUrl + "/cart", svc.addToCartHandler).Methods(http.MethodPost)
r.HandleFunc(baseUrl + "/cart/empty", svc.emptyCartHandler).Methods(http.MethodPost)
r.HandleFunc(baseUrl + "/setCurrency", svc.setCurrencyHandler).Methods(http.MethodPost)
r.HandleFunc(baseUrl + "/logout", svc.logoutHandler).Methods(http.MethodGet)
r.HandleFunc(baseUrl + "/cart/checkout", svc.placeOrderHandler).Methods(http.MethodPost)
r.HandleFunc(baseUrl + "/assistant", svc.assistantHandler).Methods(http.MethodGet)
r.PathPrefix(baseUrl + "/static/").Handler(http.StripPrefix(baseUrl + "/static/", http.FileServer(http.Dir("./static/"))))
r.HandleFunc(baseUrl + "/robots.txt", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "User-agent: *\nDisallow: /") })
r.HandleFunc(baseUrl + "/_healthz", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "ok") })
r.HandleFunc(baseUrl + "/product-meta/{ids}", svc.getProductByID).Methods(http.MethodGet)
r.HandleFunc(baseUrl + "/bot", svc.chatBotHandler).Methods(http.MethodPost)

var handler http.Handler = r
handler = &logHandler{log: log, next: handler} // add logging
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/templates/ad.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
<div class="container py-3 px-lg-5 py-lg-5">
<div role="alert">
<strong>Ad</strong>
<a href="{{.RedirectUrl}}" rel="nofollow noopener noreferrer" target="_blank">
{{.Text}}
<a href="{{$.baseUrl}}{{.ad.RedirectUrl}}" rel="nofollow noopener noreferrer" target="_blank">
{{.ad.Text}}
</a>
</div>
</div>
{{ end }}
{{ end }}
24 changes: 20 additions & 4 deletions src/frontend/templates/assistant.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<!--
Copyright 2020 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

{{ define "assistant" }}

{{ template "header" . }}
Expand Down Expand Up @@ -115,7 +131,7 @@
botMessages.scrollTo(0, botMessages.scrollHeight);

// Request a response from the Shopping Assistant
const response = await fetch("/bot", {
const response = await fetch("{{ $.baseUrl }}/bot", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -146,7 +162,7 @@
// For each product...
for (const id of extractedIds) {
// Retrieve product metadata from the Product Catalog
const productResponse = await fetch("/product-meta/" + id, {
const productResponse = await fetch("{{ $.baseUrl }}/product-meta/" + id, {
method: "GET",
headers: {
"Content-Type": "application/json",
Expand All @@ -157,7 +173,7 @@
// Construct main product div
const botProductDiv = document.createElement("a");
botProductDiv.classList.add("bot-product");
botProductDiv.href = "/product/" + id;
botProductDiv.href = "{{ $.baseUrl }}/product/" + id;

// Construct product image
const botProductImg = document.createElement("img");
Expand All @@ -182,7 +198,7 @@
// Render products
botMessages.appendChild(botProductsDiv)
}

botMessages.scrollTo(0, botMessages.scrollHeight);

// Re-enable button and input field
Expand Down
24 changes: 12 additions & 12 deletions src/frontend/templates/cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

{{ define "cart" }}
{{ template "header" . }}

<div {{ with $.platform_css }} class="{{.}}" {{ end }}>
<span class="platform-flag">
{{$.platform_name}}
</span>
</div>

<main role="main" class="cart-sections">

{{ if eq (len $.items) 0 }}
<section class="empty-cart-section">
<h3>Your shopping cart is empty!</h3>
<p>Items you add to your shopping cart will appear here.</p>
<a class="cymbal-button-primary" href="/" role="button">Continue Shopping</a>
<a class="cymbal-button-primary" href="{{ $.baseUrl }}/" role="button">Continue Shopping</a>
</section>
{{ else }}
<section class="container">
Expand All @@ -42,11 +42,11 @@ <h3>Your shopping cart is empty!</h3>
<h3>Cart ({{ $.cart_size }})</h3>
</div>
<div class="col-8 pr-md-0 text-right">
<form method="POST" action="/cart/empty">
<form method="POST" action="{{ $.baseUrl }}/cart/empty">
<button class="cymbal-button-secondary cart-summary-empty-cart-button" type="submit">
Empty Cart
</button>
<a class="cymbal-button-primary" href="/" role="button">
<a class="cymbal-button-primary" href="{{ $.baseUrl }}/" role="button">
Continue Shopping
</a>
</form>
Expand All @@ -56,8 +56,8 @@ <h3>Cart ({{ $.cart_size }})</h3>
{{ range $.items }}
<div class="row cart-summary-item-row">
<div class="col-md-4 pl-md-0">
<a href="/product/{{.Item.Id}}">
<img class="img-fluid" alt="" src="{{.Item.Picture}}" />
<a href="{{ $.baseUrl }}/product/{{.Item.Id}}">
<img class="img-fluid" alt="" src="{{ $.baseUrl }}{{.Item.Picture}}" />
</a>
</div>
<div class="col-md-8 pr-md-0">
Expand Down Expand Up @@ -99,7 +99,7 @@ <h4>{{ .Item.Name }}</h4>

<div class="col-lg-5 offset-lg-1 col-xl-4">

<form class="cart-checkout-form" action="/cart/checkout" method="POST">
<form class="cart-checkout-form" action="{{ $.baseUrl }}/cart/checkout" method="POST">

<div class="row">
<div class="col">
Expand Down Expand Up @@ -187,7 +187,7 @@ <h3 class="payment-method-heading">Payment Method</h3>
<option value="11">November</option>
<option value="12">January</option>
</select>
<img src="/static/icons/Hipster_DownArrow.svg" alt="" class="cymbal-dropdown-chevron">
<img src="{{ $.baseUrl }}/static/icons/Hipster_DownArrow.svg" alt="" class="cymbal-dropdown-chevron">
</div>
<div class="col-md-4 cymbal-form-field">
<label for="credit_card_expiration_year">Year</label>
Expand All @@ -198,7 +198,7 @@ <h3 class="payment-method-heading">Payment Method</h3>
{{- end}}
>{{$y}}</option>{{end}}
</select>
<img src="/static/icons/Hipster_DownArrow.svg" alt="" class="cymbal-dropdown-chevron">
<img src="{{ $.baseUrl }}/static/icons/Hipster_DownArrow.svg" alt="" class="cymbal-dropdown-chevron">
</div>
<div class="col-md-3 cymbal-form-field">
<label for="credit_card_cvv">CVV</label>
Expand Down Expand Up @@ -226,8 +226,8 @@ <h3 class="payment-method-heading">Payment Method</h3>
</main>

{{ if $.recommendations }}
{{ template "recommendations" $.recommendations }}
{{ template "recommendations" $ }}
{{ end }}

{{ template "footer" . }}
{{ end }}
{{ end }}
30 changes: 15 additions & 15 deletions src/frontend/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Google+Symbols:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="/static/styles/styles.css">
<link rel="stylesheet" type="text/css" href="/static/styles/cart.css">
<link rel="stylesheet" type="text/css" href="/static/styles/order.css">
<link rel="stylesheet" type="text/css" href="/static/styles/bot.css">
<link rel="stylesheet" type="text/css" href="{{ $.baseUrl }}/static/styles/styles.css">
<link rel="stylesheet" type="text/css" href="{{ $.baseUrl }}/static/styles/cart.css">
<link rel="stylesheet" type="text/css" href="{{ $.baseUrl }}/static/styles/order.css">
<link rel="stylesheet" type="text/css" href="{{ $.baseUrl }}/static/styles/bot.css">
{{ if $.is_cymbal_brand }}
<link rel='shortcut icon' type='image/x-icon' href='/static/favicon-cymbal.ico' />
<link rel='shortcut icon' type='image/x-icon' href='{{ $.baseUrl }}/static/favicon-cymbal.ico' />
{{ else }}
<link rel='shortcut icon' type='image/x-icon' href='/static/favicon.ico' />
<link rel='shortcut icon' type='image/x-icon' href='{{ $.baseUrl }}/static/favicon.ico' />
{{ end }}
</head>

Expand All @@ -57,11 +57,11 @@
{{ end }}
<div class="navbar sub-navbar">
<div class="container d-flex justify-content-between">
<a href="/" class="navbar-brand d-flex align-items-center">
<a href="{{ $.baseUrl }}/" class="navbar-brand d-flex align-items-center">
{{ if $.is_cymbal_brand }}
<img src="/static/icons/Cymbal_NavLogo.svg" alt="" class="top-left-logo-cymbal" />
<img src="{{ $.baseUrl }}/static/icons/Cymbal_NavLogo.svg" alt="" class="top-left-logo-cymbal" />
{{ else }}
<img src="/static/icons/Hipster_NavLogo.svg" alt="" class="top-left-logo" />
<img src="{{ $.baseUrl }}/static/icons/Hipster_NavLogo.svg" alt="" class="top-left-logo" />
{{ end }}
</a>
<div class="controls">
Expand All @@ -70,26 +70,26 @@
<div class="h-controls">
<div class="h-control">
<span class="icon currency-icon"> {{ renderCurrencyLogo $.user_currency}}</span>
<form method="POST" class="controls-form" action="/setCurrency" id="currency_form" >
<form method="POST" class="controls-form" action="{{ $.baseUrl }}/setCurrency" id="currency_form" >
<select name="currency_code" onchange="document.getElementById('currency_form').submit();">
{{range $.currencies}}
<option value="{{.}}" {{if eq . $.user_currency}}selected="selected"{{end}}>{{.}}</option>
{{end}}
</select>
</form>
<img src="/static/icons/Hipster_DownArrow.svg" alt="" class="icon arrow" />
<img src="{{ $.baseUrl }}/static/icons/Hipster_DownArrow.svg" alt="" class="icon arrow" />
</div>
</div>
{{ end }}

{{ if $.assistant_enabled }}
<a href="/assistant" class="cart-link">
<img src="/static/icons/Hipster_WandIcon.svg" style="width: 22px; height: 22px;" alt="Assistant icon" class="logo" title="Assistant" />
<a href="{{ $.baseUrl }}/assistant" class="cart-link">
<img src="{{ $.baseUrl }}/static/icons/Hipster_WandIcon.svg" style="width: 22px; height: 22px;" alt="Assistant icon" class="logo" title="Assistant" />
</a>
{{ end }}

<a href="/cart" class="cart-link">
<img src="/static/icons/Hipster_CartIcon.svg" alt="Cart icon" class="logo" title="Cart" />
<a href="{{ $.baseUrl }}/cart" class="cart-link">
<img src="{{ $.baseUrl }}/static/icons/Hipster_CartIcon.svg" alt="Cart icon" class="logo" title="Cart" />
{{ if $.cart_size }}
<span class="cart-size-circle">{{$.cart_size}}</span>
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ <h3>Hot Products</h3>

{{ range $.products }}
<div class="col-md-4 hot-product-card">
<a href="/product/{{.Item.Id}}">
<img loading="lazy" src="{{.Item.Picture}}">
<a href="{{ $.baseUrl }}/product/{{.Item.Id}}">
<img loading="lazy" src="{{ $.baseUrl }}{{.Item.Picture}}">
<div class="hot-product-card-img-overlay"></div>
</a>
<div>
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/templates/order.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ <h3>
</div>
<div class="row">
<div class="col-12 text-center">
<a class="cymbal-button-primary" href="/" role="button">
<a class="cymbal-button-primary" href="{{ $.baseUrl }}/" role="button">
Continue Shopping
</a>
</div>
</div>
</section>

{{ if $.recommendations }}
{{ template "recommendations" $.recommendations }}
{{ template "recommendations" $ }}
{{ end }}

</main>
Expand Down
10 changes: 5 additions & 5 deletions src/frontend/templates/product.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="h-product container">
<div class="row">
<div class="col-md-6">
<img class="product-image" alt="" src="{{$.product.Item.Picture}}" />
<img class="product-image" alt="" src="{{ $.baseUrl }}{{$.product.Item.Picture}}" />
</div>
<div class="product-info col-md-5">
<div class="product-wrapper">
Expand All @@ -53,7 +53,7 @@ <h3>Packaging</h3>
</div>
{{ end }}

<form method="POST" action="/cart">
<form method="POST" action="{{ $.baseUrl }}/cart">
<input type="hidden" name="product_id" value="{{$.product.Item.Id}}" />
<div class="product-quantity-dropdown">
<select name="quantity" id="quantity">
Expand All @@ -64,7 +64,7 @@ <h3>Packaging</h3>
<option>5</option>
<option>10</option>
</select>
<img src="/static/icons/Hipster_DownArrow.svg" alt="">
<img src="{{ $.baseUrl }}/static/icons/Hipster_DownArrow.svg" alt="">
</div>
<button type="submit" class="cymbal-button-primary">Add To Cart</button>
</form>
Expand All @@ -74,11 +74,11 @@ <h3>Packaging</h3>
</div>
<div>
{{ if $.recommendations}}
{{ template "recommendations" $.recommendations }}
{{ template "recommendations" $ }}
{{ end }}
</div>
<div class="ad">
{{ with $.ad }}{{ template "text_ad" . }}{{ end }}
{{ if $.ad }}{{ template "text_ad" $ }}{{ end }}
</div>

</main>
Expand Down
Loading
Loading