Browse Source

Clean up redis configuration (#108)

* Clean up redis configuration

- put redis configs into their own struct
- put a redisConf struct into the default config so that envstruct
  will populate it
- allow tweaking of retry, db index and read/write timeout settings
- update example config.yaml

This is potentially a breaking change for anyone who's been using
the redis backend already, but maybe that's just me? :)

* fix struct tags and s/Db/DB/

* remove unnecessary declarations
dependabot/npm_and_yarn/web/prismjs-1.21.0
memory 8 years ago
committed by Max Schmitt
parent
commit
7fd1287493
  1. 9
      build/config.yaml
  2. 19
      stores/redis/redis.go
  3. 5
      stores/store.go
  4. 18
      util/config.go

9
build/config.yaml

@ -1,8 +1,6 @@
ListenAddr: ':8080' # Consists of 'IP:Port', e.g. ':8080' listens on any IP and on Port 8080 ListenAddr: ':8080' # Consists of 'IP:Port', e.g. ':8080' listens on any IP and on Port 8080
BaseURL: 'http://localhost:3000' # Origin URL, required for the authentication via OAuth BaseURL: 'http://localhost:3000' # Origin URL, required for the authentication via OAuth
Backend: boltdb # Can be 'boltdb' or 'redis' Backend: boltdb # Can be 'boltdb' or 'redis'
RedisHost: localhost:6379 # If using the redis backend, a host:port combination
RedisPassword: replace me # if using the redis backend, a conneciton password.
DataDir: ./data # Contains: the database and the private key DataDir: ./data # Contains: the database and the private key
EnableDebugMode: true # Activates more detailed logging EnableDebugMode: true # Activates more detailed logging
EnableAccessLogs: true # Enable GIN access logs (default is true; set to false to disable access logging) EnableAccessLogs: true # Enable GIN access logs (default is true; set to false to disable access logging)
@ -22,3 +20,10 @@ Proxy: # only relevant when using the proxy authbackend
RequireUserHeader: false # If true, will reject connections that do not have the UserHeader set RequireUserHeader: false # If true, will reject connections that do not have the UserHeader set
UserHeader: "X-Goog-Authenticated-User-ID" # pull the unique user ID from this header UserHeader: "X-Goog-Authenticated-User-ID" # pull the unique user ID from this header
DisplayNameHeader: "X-Goog-Authenticated-User-Email" # pull the display naem from this header DisplayNameHeader: "X-Goog-Authenticated-User-Email" # pull the display naem from this header
Redis:
Host: localhost:6379 # host:port combination; required
Password: replace me # redis connection password; optional; default is none
Db: 0 # redis index (https://redis.io/commands/select); optional; default is 0
MaxRetries: 3 # maximum number of retries for a failed redis command
ReadTimeout: 3s # timeout for read operations; default is 3s. This is a golang time.ParseDuration string
WriteTimeout: 3s # timeout for write operations; default is 3s. This is a golang time.ParseDuration string

19
stores/redis/redis.go

@ -24,15 +24,26 @@ type Store struct {
} }
// New initializes connection to the redis instance. // New initializes connection to the redis instance.
func New(hostaddr, password string) (*Store, error) { func New(hostaddr, password string, db int, maxRetries int, readTimeout string, writeTimeout string) (*Store, error) {
var rt, wt time.Duration
var err error
if rt, err = time.ParseDuration(readTimeout); err != nil {
return nil, errors.Wrap(err, "Could not parse read timeout")
}
if wt, err = time.ParseDuration(writeTimeout); err != nil {
return nil, errors.Wrap(err, "Could not parse write timeout")
}
c := redis.NewClient(&redis.Options{ c := redis.NewClient(&redis.Options{
Addr: hostaddr, Addr: hostaddr,
Password: password, Password: password,
DB: 0, DB: db,
MaxRetries: maxRetries,
ReadTimeout: rt,
WriteTimeout: wt,
}) })
// if we can't talk to redis, fail fast // if we can't talk to redis, fail fast
_, err := c.Ping().Result() if _, err = c.Ping().Result(); err != nil {
if err != nil {
return nil, errors.Wrap(err, "Could not connect to redis db0") return nil, errors.Wrap(err, "Could not connect to redis db0")
} }
ret := &Store{c: c} ret := &Store{c: c}

5
stores/store.go

@ -43,7 +43,10 @@ func New() (*Store, error) {
var s shared.Storage var s shared.Storage
switch backend := util.GetConfig().Backend; backend { switch backend := util.GetConfig().Backend; backend {
case "redis": case "redis":
s, err = redis.New(util.GetConfig().RedisHost, util.GetConfig().RedisPassword) conf := util.GetConfig().Redis
s, err = redis.New(conf.Host, conf.Password, conf.DB,
conf.MaxRetries, conf.ReadTimeout,
conf.WriteTimeout)
case "boltdb": case "boltdb":
s, err = boltdb.New(filepath.Join(util.GetConfig().DataDir, "main.db")) s, err = boltdb.New(filepath.Join(util.GetConfig().DataDir, "main.db"))
default: default:

18
util/config.go

@ -18,8 +18,6 @@ type Configuration struct {
BaseURL string `yaml:"BaseURL" env:"BASE_URL"` BaseURL string `yaml:"BaseURL" env:"BASE_URL"`
DataDir string `yaml:"DataDir" env:"DATA_DIR"` DataDir string `yaml:"DataDir" env:"DATA_DIR"`
Backend string `yaml:"Backend" env:"BACKEND"` Backend string `yaml:"Backend" env:"BACKEND"`
RedisHost string `yaml:"RedisHost" env:"REDIS_HOST"`
RedisPassword string `yaml:"RedisPassword" env:"REDIS_PASSWORD"`
AuthBackend string `yaml:"AuthBackend" env:"AUTH_BACKEND"` AuthBackend string `yaml:"AuthBackend" env:"AUTH_BACKEND"`
UseSSL bool `yaml:"EnableSSL" env:"USE_SSL"` UseSSL bool `yaml:"EnableSSL" env:"USE_SSL"`
EnableDebugMode bool `yaml:"EnableDebugMode" env:"ENABLE_DEBUG_MODE"` EnableDebugMode bool `yaml:"EnableDebugMode" env:"ENABLE_DEBUG_MODE"`
@ -30,6 +28,16 @@ type Configuration struct {
GitHub oAuthConf `yaml:"GitHub" env:"GITHUB"` GitHub oAuthConf `yaml:"GitHub" env:"GITHUB"`
Microsoft oAuthConf `yaml:"Microsoft" env:"MICROSOFT"` Microsoft oAuthConf `yaml:"Microsoft" env:"MICROSOFT"`
Proxy proxyAuthConf `yaml:"Proxy" env:"PROXY"` Proxy proxyAuthConf `yaml:"Proxy" env:"PROXY"`
Redis redisConf `yaml:"Redis" env:"REDIS"`
}
type redisConf struct {
Host string `yaml:"Host" env:"REDIS_HOST"`
Password string `yaml:"Password" env:"REDIS_PASSWORD"`
DB int `yaml:"DB" env:"REDIS_DB"`
MaxRetries int `yaml:"MaxRetries" env:"REDIS_MAX_RETRIES"`
ReadTimeout string `yaml:"ReadTimeout" env:"REDIS_READ_TIMEOUT"`
WriteTimeout string `yaml:"WriteTimeout" env:"REDIS_WRITE_TIMEOUT"`
} }
type oAuthConf struct { type oAuthConf struct {
@ -55,6 +63,12 @@ var Config = Configuration{
UseSSL: false, UseSSL: false,
ShortedIDLength: 4, ShortedIDLength: 4,
AuthBackend: "oauth", AuthBackend: "oauth",
Redis: redisConf{
Host: "127.0.0.1:6379",
MaxRetries: 3,
ReadTimeout: "3s",
WriteTimeout: "3s",
},
} }
// ReadInConfig loads the Configuration and other needed folders for further usage // ReadInConfig loads the Configuration and other needed folders for further usage

Loading…
Cancel
Save