templates = t
}
+func internalServerError(ctx context.Context, w http.ResponseWriter, err error) {
+ log.Errorf(ctx, "%v", err)
+
+ http.Error(w, "Internal Server Error\n\nReference: "+appengine.RequestID(ctx), http.StatusInternalServerError)
+}
+
// ContextHandler implements http.Handler
type ContextHandler func(context.Context, http.ResponseWriter, *http.Request) error
ctx := appengine.NewContext(r)
if err := app.LoadConfig(ctx); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ internalServerError(ctx, w, fmt.Errorf("LoadConfig() = %v", err))
return
}
if err := hndl(ctx, w, r); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ internalServerError(ctx, w, err)
return
}
}
ctx := appengine.NewContext(r)
if err := app.LoadConfig(ctx); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ internalServerError(ctx, w, fmt.Errorf("LoadConfig() = %v", err))
return
}
if gaeUser == nil {
url, err := user.LoginURL(ctx, r.URL.String())
if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ internalServerError(ctx, w, fmt.Errorf("LoginURL() = %v", err))
return
}
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
u, err := app.NewUser(ctx, gaeUser.Email)
if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ internalServerError(ctx, w, fmt.Errorf("NewUser(%q) = %v", gaeUser.Email, err))
return
}
if err := hndl(ctx, w, r, u); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ internalServerError(ctx, w, err)
return
}
}
return nil
}
-func fitbitConnectHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ *app.User) error {
- http.Redirect(w, r, fitbit.AuthURL(), http.StatusTemporaryRedirect)
+func fitbitConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
+ http.Redirect(w, r, fitbit.AuthURL(ctx, u), http.StatusTemporaryRedirect)
return nil
}
return err
}
- var errs appengine.MultiError
-
- for _, collection := range []string{"activities", "sleep"} {
- if err := c.Unsubscribe(ctx, collection); err != nil {
- errs = append(errs, fmt.Errorf("Unsubscribe(%q) = %v", collection, err))
- }
- log.Infof(ctx, "Successfully unsubscribed from %q", collection)
+ if err := c.UnsubscribeAll(ctx); err != nil {
+ return fmt.Errorf("UnsubscribeAll() = %v", err)
}
if err := c.DeleteToken(ctx); err != nil {
- errs = append(errs, fmt.Errorf("DeleteToken() = %v", err))
- }
- if len(errs) != 0 {
- return errs
+ return err
}
redirectURL := r.URL
return nil
}
-func googleConnectHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ *app.User) error {
- http.Redirect(w, r, gfit.AuthURL(), http.StatusTemporaryRedirect)
+func googleConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
+ http.Redirect(w, r, gfit.AuthURL(ctx, u), http.StatusTemporaryRedirect)
return nil
}
// 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 {
+ log.Debugf(ctx, "NOTIFY -> %s", payload)
+
if err := app.LoadConfig(ctx); err != nil {
return err
}
}
func handleNotification(ctx context.Context, s *fitbit.Subscription) error {
- u, err := app.UserByID(ctx, s.SubscriptionID)
+ u, err := fitbit.UserFromSubscriberID(ctx, s.SubscriptionID)
if err != nil {
return err
}