X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=kraftakt.go;h=b49f787fd2797f697d95aea30fb5d9d674222bf6;hb=e45460a9cf2134526a755cfa0c8124282f8ccf92;hp=e0ad321e325d18b34d453c31893f74c3b8009b63;hpb=304308a757d192b9b80455017bda1cb82889d346;p=kraftakt.git diff --git a/kraftakt.go b/kraftakt.go index e0ad321..b49f787 100644 --- a/kraftakt.go +++ b/kraftakt.go @@ -36,6 +36,11 @@ type ContextHandler func(context.Context, http.ResponseWriter, *http.Request) er func (hndl ContextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) + if err := app.LoadConfig(ctx); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if err := hndl(ctx, w, r); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -47,6 +52,11 @@ type AuthenticatedHandler func(context.Context, http.ResponseWriter, *http.Reque func (hndl AuthenticatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) + if err := app.LoadConfig(ctx); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + gaeUser := user.Current(ctx) if gaeUser == nil { url, err := user.LoginURL(ctx, r.URL.String()) @@ -171,7 +181,7 @@ func fitbitNotifyHandler(ctx context.Context, w http.ResponseWriter, r *http.Req // this is used when setting up a new subscriber in the UI. Once set // up, this code path should not be triggered. if verify := r.FormValue("verify"); verify != "" { - if verify == "@FITBIT_SUBSCRIBER_CODE@" { + if verify == app.Config.FitbitSubscriberCode { w.WriteHeader(http.StatusNoContent) } else { w.WriteHeader(http.StatusNotFound) @@ -187,6 +197,7 @@ func fitbitNotifyHandler(ctx context.Context, w http.ResponseWriter, r *http.Req // Fitbit recommendation: "If signature verification fails, you should // respond with a 404" if !fitbit.CheckSignature(ctx, data, r.Header.Get("X-Fitbit-Signature")) { + log.Warningf(ctx, "signature mismatch") w.WriteHeader(http.StatusNotFound) return nil } @@ -202,6 +213,10 @@ func fitbitNotifyHandler(ctx context.Context, w http.ResponseWriter, r *http.Req // handleNotifications parses fitbit notifications and requests the individual // activities from Fitbit. It is executed asynchronously via the delay package. func handleNotifications(ctx context.Context, payload []byte) error { + if err := app.LoadConfig(ctx); err != nil { + return err + } + var subscriptions []fitbit.Subscription if err := json.Unmarshal(payload, &subscriptions); err != nil { return err @@ -307,7 +322,7 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error { break } if err := gfitClient.SetDistance(ctx, distanceMeters, tm); err != nil { - errs = append(errs, fmt.Errorf("gfitClient.SetDistance(%d) = %v", distanceMeters, err)) + errs = append(errs, fmt.Errorf("gfitClient.SetDistance(%g) = %v", distanceMeters, err)) return } }()