Skip to content

Commit

Permalink
Move config & add backend factory
Browse files Browse the repository at this point in the history
  • Loading branch information
dewey committed Aug 29, 2018
1 parent b9ea8c3 commit 6228413
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

// Config holds our app configuration
type Config struct {
RefreshInterval int `env:"REFRESH_INTERVAL" envDefault:"15"`
CacheExpiration int `env:"CACHE_EXPIRATION" envDefault:"30"`
CacheExpiredPurge int `env:"CACHE_EXPIRED_PURGE" envDefault:"60"`
StorageBackend string `env:"STORAGE_BACKEND" envDefault:"memory"`
StoragePath string `env:"STORAGE_PATH" envDefault:"feedbridge-data"`
Environment string `env:"ENVIRONMENT" envDefault:"develop"`
Port int `env:"PORT" envDefault:"8080"`
}
29 changes: 1 addition & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,13 @@ import (
"github.com/dewey/feedbridge/plugin"
"github.com/dewey/feedbridge/plugins/scmp"
"github.com/dewey/feedbridge/runner"
"github.com/dewey/feedbridge/store"
"github.com/go-chi/chi"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)

var config struct {
RefreshInterval int `env:"REFRESH_INTERVAL" envDefault:"15"`
CacheExpiration int `env:"CACHE_EXPIRATION" envDefault:"30"`
CacheExpiredPurge int `env:"CACHE_EXPIRED_PURGE" envDefault:"60"`
StorageBackend string `env:"STORAGE_BACKEND" envDefault:"memory"`
StoragePath string `env:"STORAGE_PATH" envDefault:"feedbridge-data"`
Environment string `env:"ENVIRONMENT" envDefault:"develop"`
Port int `env:"PORT" envDefault:"8080"`
}

func main() {
err := env.Parse(&config)
err := env.Parse(&config.Config)
if err != nil {
panic(err)
}
Expand All @@ -50,22 +39,6 @@ func main() {
}
l = log.With(l, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)

var storageRepo store.StorageRepository
switch config.StorageBackend {
case "memory":
memory, err := store.NewMemRepository(config.CacheExpiration, config.CacheExpiredPurge)
if err != nil {
return
}
storageRepo = memory
case "persistent":
disk, err := store.NewDiskRepository(config.StoragePath)
if err != nil {
return
}
storageRepo = disk
}

t := &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
Expand Down
22 changes: 22 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package store

import "github.com/dewey/feedbridge/config"

// StorageRepository defines the interface for a k/v backend
type StorageRepository interface {
// Save stores an item in the store
Expand All @@ -8,3 +10,23 @@ type StorageRepository interface {
// Get item from store
Get(string) (string, error)
}

// NewStoreBackend is a factory to return a store implementation based on the config choosen
func NewStoreBackend(cfg config.Config) (*StorageRepository, error) {
var storageRepo *StorageRepository
switch cfg.StorageBackend {
case "memory":
memory, err := NewMemRepository(config.CacheExpiration, config.CacheExpiredPurge)
if err != nil {
return nil, err
}
storageRepo = memory
case "persistent":
disk, err := NewDiskRepository(config.StoragePath)
if err != nil {
return nil, err
}
storageRepo = disk
}
return storageRepo, nil
}

0 comments on commit 6228413

Please sign in to comment.