-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrong.go
195 lines (157 loc) · 5.37 KB
/
strong.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/adiazny/strong/internal/pkg/auth"
"github.com/adiazny/strong/internal/pkg/gdrive"
"github.com/adiazny/strong/internal/pkg/store"
"github.com/adiazny/strong/internal/pkg/strava"
"github.com/adiazny/strong/internal/pkg/strong"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
const version = "1.1.0"
const (
defaultAPIPort = 5000
defaultPath = "./strong.csv"
defaultRedirectURL = "http://localhost:4001/v1/redirect"
gdriveTokenPath = "gdrive/storage.json"
stravaTokenPath = "strava/storage.json"
)
type config struct {
port int
path string
stravaClientID string
stravaClientSecret string
stravaRedirectURL string
gdriveClientID string
gdriveClientSecret string
gdriveRedirectURL string
}
type application struct {
config config
log *log.Logger
stravaAuthProvider *auth.Provider
gdriveAuthProvider *auth.Provider
}
func main() {
log := log.New(os.Stdout, "", log.Ldate|log.Ltime)
ctx := context.Background()
var cfg config
flag.IntVar(&cfg.port, "port", defaultAPIPort, "API server port")
flag.StringVar(&cfg.path, "path", defaultPath, "Path to strong file")
flag.StringVar(&cfg.stravaClientID, "strava-client", os.Getenv("STRAVA_CLIENT_ID"), "Strava API Client ID")
flag.StringVar(&cfg.stravaClientSecret, "strava-secret", os.Getenv("STRAVA_CLIENT_SECRET"), "Strava API Client Secret")
flag.StringVar(&cfg.stravaRedirectURL, "strava-redirect", defaultRedirectURL, "Strava Redirect URL")
flag.StringVar(&cfg.gdriveClientID, "gdrive-client", os.Getenv("GDRIVE_CLIENT_ID"), "Google Drive API Client ID")
flag.StringVar(&cfg.gdriveClientSecret, "gdrive-secret", os.Getenv("GDRIVE_CLIENT_SECRET"), "Google Drive API Client Secret")
flag.StringVar(&cfg.gdriveRedirectURL, "gdrive-redirect", defaultRedirectURL, "Google Drive Redirect URL")
flag.Parse()
//========================================================================
// Create files
gStore, err := store.NewFile(gdriveTokenPath)
if err != nil {
log.Printf("error creating google drive file store %v\n", err)
os.Exit(1)
}
stravaStore, err := store.NewFile(stravaTokenPath)
if err != nil {
log.Printf("error creating strava file store %v\n", err)
os.Exit(1)
}
//========================================================================
// Bootstrap OAuth Providers
gdriveAuthProvider, err := auth.NewProvider(auth.GDriveService, gdriveTokenPath, cfg.gdriveClientID, cfg.gdriveClientSecret, cfg.gdriveRedirectURL, gStore)
if err != nil {
log.Printf("error creating gdrive auth provider %v\n", err)
os.Exit(1)
}
stravaAuthProvider, err := auth.NewProvider(auth.StravaService, stravaTokenPath, cfg.stravaClientID, cfg.stravaClientSecret, cfg.stravaRedirectURL, stravaStore)
if err != nil {
log.Printf("error creating strava auth provider %v\n", err)
os.Exit(1)
}
//========================================================================
// API Server Flow
app := &application{
config: cfg,
log: log,
stravaAuthProvider: stravaAuthProvider,
gdriveAuthProvider: gdriveAuthProvider,
}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.port),
Handler: app.routes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
go func() {
log.Printf("starting api server on %s", srv.Addr)
err := srv.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
log.Printf("error encounted with http server %v\n", err)
}
log.Print("shutting down api server\n")
}()
//========================================================================
// Google Drive Flow
token, err := gdriveAuthProvider.Storage.GetToken()
if err != nil || !token.Valid() {
gdriveURL := gdriveAuthProvider.AuthCodeURL("gdrive-state")
log.Println(gdriveURL)
for gStore.FileNotPresent() {
time.Sleep(10 * time.Second)
}
}
gdriveHttpClient := gdriveAuthProvider.Client(ctx, token)
driveService, err := drive.NewService(ctx, option.WithHTTPClient(gdriveHttpClient))
if err != nil {
log.Fatalf("error to creating gdrive service: %v", err)
}
driveProvider := &gdrive.Provider{
DataPath: "strong.csv",
DriveService: driveService,
}
driveBytes, err := driveProvider.Import(ctx)
if err != nil {
log.Fatalf("error to importing gdrive file: %v", err)
}
var workouts []strong.Workout
if driveBytes != nil {
file := bytes.NewReader(driveBytes)
workouts, err = strong.Process(file)
if err != nil {
log.Printf("error processing file %v\n", err)
os.Exit(1)
}
} else {
log.Printf("empty drive file imported\n")
os.Exit(1)
}
//========================================================================
// Strava Flow
token, err = stravaAuthProvider.Storage.GetToken()
if err != nil || !token.Valid() {
stravaURL := stravaAuthProvider.AuthCodeURL("strava-state")
log.Println(stravaURL)
// This should be the filestorage method
for stravaStore.FileNotPresent() {
time.Sleep(10 * time.Second)
}
}
stravaClient := stravaAuthProvider.Client(ctx, token)
stravaProvider := strava.NewProvider(log, stravaClient)
err = stravaProvider.UploadNewWorkouts(context.Background(), workouts)
if err != nil {
log.Fatalf("error uploading strava activities %v", err)
}
log.Print("uploaded new workouts to strava")
}