X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=kraftakt.go;h=4700104d42b8d0216fb9f202e633a92b5e5a04fb;hb=49ab9e8d222250c29a7167a0ce7b60e4c8d4ee12;hp=1f3ac98ed07e4b9b5b2614cbb4a24ee6cb2b114a;hpb=85a81c5e601b14166dfa0bd9633ba609b1b3ac3d;p=kraftakt.git
diff --git a/kraftakt.go b/kraftakt.go
index 1f3ac98..4700104 100644
--- a/kraftakt.go
+++ b/kraftakt.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "html/template"
"io/ioutil"
"net/http"
"sync"
@@ -21,15 +22,31 @@ import (
var delayedHandleNotifications = delay.Func("handleNotifications", handleNotifications)
+var templates *template.Template
+
func init() {
+ http.Handle("/login", AuthenticatedHandler(loginHandler))
http.Handle("/fitbit/connect", AuthenticatedHandler(fitbitConnectHandler))
http.Handle("/fitbit/grant", AuthenticatedHandler(fitbitGrantHandler))
- http.Handle("/fitbit/notify", ContextHandler(fitbitNotifyHandler))
http.Handle("/fitbit/disconnect", AuthenticatedHandler(fitbitDisconnectHandler))
http.Handle("/google/connect", AuthenticatedHandler(googleConnectHandler))
http.Handle("/google/grant", AuthenticatedHandler(googleGrantHandler))
http.Handle("/google/disconnect", AuthenticatedHandler(googleDisconnectHandler))
- http.Handle("/", AuthenticatedHandler(indexHandler))
+ // unauthenticated
+ http.Handle("/fitbit/notify", ContextHandler(fitbitNotifyHandler))
+ http.Handle("/", ContextHandler(indexHandler))
+
+ t, err := template.ParseGlob("templates/*.html")
+ if err != nil {
+ panic(err)
+ }
+ 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
@@ -39,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
}
}
@@ -55,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
}
@@ -63,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)
@@ -72,61 +89,61 @@ 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
}
}
-func indexHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
- _, err := u.Token(ctx, "Fitbit")
- if err != nil && err != datastore.ErrNoSuchEntity {
- return err
+func indexHandler(ctx context.Context, w http.ResponseWriter, _ *http.Request) error {
+ var templateData struct {
+ HaveFitbit bool
+ HaveGoogleFit bool
+ *app.User
}
- haveFitbitToken := err == nil
+ templateName := "main.html"
- _, err = u.Token(ctx, "Google")
- if err != nil && err != datastore.ErrNoSuchEntity {
- return err
- }
- haveGoogleToken := err == nil
+ if gaeUser := user.Current(ctx); gaeUser != nil {
+ templateName = "loggedin.html"
- fmt.Fprintln(w, "
Kraftakt")
- fmt.Fprintln(w, "Kraftakt
")
-
- fmt.Fprintln(w, "Kraftakt copies your Fitbit data to Google Fit, seconds after you sync.
")
-
- fmt.Fprintf(w, "Hello %s
\n", user.Current(ctx).Email)
- fmt.Fprintln(w, "")
+ u, err := app.NewUser(ctx, gaeUser.Email)
+ if err != nil {
+ return err
+ }
+ templateData.User = u
- fmt.Fprint(w, "- Fitbit: ")
- if haveFitbitToken {
- fmt.Fprint(w, `Authorized`)
- } else {
- fmt.Fprint(w, `Not authorized (Authorize)`)
- }
- fmt.Fprintln(w, "
")
+ _, err = u.Token(ctx, "Fitbit")
+ if err != nil && err != datastore.ErrNoSuchEntity {
+ return err
+ }
+ templateData.HaveFitbit = (err == nil)
- fmt.Fprint(w, "- Google Fit: ")
- if haveGoogleToken {
- fmt.Fprint(w, `Authorized`)
- } else {
- fmt.Fprint(w, `Not authorized (Authorize)`)
+ _, err = u.Token(ctx, "Google")
+ if err != nil && err != datastore.ErrNoSuchEntity {
+ return err
+ }
+ templateData.HaveGoogleFit = (err == nil)
}
- fmt.Fprintln(w, "
")
- fmt.Fprintln(w, "
")
- fmt.Fprintln(w, "")
+ return templates.ExecuteTemplate(w, templateName, &templateData)
+}
+func loginHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ *app.User) error {
+ // essentially a nop; all the heavy lifting (i.e. logging in) has been done by the AuthenticatedHandler wrapper.
+ redirectURL := r.URL
+ redirectURL.Path = "/"
+ redirectURL.RawQuery = ""
+ redirectURL.Fragment = ""
+ http.Redirect(w, r, redirectURL.String(), http.StatusTemporaryRedirect)
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
}
@@ -134,7 +151,7 @@ func fitbitGrantHandler(ctx context.Context, w http.ResponseWriter, r *http.Requ
if err := fitbit.ParseToken(ctx, r, u); err != nil {
return err
}
- c, err := fitbit.NewClient(ctx, "-", u)
+ c, err := fitbit.NewClient(ctx, "", u)
if err != nil {
return err
}
@@ -154,8 +171,31 @@ func fitbitGrantHandler(ctx context.Context, w http.ResponseWriter, r *http.Requ
return nil
}
-func googleSetupHandler(w http.ResponseWriter, r *http.Request) {
- http.Redirect(w, r, gfit.AuthURL(), http.StatusTemporaryRedirect)
+func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
+ c, err := fitbit.NewClient(ctx, "", u)
+ if err != nil {
+ return err
+ }
+
+ if err := c.UnsubscribeAll(ctx); err != nil {
+ return fmt.Errorf("UnsubscribeAll() = %v", err)
+ }
+
+ if err := c.DeleteToken(ctx); err != nil {
+ return err
+ }
+
+ redirectURL := r.URL
+ redirectURL.Path = "/"
+ redirectURL.RawQuery = ""
+ redirectURL.Fragment = ""
+ http.Redirect(w, r, redirectURL.String(), http.StatusTemporaryRedirect)
+ return nil
+}
+
+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
}
func googleGrantHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
@@ -171,6 +211,24 @@ func googleGrantHandler(ctx context.Context, w http.ResponseWriter, r *http.Requ
return nil
}
+func googleDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error {
+ c, err := gfit.NewClient(ctx, u)
+ if err != nil {
+ return err
+ }
+
+ if err := c.DeleteToken(ctx); err != nil {
+ return err
+ }
+
+ redirectURL := r.URL
+ redirectURL.Path = "/"
+ redirectURL.RawQuery = ""
+ redirectURL.Fragment = ""
+ http.Redirect(w, r, redirectURL.String(), http.StatusTemporaryRedirect)
+ return nil
+}
+
// fitbitNotifyHandler is called by Fitbit whenever there are updates to a
// subscription. It verifies the payload, splits it into individual
// notifications and adds it to the taskqueue service.
@@ -216,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
}
@@ -241,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
}