From efc1f271711fbbbf06543c1d4883f377019f8c84 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 29 Jan 2018 10:27:16 +0100 Subject: [PATCH] Read runtime configuration from datastore. --- app/config.go | 28 ++++++++++++++++++++++++++++ fitbit/fitbit.go | 4 ++-- gfit/gfit.go | 8 ++++---- kraftakt.go | 6 +++++- 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 app/config.go diff --git a/app/config.go b/app/config.go new file mode 100644 index 0000000..f05a9e6 --- /dev/null +++ b/app/config.go @@ -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 +} diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index 90457d2..5c967b1 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -18,8 +18,8 @@ import ( ) 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{ diff --git a/gfit/gfit.go b/gfit/gfit.go index 69308a7..1599799 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -29,8 +29,8 @@ const ( ) 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{ @@ -42,7 +42,7 @@ var oauthConfig = &oauth2.Config{ func Application(ctx context.Context) *fitness.Application { return &fitness.Application{ - Name: "Fitbit to Google Fit sync", + Name: "Kraftakt", Version: appengine.VersionID(ctx), DetailsUrl: "", // optional } @@ -89,7 +89,7 @@ func DataStreamID(dataSource *fitness.DataSource) string { fields := []string{ dataSource.Type, dataSource.DataType.Name, - "@PROJECT_NUMBER@", // FIXME + app.Config.ProjectNumber, } if dev := dataSource.Device; dev != nil { diff --git a/kraftakt.go b/kraftakt.go index e0ad321..69815c2 100644 --- a/kraftakt.go +++ b/kraftakt.go @@ -28,6 +28,10 @@ func init() { 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 @@ -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 != "" { - if verify == "@FITBIT_SUBSCRIBER_CODE@" { + if verify == app.Config.FitbitSubscriberCode { w.WriteHeader(http.StatusNoContent) } else { w.WriteHeader(http.StatusNotFound) -- 2.11.0