From 4a33a7d11ce92204b8a628485f960e71301c8a94 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Thu, 9 May 2019 10:16:13 -0700 Subject: [PATCH] Rework configuration management --- cmd/api/main.go | 48 +++++++++++++++------ go.mod | 11 +---- go.sum | 22 +--------- pkg/config/config.go | 72 -------------------------------- pkg/config/config_test.go | 83 ------------------------------------- pkg/handler/handler.go | 37 ++++++++++------- pkg/handler/handler_test.go | 11 ++--- 7 files changed, 65 insertions(+), 219 deletions(-) delete mode 100644 pkg/config/config.go delete mode 100644 pkg/config/config_test.go diff --git a/cmd/api/main.go b/cmd/api/main.go index 04669cb..c8d60b5 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -11,18 +11,34 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/jessevdk/go-flags" log "github.com/sirupsen/logrus" "github.com/mxpv/podsync/pkg/api" "github.com/mxpv/podsync/pkg/builders" "github.com/mxpv/podsync/pkg/cache" - "github.com/mxpv/podsync/pkg/config" "github.com/mxpv/podsync/pkg/feeds" "github.com/mxpv/podsync/pkg/handler" "github.com/mxpv/podsync/pkg/storage" "github.com/mxpv/podsync/pkg/support" ) +type Opts struct { + YouTubeAPIKey string `long:"youtube-key" required:"true" env:"YOUTUBE_API_KEY"` + VimeoAPIKey string `long:"vimeo-key" required:"true" env:"VIMEO_API_KEY"` + PatreonClientID string `long:"patreon-client-id" required:"true" env:"PATREON_CLIENT_ID"` + PatreonSecret string `long:"patreon-secret" required:"true" env:"PATREON_SECRET"` + PatreonRedirectURL string `long:"patreon-redirect-url" required:"true" env:"PATREON_REDIRECT_URL"` + PatreonWebhooksSecret string `long:"patreon-webhook-secret" required:"true" env:"PATREON_WEBHOOKS_SECRET"` + PostgresConnectionURL string `long:"pg-url" env:"POSTGRES_CONNECTION_URL"` + CookieSecret string `long:"cookie-secret" required:"true" env:"COOKIE_SECRET"` + AWSAccessKey string `long:"aws-key" required:"true" env:"AWS_ACCESS_KEY"` + AWSAccessSecret string `long:"aws-secret" required:"true" env:"AWS_ACCESS_SECRET"` + DynamoFeedsTableName string `long:"dynamo-feeds-table" env:"DYNAMO_FEEDS_TABLE_NAME"` + DynamoPledgesTableName string `long:"dynamo-pledges-table" env:"DYNAMO_PLEDGES_TABLE_NAME"` + RedisURL string `long:"redis-url" required:"true" env:"REDIS_CONNECTION_URL"` +} + func main() { log.SetFormatter(&log.JSONFormatter{}) @@ -34,14 +50,14 @@ func main() { // Create core services - cfg, err := config.ReadConfiguration() - if err != nil { + var opts Opts + if _, err := flags.Parse(&opts); err != nil { log.WithError(err).Fatal("failed to read configuration") } awsCfg := &aws.Config{ Region: aws.String("us-east-1"), - Credentials: credentials.NewStaticCredentials(cfg.AWSAccessKey, cfg.AWSAccessSecret, ""), + Credentials: credentials.NewStaticCredentials(opts.AWSAccessKey, opts.AWSAccessSecret, ""), } database, err := storage.NewDynamo(awsCfg) @@ -49,31 +65,31 @@ func main() { log.WithError(err).Fatal("failed to create database") } - if cfg.DynamoPledgesTableName != "" { - database.PledgesTableName = aws.String(cfg.DynamoPledgesTableName) + if opts.DynamoPledgesTableName != "" { + database.PledgesTableName = aws.String(opts.DynamoPledgesTableName) } - if cfg.DynamoFeedsTableName != "" { - database.FeedsTableName = aws.String(cfg.DynamoFeedsTableName) + if opts.DynamoFeedsTableName != "" { + database.FeedsTableName = aws.String(opts.DynamoFeedsTableName) } patreon := support.NewPatreon(database) // Cache - redisCache, err := cache.NewRedisCache(cfg.RedisURL) + redisCache, err := cache.NewRedisCache(opts.RedisURL) if err != nil { log.WithError(err).Fatal("failed to initialize Redis cache") } // Builders - youtube, err := builders.NewYouTubeBuilder(cfg.YouTubeAPIKey) + youtube, err := builders.NewYouTubeBuilder(opts.YouTubeAPIKey) if err != nil { log.WithError(err).Fatal("failed to create YouTube builder") } - vimeo, err := builders.NewVimeoBuilder(ctx, cfg.VimeoAPIKey) + vimeo, err := builders.NewVimeoBuilder(ctx, opts.VimeoAPIKey) if err != nil { log.WithError(err).Fatal("failed to create Vimeo builder") } @@ -93,9 +109,17 @@ func main() { log.WithError(err).Fatal("failed to create feed service") } + web := handler.New(feed, patreon, handler.Opts{ + CookieSecret: opts.CookieSecret, + PatreonClientID: opts.PatreonClientID, + PatreonSecret: opts.PatreonSecret, + PatreonRedirectURL: opts.PatreonRedirectURL, + PatreonWebhooksSecret: opts.PatreonWebhooksSecret, + }) + srv := http.Server{ Addr: fmt.Sprintf(":%d", 5001), - Handler: handler.New(feed, patreon, cfg), + Handler: web, } go func() { diff --git a/go.mod b/go.mod index 814cb8b..6659a99 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/mxpv/podsync require ( cloud.google.com/go v0.25.0 // indirect github.com/BrianHicks/finch v0.0.0-20140409222414-419bd73c29ec - github.com/BurntSushi/toml v0.3.1 // indirect github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20170929212804-61590edac4c7 github.com/aws/aws-lambda-go v1.10.0 github.com/aws/aws-sdk-go v1.15.81 @@ -19,26 +18,18 @@ require ( github.com/go-redis/redis v6.15.2+incompatible github.com/golang/mock v1.2.0 github.com/gorilla/sessions v1.1.1 // indirect - github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect + github.com/jessevdk/go-flags v1.4.0 github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect github.com/kidstuff/mongostore v0.0.0-20180412085134-db2a8b4fac1f // indirect - github.com/magiconair/properties v1.8.0 // indirect github.com/mattn/go-isatty v0.0.3 // indirect github.com/memcachier/mc v2.0.1+incompatible // indirect - github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 // indirect github.com/mxpv/patreon-go v0.0.0-20180807002359-67dbab1ad14c github.com/mxpv/podcast v0.0.0-20170823220358-fe328ad87d18 github.com/onsi/ginkgo v1.7.0 // indirect github.com/onsi/gomega v1.4.3 // indirect - github.com/pelletier/go-toml v1.2.0 // indirect github.com/pkg/errors v0.8.0 github.com/silentsokolov/go-vimeo v0.0.0-20190116124215-06829264260c github.com/sirupsen/logrus v1.2.0 - github.com/spf13/afero v1.1.1 // indirect - github.com/spf13/cast v1.2.0 // indirect - github.com/spf13/jwalterweatherman v0.0.0-20180109140146-7c0cea34c8ec // indirect - github.com/spf13/pflag v1.0.1 // indirect - github.com/spf13/viper v1.0.2 github.com/stretchr/testify v1.2.2 github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf // indirect github.com/ugorji/go v1.1.1 // indirect diff --git a/go.sum b/go.sum index 197e8a7..8ccf3e6 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,6 @@ cloud.google.com/go v0.25.0 h1:6vD6xZTc8Jo6To8gHxFDRVsMvWFDgY3rugNszcDalN8= cloud.google.com/go v0.25.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BrianHicks/finch v0.0.0-20140409222414-419bd73c29ec h1:1VPruZMM1WQC7POhjxbZOWK564cuFz1hlpwYW6ocM4E= github.com/BrianHicks/finch v0.0.0-20140409222414-419bd73c29ec/go.mod h1:+hWo/MWgY8VtjZvdrYM2nPRMaK40zX2iPsH/qD0+Xs0= -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20170929212804-61590edac4c7 h1:Clo7QBZv+fHzjCgVp4ELlbIsY5rScCmj+4VCfoMfqtQ= github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20170929212804-61590edac4c7/go.mod h1:aJ4qN3TfrelA6NZ6AXsXRfmEVaYin3EDbSPJrKS8OXo= github.com/aws/aws-lambda-go v1.10.0 h1:uafgdfYGQD0UeT7d2uKdyWW8j/ZYRifRPIdmeqLzLCk= @@ -44,10 +42,10 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.1.1 h1:YMDmfaK68mUixINzY/XjscuJ47uXFWSSHzFbBQM0PrE= github.com/gorilla/sessions v1.1.1/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w= -github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce h1:xdsDDbiBDQTKASoGEZ+pEmF1OnWuu8AQ9I8iNbHNeno= -github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k= github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE= @@ -56,14 +54,10 @@ github.com/kidstuff/mongostore v0.0.0-20180412085134-db2a8b4fac1f h1:84d0qxD9Aiu github.com/kidstuff/mongostore v0.0.0-20180412085134-db2a8b4fac1f/go.mod h1:g2nVr8KZVXJSS97Jo8pJ0jgq29P6H7dG0oplUA86MQw= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= -github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/memcachier/mc v2.0.1+incompatible h1:s8EDz0xrJLP8goitwZOoq1vA/sm0fPS4X3KAF0nyhWQ= github.com/memcachier/mc v2.0.1+incompatible/go.mod h1:7bkvFE61leUBvXz+yxsOnGBQSZpBSPIMUQSmmSHvuXc= -github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 h1:KXZJFdun9knAVAR8tg/aHJEr5DgtcbqyvzacK+CDCaI= -github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mxpv/patreon-go v0.0.0-20180807002359-67dbab1ad14c h1:glRDvmNgmckYjQHMQ17XFlci6RehtmGmRQV+Cq6FusI= github.com/mxpv/patreon-go v0.0.0-20180807002359-67dbab1ad14c/go.mod h1:ksYjm2GAbGlgIP7jO9Q5/AdyE4MwwEbgQ+lFMx3hyiM= github.com/mxpv/podcast v0.0.0-20170823220358-fe328ad87d18 h1:YYsu49Y42JA+CSs9+z2MGBdGxb5jklpagLp5QPJ6BwQ= @@ -73,8 +67,6 @@ github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= -github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= -github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -83,16 +75,6 @@ github.com/silentsokolov/go-vimeo v0.0.0-20190116124215-06829264260c h1:KhHx/Ta3 github.com/silentsokolov/go-vimeo v0.0.0-20190116124215-06829264260c/go.mod h1:10FeaKUMy5t3KLsYfy54dFrq0rpwcfyKkKcF7vRGIRY= github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/spf13/afero v1.1.1 h1:Lt3ihYMlE+lreX1GS4Qw4ZsNpYQLxIXKBTEOXm3nt6I= -github.com/spf13/afero v1.1.1/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg= -github.com/spf13/cast v1.2.0/go.mod h1:r2rcYCSwa1IExKTDiTfzaxqT2FNHs8hODu4LnUfgKEg= -github.com/spf13/jwalterweatherman v0.0.0-20180109140146-7c0cea34c8ec h1:2ZXvIUGghLpdTVHR1UfvfrzoVlZaE/yOWC5LueIHZig= -github.com/spf13/jwalterweatherman v0.0.0-20180109140146-7c0cea34c8ec/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= -github.com/spf13/pflag v1.0.1 h1:aCvUg6QPl3ibpQUxyLkrEkCHtPqYJL4x9AuhqVqFis4= -github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/viper v1.0.2 h1:Ncr3ZIuJn322w2k1qmzXDnkLAdQMlJqBa9kfAH+irso= -github.com/spf13/viper v1.0.2/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/pkg/config/config.go b/pkg/config/config.go deleted file mode 100644 index bd8e9d0..0000000 --- a/pkg/config/config.go +++ /dev/null @@ -1,72 +0,0 @@ -package config - -import ( - "strings" - - "github.com/spf13/viper" -) - -const FileName = "podsync" - -type AppConfig struct { - YouTubeAPIKey string `yaml:"youtubeApiKey"` - VimeoAPIKey string `yaml:"vimeoApiKey"` - PatreonClientID string `yaml:"patreonClientId"` - PatreonSecret string `yaml:"patreonSecret"` - PatreonRedirectURL string `yaml:"patreonRedirectUrl"` - PatreonWebhooksSecret string `json:"patreonWebhooksSecret"` - PostgresConnectionURL string `yaml:"postgresConnectionUrl"` - CookieSecret string `yaml:"cookieSecret"` - AWSAccessKey string `yaml:"awsAccessKey"` - AWSAccessSecret string `yaml:"awsAccessSecret"` - DynamoFeedsTableName string `yaml:"dynamoFeedsTableName"` - DynamoPledgesTableName string `yaml:"dynamoPledgesTableName"` - RedisURL string `yaml:"redisUrl"` -} - -func ReadConfiguration() (*AppConfig, error) { - viper.SetConfigName(FileName) - - // Configuration file - viper.AddConfigPath(".") - viper.AddConfigPath("/app/config/") - - // Env variables - viper.AutomaticEnv() - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - - envmap := map[string]string{ - "youtubeApiKey": "YOUTUBE_API_KEY", - "vimeoApiKey": "VIMEO_API_KEY", - "patreonClientId": "PATREON_CLIENT_ID", - "patreonSecret": "PATREON_SECRET", - "patreonRedirectUrl": "PATREON_REDIRECT_URL", - "patreonWebhooksSecret": "PATREON_WEBHOOKS_SECRET", - "postgresConnectionUrl": "POSTGRES_CONNECTION_URL", - "redisUrl": "REDIS_CONNECTION_URL", - "cookieSecret": "COOKIE_SECRET", - "awsAccessKey": "AWS_ACCESS_KEY", - "awsAccessSecret": "AWS_ACCESS_SECRET", - "dynamoFeedsTableName": "DYNAMO_FEEDS_TABLE_NAME", - "dynamoPledgesTableName": "DYNAMO_PLEDGES_TABLE_NAME", - } - - for k, v := range envmap { - viper.BindEnv(k, v) - } - - err := viper.ReadInConfig() - if err != nil { - if _, ok := err.(viper.ConfigFileNotFoundError); !ok { - return nil, err - } - } - - cfg := &AppConfig{} - - if err := viper.Unmarshal(cfg); err != nil { - return nil, err - } - - return cfg, nil -} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go deleted file mode 100644 index 50c50a3..0000000 --- a/pkg/config/config_test.go +++ /dev/null @@ -1,83 +0,0 @@ -package config - -import ( - "io/ioutil" - "os" - "testing" - - "github.com/spf13/viper" - "github.com/stretchr/testify/require" -) - -const YamlConfig = ` -youtubeApiKey: "1" -vimeoApiKey: "2" -patreonClientId: "3" -patreonSecret: "4" -postgresConnectionUrl: "5" -cookieSecret: "6" -patreonRedirectUrl: "7" -patreonWebhooksSecret: "10" -dynamoFeedsTableName: "11" -dynamoPledgesTableName: "12" -awsAccessKey: "13" -awsAccessSecret: "14" -` - -func TestReadYaml(t *testing.T) { - defer viper.Reset() - - err := ioutil.WriteFile("./podsync.yaml", []byte(YamlConfig), 0644) - defer os.Remove("./podsync.yaml") - require.NoError(t, err) - - cfg, err := ReadConfiguration() - require.NoError(t, err) - - require.Equal(t, "1", cfg.YouTubeAPIKey) - require.Equal(t, "2", cfg.VimeoAPIKey) - require.Equal(t, "3", cfg.PatreonClientID) - require.Equal(t, "4", cfg.PatreonSecret) - require.Equal(t, "5", cfg.PostgresConnectionURL) - require.Equal(t, "6", cfg.CookieSecret) - require.Equal(t, "7", cfg.PatreonRedirectURL) - require.Equal(t, "10", cfg.PatreonWebhooksSecret) - require.Equal(t, "11", cfg.DynamoFeedsTableName) - require.Equal(t, "12", cfg.DynamoPledgesTableName) - require.Equal(t, "13", cfg.AWSAccessKey) - require.Equal(t, "14", cfg.AWSAccessSecret) -} - -func TestReadEnv(t *testing.T) { - defer viper.Reset() - defer os.Clearenv() - - os.Setenv("YOUTUBE_API_KEY", "11") - os.Setenv("VIMEO_API_KEY", "22") - os.Setenv("PATREON_CLIENT_ID", "33") - os.Setenv("PATREON_SECRET", "44") - os.Setenv("POSTGRES_CONNECTION_URL", "55") - os.Setenv("COOKIE_SECRET", "66") - os.Setenv("PATREON_REDIRECT_URL", "77") - os.Setenv("PATREON_WEBHOOKS_SECRET", "1010") - os.Setenv("DYNAMO_FEEDS_TABLE_NAME", "1111") - os.Setenv("DYNAMO_PLEDGES_TABLE_NAME", "1212") - os.Setenv("AWS_ACCESS_KEY", "1313") - os.Setenv("AWS_ACCESS_SECRET", "1414") - - cfg, err := ReadConfiguration() - require.NoError(t, err) - - require.Equal(t, "11", cfg.YouTubeAPIKey) - require.Equal(t, "22", cfg.VimeoAPIKey) - require.Equal(t, "33", cfg.PatreonClientID) - require.Equal(t, "44", cfg.PatreonSecret) - require.Equal(t, "55", cfg.PostgresConnectionURL) - require.Equal(t, "66", cfg.CookieSecret) - require.Equal(t, "77", cfg.PatreonRedirectURL) - require.Equal(t, "1010", cfg.PatreonWebhooksSecret) - require.Equal(t, "1111", cfg.DynamoFeedsTableName) - require.Equal(t, "1212", cfg.DynamoPledgesTableName) - require.Equal(t, "1313", cfg.AWSAccessKey) - require.Equal(t, "1414", cfg.AWSAccessSecret) -} diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index f0da16a..0026666 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -12,7 +12,6 @@ import ( "golang.org/x/oauth2" "github.com/mxpv/podsync/pkg/api" - "github.com/mxpv/podsync/pkg/config" "github.com/mxpv/podsync/pkg/session" log "github.com/sirupsen/logrus" @@ -35,32 +34,40 @@ type patreonService interface { GetFeatureLevelFromAmount(amount int) int } -type handler struct { - feed feedService - cfg *config.AppConfig - oauth2 oauth2.Config - patreon patreonService +type Opts struct { + CookieSecret string + PatreonClientID string + PatreonSecret string + PatreonRedirectURL string + PatreonWebhooksSecret string } -func New(feed feedService, support patreonService, cfg *config.AppConfig) http.Handler { +type handler struct { + feed feedService + oauth2 oauth2.Config + patreon patreonService + PatreonWebhooksSecret string +} + +func New(feed feedService, support patreonService, opts Opts) http.Handler { r := gin.New() r.Use(gin.Recovery()) - store := sessions.NewCookieStore([]byte(cfg.CookieSecret)) + store := sessions.NewCookieStore([]byte(opts.CookieSecret)) r.Use(sessions.Sessions("podsync", store)) h := handler{ - feed: feed, - patreon: support, - cfg: cfg, + feed: feed, + patreon: support, + PatreonWebhooksSecret: opts.PatreonWebhooksSecret, } // OAuth 2 configuration h.oauth2 = oauth2.Config{ - ClientID: cfg.PatreonClientID, - ClientSecret: cfg.PatreonSecret, - RedirectURL: cfg.PatreonRedirectURL, + ClientID: opts.PatreonClientID, + ClientSecret: opts.PatreonSecret, + RedirectURL: opts.PatreonRedirectURL, Scopes: []string{"users", "pledges-to-me", "my-campaign"}, Endpoint: oauth2.Endpoint{ AuthURL: patreon.AuthorizationURL, @@ -240,7 +247,7 @@ func (h handler) webhook(c *gin.Context) { // Verify signature signature := c.GetHeader(patreon.HeaderSignature) - valid, err := patreon.VerifySignature(body, h.cfg.PatreonWebhooksSecret, signature) + valid, err := patreon.VerifySignature(body, h.PatreonWebhooksSecret, signature) if err != nil { log.WithError(err).Error("failed to verify signature") c.Status(http.StatusBadRequest) diff --git a/pkg/handler/handler_test.go b/pkg/handler/handler_test.go index cbe64a9..7e7fcc5 100644 --- a/pkg/handler/handler_test.go +++ b/pkg/handler/handler_test.go @@ -13,11 +13,8 @@ import ( "github.com/stretchr/testify/require" "github.com/mxpv/podsync/pkg/api" - "github.com/mxpv/podsync/pkg/config" ) -var cfg = &config.AppConfig{} - func TestCreateFeed(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -35,7 +32,7 @@ func TestCreateFeed(t *testing.T) { patreon := NewMockpatreonService(ctrl) patreon.EXPECT().GetFeatureLevelByID(gomock.Any()).Return(api.DefaultFeatures) - srv := httptest.NewServer(New(feed, patreon, cfg)) + srv := httptest.NewServer(New(feed, patreon, Opts{})) defer srv.Close() query := `{"url": "https://youtube.com/channel/123", "page_size": 55, "quality": "low", "format": "audio"}` @@ -50,7 +47,7 @@ func TestCreateInvalidFeed(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - srv := httptest.NewServer(New(NewMockfeedService(ctrl), nil, cfg)) + srv := httptest.NewServer(New(NewMockfeedService(ctrl), nil, Opts{})) defer srv.Close() query := `{}` @@ -101,7 +98,7 @@ func TestGetFeed(t *testing.T) { feed := NewMockfeedService(ctrl) feed.EXPECT().BuildFeed("123").Return([]byte("Test"), nil) - srv := httptest.NewServer(New(feed, nil, cfg)) + srv := httptest.NewServer(New(feed, nil, Opts{})) defer srv.Close() resp, err := http.Get(srv.URL + "/123") @@ -116,7 +113,7 @@ func TestGetMetadata(t *testing.T) { feed := NewMockfeedService(ctrl) feed.EXPECT().GetMetadata("123").Times(1).Return(&api.Metadata{}, nil) - srv := httptest.NewServer(New(feed, nil, cfg)) + srv := httptest.NewServer(New(feed, nil, Opts{})) defer srv.Close() resp, err := http.Get(srv.URL + "/api/metadata/123")