adding apigee clientID support

This commit is contained in:
phriscage 2018-12-02 16:04:02 -05:00
parent d966bc7c5d
commit 03de6a9b1c
3 changed files with 108 additions and 6 deletions

View file

@ -344,6 +344,60 @@ func (fe *frontendServer) setCurrencyHandler(w http.ResponseWriter, r *http.Requ
w.WriteHeader(http.StatusFound)
}
// viewConfigHandler adds an endpoint for administrators to add custom configuration items.
// I.E. apigeeClientID
// TODO(phriscage) update for multiple config values
func (fe *frontendServer) viewConfigHandler(w http.ResponseWriter, r *http.Request) {
log := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)
currencies, err := fe.getCurrencies(r.Context())
if err != nil {
renderHTTPError(log, r, w, errors.Wrap(err, "could not retrieve currencies"), http.StatusInternalServerError)
return
}
cart, err := fe.getCart(r.Context(), sessionID(r))
if err != nil {
renderHTTPError(log, r, w, errors.Wrap(err, "could not retrieve cart"), http.StatusInternalServerError)
return
}
apigeeClientID := currentApigeeClientID(r)
if err := templates.ExecuteTemplate(w, "config", map[string]interface{}{
"session_id": sessionID(r),
"request_id": r.Context().Value(ctxKeyRequestID{}),
"user_currency": currentCurrency(r),
"currencies": currencies,
"apigee_client_id": apigeeClientID,
"cart_size": len(cart),
}); err != nil {
log.Println(err)
}
}
// setConfigHandler we set the config variables from the FORM POST.
// Currently utilizing Cookies until appropriate storage library is determined
func (fe *frontendServer) setConfigHandler(w http.ResponseWriter, r *http.Request) {
log := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)
apigeeClientID := r.FormValue("apigee_client_id")
log.WithField("apigee_client_id.new", apigeeClientID).WithField("apigeeClientID.old", currentApigeeClientID(r)).
Debug("setting apigee_client_id")
if apigeeClientID != "" {
http.SetCookie(w, &http.Cookie{
Name: apigeeClientIDHeaderName,
Value: apigeeClientID,
MaxAge: cookieMaxAge,
})
}
referer := r.Header.Get("referer")
if referer == "" {
referer = "/"
}
w.Header().Set("Location", referer)
w.WriteHeader(http.StatusFound)
}
// chooseAd queries for advertisements available and randomly chooses one, if
// available. It ignores the error retrieving the ad since it is not critical.
func (fe *frontendServer) chooseAd(ctx context.Context, ctxKeys []string, log logrus.FieldLogger) *pb.Ad {
@ -376,6 +430,14 @@ func currentCurrency(r *http.Request) string {
return defaultCurrency
}
func currentApigeeClientID(r *http.Request) string {
c, _ := r.Cookie(apigeeClientIDHeaderName)
if c != nil {
return c.Value
}
return defaultApigeeClientID
}
func sessionID(r *http.Request) string {
v := r.Context().Value(ctxKeySessionID{})
if v != nil {

View file

@ -35,13 +35,15 @@ import (
)
const (
port = "8080"
defaultCurrency = "USD"
cookieMaxAge = 60 * 60 * 48
port = "8080"
defaultCurrency = "USD"
cookieMaxAge = 60 * 60 * 48
defaultApigeeClientID = "Add your Apigee Application Client ID here"
cookiePrefix = "shop_"
cookieSessionID = cookiePrefix + "session-id"
cookieCurrency = cookiePrefix + "currency"
cookiePrefix = "shop_"
cookieSessionID = cookiePrefix + "session-id"
cookieCurrency = cookiePrefix + "currency"
apigeeClientIDHeaderName = "x-api-key"
)
var (
@ -124,6 +126,8 @@ func main() {
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("/config", svc.viewConfigHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/config", svc.setConfigHandler).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)

View file

@ -0,0 +1,36 @@
{{ define "config" }}
{{ template "header" . }}
<main role="main">
<div class="py-5">
<div class="container bg-light py-3 px-lg-5 py-lg-5">
<div class="row py-3 my-2">
<div class="col-12 col-lg-8 offset-lg-2">
<h2>Hipster App Configuration</h2>
<hr/>
<form action="/config" method="POST">
<div class="form-row">
<div class="col-lg-3 mb-3 text-center">Apigee Client ID:</div>
<div class="col-lg-8 mb-3">
<input type="text" class="form-control" id="apigee_client_id" name="apigee_client_id" value="{{ .apigee_client_id }}" required>
</div>
</div>
<hr/>
<div class="form-row">
<div class="col">
</div>
<div class="col-lg-5 mb-3">
<button class="btn btn-primary btn-block" type="submit">Save</button>
</div>
<div class="col">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</main>
{{ template "footer" . }}
{{ end }}