frontend: more organization
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
ef92ddc351
commit
7063092393
4 changed files with 41 additions and 8 deletions
|
@ -2,11 +2,14 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
|
pb "frontend/genproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -36,8 +39,7 @@ func ensureSessionID(next http.HandlerFunc) http.HandlerFunc {
|
||||||
MaxAge: cookieMaxAge,
|
MaxAge: cookieMaxAge,
|
||||||
})
|
})
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Printf("unrecognized cookie error: %+v", err)
|
http.Error(w, fmt.Sprintf("unrecognized cookie error: %+v", err), http.StatusInternalServerError)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
sessionID = c.Value
|
sessionID = c.Value
|
||||||
|
@ -59,16 +61,32 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("currencies: %+v", currencies)
|
log.Printf("currencies: %+v", currencies)
|
||||||
products, err := fe.getProducts(r.Context())
|
products, err := fe.getProducts(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("# products: %d", len(products))
|
log.Printf("# products: %d", len(products))
|
||||||
|
|
||||||
|
type productView struct {
|
||||||
|
Item *pb.Product
|
||||||
|
Price *pb.Money
|
||||||
|
}
|
||||||
|
ps := make([]productView, len(products))
|
||||||
|
for i, p := range products {
|
||||||
|
price, err := fe.convertCurrency(r.Context(), &pb.Money{
|
||||||
|
Amount: p.PriceUsd,
|
||||||
|
CurrencyCode: defaultCurrency,
|
||||||
|
}, currentCurrency(r))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ps[i] = productView{p, price}
|
||||||
|
}
|
||||||
|
|
||||||
if err := homeTemplate.Execute(w, map[string]interface{}{
|
if err := homeTemplate.Execute(w, map[string]interface{}{
|
||||||
"user_currency": currentCurrency(r),
|
"user_currency": currentCurrency(r),
|
||||||
"currencies": currencies,
|
"currencies": currencies,
|
||||||
"products": products,
|
"products": ps,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
@ -76,8 +94,6 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func (fe *frontendServer) logoutHandler(w http.ResponseWriter, r *http.Request) {
|
func (fe *frontendServer) logoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{}))
|
log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{}))
|
||||||
|
|
||||||
// clear all cookies
|
|
||||||
for _, c := range r.Cookies() {
|
for _, c := range r.Cookies() {
|
||||||
c.MaxAge = -1
|
c.MaxAge = -1
|
||||||
http.SetCookie(w, c)
|
http.SetCookie(w, c)
|
||||||
|
|
|
@ -40,6 +40,7 @@ type frontendServer struct {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
log.SetFlags(log.Lshortfile | log.Ltime)
|
||||||
|
|
||||||
srvPort := port
|
srvPort := port
|
||||||
if os.Getenv("PORT") != "" {
|
if os.Getenv("PORT") != "" {
|
||||||
|
|
|
@ -6,6 +6,10 @@ import (
|
||||||
pb "frontend/genproto"
|
pb "frontend/genproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
avoidNoopCurrencyConversionRPC = false
|
||||||
|
)
|
||||||
|
|
||||||
func (fe *frontendServer) getCurrencies(ctx context.Context) ([]string, error) {
|
func (fe *frontendServer) getCurrencies(ctx context.Context) ([]string, error) {
|
||||||
currs, err := pb.NewCurrencyServiceClient(fe.currencySvcConn).
|
currs, err := pb.NewCurrencyServiceClient(fe.currencySvcConn).
|
||||||
GetSupportedCurrencies(ctx, &pb.Empty{})
|
GetSupportedCurrencies(ctx, &pb.Empty{})
|
||||||
|
@ -26,3 +30,13 @@ func (fe *frontendServer) getProducts(ctx context.Context) ([]*pb.Product, error
|
||||||
ListProducts(ctx, &pb.Empty{})
|
ListProducts(ctx, &pb.Empty{})
|
||||||
return resp.GetProducts(), err
|
return resp.GetProducts(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fe *frontendServer) convertCurrency(ctx context.Context, money *pb.Money, currency string) (*pb.Money, error) {
|
||||||
|
if avoidNoopCurrencyConversionRPC && money.GetCurrencyCode() == currency {
|
||||||
|
return money, nil
|
||||||
|
}
|
||||||
|
return pb.NewCurrencyServiceClient(fe.currencySvcConn).
|
||||||
|
Convert(ctx, &pb.CurrencyConversionRequest{
|
||||||
|
From: money,
|
||||||
|
ToCode: currency})
|
||||||
|
}
|
||||||
|
|
|
@ -17,7 +17,9 @@
|
||||||
</select>
|
</select>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
||||||
<p>{{ len $.products }} products</p>
|
<p>{{ len $.products }} products</p>
|
||||||
|
{{range $.products }}
|
||||||
|
<p>{{.Item.Name}} -- <{{.Price}}></p>
|
||||||
|
{{end}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue