X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=kraftakt.go;h=4700104d42b8d0216fb9f202e633a92b5e5a04fb;hb=49ab9e8d222250c29a7167a0ce7b60e4c8d4ee12;hp=87b8bf2d3be57813284ca965ccda56713ebe72d9;hpb=52cd11fdc84ec2299ba13de1dfbf47b956cd12bf;p=kraftakt.git diff --git a/kraftakt.go b/kraftakt.go index 87b8bf2..4700104 100644 --- a/kraftakt.go +++ b/kraftakt.go @@ -43,6 +43,12 @@ func init() { 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 @@ -50,12 +56,12 @@ 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) + 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 } } @@ -66,7 +72,7 @@ func (hndl AuthenticatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques 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 } @@ -74,7 +80,7 @@ func (hndl AuthenticatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques 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) @@ -83,12 +89,12 @@ func (hndl AuthenticatedHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques 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 } } @@ -136,8 +142,8 @@ func loginHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ * 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 } @@ -171,20 +177,12 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http 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 @@ -195,8 +193,8 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http 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 } @@ -276,6 +274,8 @@ 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 { + log.Debugf(ctx, "NOTIFY -> %s", payload) + if err := app.LoadConfig(ctx); err != nil { return err } @@ -301,7 +301,7 @@ func handleNotifications(ctx context.Context, payload []byte) error { } 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 }