Browse Source

Merge branch 'master' of https://github.com/mxschmitt/golang-url-shortener

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
7461418145
  1. 0
      LICENSE
  2. 9
      build/config.yaml
  3. 5
      renovate.json
  4. 19
      stores/redis/redis.go
  5. 5
      stores/store.go
  6. 18
      util/config.go

0
LICENSE.md → LICENSE

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
BaseURL: 'http://localhost:3000' # Origin URL, required for the authentication via OAuth
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
EnableDebugMode: true # Activates more detailed 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
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
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

5
renovate.json

@ -1,5 +0,0 @@
{
"extends": [
"config:base"
]
}

19
stores/redis/redis.go

@ -24,15 +24,26 @@ type Store struct {
}
// 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{
Addr: hostaddr,
Password: password,
DB: 0,
DB: db,
MaxRetries: maxRetries,
ReadTimeout: rt,
WriteTimeout: wt,
})
// if we can't talk to redis, fail fast
_, err := c.Ping().Result()
if err != nil {
if _, err = c.Ping().Result(); err != nil {
return nil, errors.Wrap(err, "Could not connect to redis db0")
}
ret := &Store{c: c}

5
stores/store.go

@ -43,7 +43,10 @@ func New() (*Store, error) {
var s shared.Storage
switch backend := util.GetConfig().Backend; backend {
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":
s, err = boltdb.New(filepath.Join(util.GetConfig().DataDir, "main.db"))
default:

18
util/config.go

@ -18,8 +18,6 @@ type Configuration struct {
BaseURL string `yaml:"BaseURL" env:"BASE_URL"`
DataDir string `yaml:"DataDir" env:"DATA_DIR"`
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"`
UseSSL bool `yaml:"EnableSSL" env:"USE_SSL"`
EnableDebugMode bool `yaml:"EnableDebugMode" env:"ENABLE_DEBUG_MODE"`
@ -30,6 +28,16 @@ type Configuration struct {
GitHub oAuthConf `yaml:"GitHub" env:"GITHUB"`
Microsoft oAuthConf `yaml:"Microsoft" env:"MICROSOFT"`
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 {
@ -55,6 +63,12 @@ var Config = Configuration{
UseSSL: false,
ShortedIDLength: 4,
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

Loading…
Cancel
Save