Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
sgarcez committed Feb 5, 2020
1 parent a50c6c2 commit 893d3be
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
24 changes: 9 additions & 15 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@ import (
"fmt"
"net/http"

"github.com/strava/go.strava"
strava "github.com/strava/go.strava"
)

// authHandler provides an auth url and HandlerFunc to handle its redirect
func authHandler(port string) (string, string, http.HandlerFunc, error) {

// Application id and secret can be found at https://www.strava.com/settings/api
// define a strava.OAuthAuthenticator to hold state.
// The callback url is used to generate an AuthorizationURL.
// AuthHandler provides an url to direct the user to as well as
// an http.HandlerFunc to handle the redirect from the remote host.
func AuthHandler(port string) (authURL string, localPath string, handler http.HandlerFunc) {
authenticator := &strava.OAuthAuthenticator{
CallbackURL: fmt.Sprintf("http://localhost:%s/exchange_token", port),
}

callbackPath, err := authenticator.CallbackPath()
if err != nil {
return "", callbackPath, nil, err
}
// the path that our server should listen on
localPath = "/exchange_token"

authURL := authenticator.AuthorizationURL(
"state1", strava.Permissions.WriteViewPrivate, true)
authURL = authenticator.AuthorizationURL("state1", "activity:write", true)

handler := authenticator.HandlerFunc(success, failure)
handler = authenticator.HandlerFunc(success, failure)

return authURL, callbackPath, handler, nil
return
}

func success(auth *strava.AuthorizationResponse, w http.ResponseWriter, r *http.Request) {
Expand Down
45 changes: 26 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"strings"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -45,19 +46,25 @@ func new() *cobra.Command {
}

var wg sync.WaitGroup
wg.Add(len(files))
log.Printf("Processing %d files\n", len(files))

for _, f := range files {
if f.IsDir() || strings.HasPrefix(f.Name(), ".") || !strings.HasSuffix(f.Name(), ".fit") {
log.Printf("Ignoring %s\n", f.Name())
continue
}

wg.Add(1)
go func(fname string) {
defer wg.Done()
f, err := os.Open(path.Join(inputDir, fname))
if err != nil {
log.Print(err)
log.Printf("%s - open: %s", fname, err)
return
}
aid, err := u.Upload(fname, f)
if err != nil {
log.Print(err)
log.Printf("%s - upload: %s", fname, err)
return
}
log.Printf(
Expand All @@ -82,22 +89,10 @@ func new() *cobra.Command {

m := http.NewServeMux()
s := &http.Server{Addr: fmt.Sprintf(":%s", port), Handler: m}
authURL, callbackPath, callbackHandler, err := authHandler(port)
if err != nil {
log.Fatal(err)
}
handleAndKill := func(in http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
in(w, r)
fmt.Fprintf(w, "\nYou can close this window")
go func() {
if err := s.Shutdown(context.Background()); err != nil {
log.Fatal(err)
}
}()
}
}
m.HandleFunc(callbackPath, handleAndKill(callbackHandler))

authURL, path, handler := AuthHandler(port)

m.HandleFunc(path, handleAndKill(s, handler))

fmt.Printf("-------------------------------\n")
fmt.Printf("Use this URL to authorise your application:\n\n%s\n", authURL)
Expand All @@ -124,3 +119,15 @@ func main() {
os.Exit(1)
}
}

func handleAndKill(s *http.Server, in http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
in(w, r)
fmt.Fprintf(w, "\nYou can close this window")
go func() {
if err := s.Shutdown(context.Background()); err != nil {
log.Fatal(err)
}
}()
}
}
28 changes: 21 additions & 7 deletions uploader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"time"
"io"
"log"

Expand Down Expand Up @@ -34,12 +36,24 @@ func (u *uploader) Upload(fname string, f io.Reader) (*int64, error) {
return nil, err
}

uploadSummary, err := u.service.Get(resp.Id).Do()
if err != nil {
// TODO: parse error and sleep if activity isn't ready yet
log.Printf("%s - %s", fname, err)
return nil, err
}
tries := 0
for {

sum, err := u.service.Get(resp.Id).Do()
if err != nil {
return nil, fmt.Errorf("get uploaded activity summary: %w", err)
}

return &uploadSummary.ActivityId, nil
if sum.Error != "" {
return nil, fmt.Errorf("%s", sum.Error)
}

if sum.Status == "Your activity is still being processed." && tries < 10 {
log.Printf("%s - waiting for activity to be processed", fname)
tries++
time.Sleep(5* time.Second)
} else {
return &sum.ActivityId, nil
}
}
}

0 comments on commit 893d3be

Please sign in to comment.