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 (
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
pb "frontend/genproto"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -36,8 +39,7 @@ func ensureSessionID(next http.HandlerFunc) http.HandlerFunc {
|
|||
MaxAge: cookieMaxAge,
|
||||
})
|
||||
} else if err != nil {
|
||||
log.Printf("unrecognized cookie error: %+v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, fmt.Sprintf("unrecognized cookie error: %+v", err), http.StatusInternalServerError)
|
||||
return
|
||||
} else {
|
||||
sessionID = c.Value
|
||||
|
@ -59,16 +61,32 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {
|
|||
log.Printf("currencies: %+v", currencies)
|
||||
products, err := fe.getProducts(r.Context())
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
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{}{
|
||||
"user_currency": currentCurrency(r),
|
||||
"currencies": currencies,
|
||||
"products": products,
|
||||
"products": ps,
|
||||
}); err != nil {
|
||||
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) {
|
||||
log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{}))
|
||||
|
||||
// clear all cookies
|
||||
for _, c := range r.Cookies() {
|
||||
c.MaxAge = -1
|
||||
http.SetCookie(w, c)
|
||||
|
|
|
@ -40,6 +40,7 @@ type frontendServer struct {
|
|||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
log.SetFlags(log.Lshortfile | log.Ltime)
|
||||
|
||||
srvPort := port
|
||||
if os.Getenv("PORT") != "" {
|
||||
|
|
|
@ -6,6 +6,10 @@ import (
|
|||
pb "frontend/genproto"
|
||||
)
|
||||
|
||||
const (
|
||||
avoidNoopCurrencyConversionRPC = false
|
||||
)
|
||||
|
||||
func (fe *frontendServer) getCurrencies(ctx context.Context) ([]string, error) {
|
||||
currs, err := pb.NewCurrencyServiceClient(fe.currencySvcConn).
|
||||
GetSupportedCurrencies(ctx, &pb.Empty{})
|
||||
|
@ -26,3 +30,13 @@ func (fe *frontendServer) getProducts(ctx context.Context) ([]*pb.Product, error
|
|||
ListProducts(ctx, &pb.Empty{})
|
||||
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>
|
||||
</form>
|
||||
|
||||
|
||||
<p>{{ len $.products }} products</p>
|
||||
{{range $.products }}
|
||||
<p>{{.Item.Name}} -- <{{.Price}}></p>
|
||||
{{end}}
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue