Read runtime configuration from datastore.
authorFlorian Forster <ff@octo.it>
Mon, 29 Jan 2018 09:27:16 +0000 (10:27 +0100)
committerFlorian Forster <ff@octo.it>
Mon, 29 Jan 2018 09:27:16 +0000 (10:27 +0100)
app/config.go [new file with mode: 0644]
fitbit/fitbit.go
gfit/gfit.go
kraftakt.go

diff --git a/app/config.go b/app/config.go
new file mode 100644 (file)
index 0000000..f05a9e6
--- /dev/null
@@ -0,0 +1,28 @@
+package app
+
+import (
+       "context"
+
+       "google.golang.org/appengine/datastore"
+       "google.golang.org/appengine/log"
+)
+
+var Config struct {
+       ProjectNumber string
+
+       FitbitClientID       string
+       FitbitClientSecret   string
+       FitbitSubscriberCode string
+
+       GoogleClientID     string
+       GoogleClientSecret string
+}
+
+func LoadConfig(ctx context.Context) error {
+       key := datastore.NewKey(ctx, "Config", "Production", 0, nil)
+       if err := datastore.Get(ctx, key, &Config); err != nil {
+               log.Errorf(ctx, `datastore.Get("Config", "Production") = %v`, err)
+               return err
+       }
+       return nil
+}
index 90457d2..5c967b1 100644 (file)
@@ -18,8 +18,8 @@ import (
 )
 
 var oauth2Config = &oauth2.Config{
 )
 
 var oauth2Config = &oauth2.Config{
-       ClientID:     "@FITBIT_CLIENT_ID@",
-       ClientSecret: "@FITBIT_CLIENT_SECRET@",
+       ClientID:     app.Config.FitbitClientID,
+       ClientSecret: app.Config.FitbitClientSecret,
        Endpoint:     oauth2fitbit.Endpoint,
        RedirectURL:  "https://kraftakt.octo.it/fitbit/grant",
        Scopes: []string{
        Endpoint:     oauth2fitbit.Endpoint,
        RedirectURL:  "https://kraftakt.octo.it/fitbit/grant",
        Scopes: []string{
index 69308a7..1599799 100644 (file)
@@ -29,8 +29,8 @@ const (
 )
 
 var oauthConfig = &oauth2.Config{
 )
 
 var oauthConfig = &oauth2.Config{
-       ClientID:     "@GOOGLE_CLIENT_ID@",
-       ClientSecret: "@GOOGLE_CLIENT_SECRET@",
+       ClientID:     app.Config.GoogleClientID,
+       ClientSecret: app.Config.GoogleClientSecret,
        Endpoint:     oauth2google.Endpoint,
        RedirectURL:  "https://kraftakt.octo.it/google/grant",
        Scopes: []string{
        Endpoint:     oauth2google.Endpoint,
        RedirectURL:  "https://kraftakt.octo.it/google/grant",
        Scopes: []string{
@@ -42,7 +42,7 @@ var oauthConfig = &oauth2.Config{
 
 func Application(ctx context.Context) *fitness.Application {
        return &fitness.Application{
 
 func Application(ctx context.Context) *fitness.Application {
        return &fitness.Application{
-               Name:       "Fitbit to Google Fit sync",
+               Name:       "Kraftakt",
                Version:    appengine.VersionID(ctx),
                DetailsUrl: "", // optional
        }
                Version:    appengine.VersionID(ctx),
                DetailsUrl: "", // optional
        }
@@ -89,7 +89,7 @@ func DataStreamID(dataSource *fitness.DataSource) string {
        fields := []string{
                dataSource.Type,
                dataSource.DataType.Name,
        fields := []string{
                dataSource.Type,
                dataSource.DataType.Name,
-               "@PROJECT_NUMBER@", // FIXME
+               app.Config.ProjectNumber,
        }
 
        if dev := dataSource.Device; dev != nil {
        }
 
        if dev := dataSource.Device; dev != nil {
index e0ad321..69815c2 100644 (file)
@@ -28,6 +28,10 @@ func init() {
        http.HandleFunc("/google/setup", googleSetupHandler)
        http.Handle("/google/grant", AuthenticatedHandler(googleGrantHandler))
        http.Handle("/", AuthenticatedHandler(indexHandler))
        http.HandleFunc("/google/setup", googleSetupHandler)
        http.Handle("/google/grant", AuthenticatedHandler(googleGrantHandler))
        http.Handle("/", AuthenticatedHandler(indexHandler))
+
+       if err := app.LoadConfig(context.Background()); err != nil {
+               panic(err)
+       }
 }
 
 // ContextHandler implements http.Handler
 }
 
 // ContextHandler implements http.Handler
@@ -171,7 +175,7 @@ func fitbitNotifyHandler(ctx context.Context, w http.ResponseWriter, r *http.Req
        // this is used when setting up a new subscriber in the UI. Once set
        // up, this code path should not be triggered.
        if verify := r.FormValue("verify"); verify != "" {
        // this is used when setting up a new subscriber in the UI. Once set
        // up, this code path should not be triggered.
        if verify := r.FormValue("verify"); verify != "" {
-               if verify == "@FITBIT_SUBSCRIBER_CODE@" {
+               if verify == app.Config.FitbitSubscriberCode {
                        w.WriteHeader(http.StatusNoContent)
                } else {
                        w.WriteHeader(http.StatusNotFound)
                        w.WriteHeader(http.StatusNoContent)
                } else {
                        w.WriteHeader(http.StatusNotFound)