Merge pull request #139 from creekorful/harmonize-flags

Allow proper cache configuration
pull/140/head
Aloïs Micard 3 years ago committed by GitHub
commit 71d0d704db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -47,7 +47,7 @@ services:
--event-srv amqp://guest:guest@rabbitmq:5672
--event-prefetch 20
--config-api http://configapi:8080
--redis redis:6379
--cache-srv redis://redis:6379
restart: always
depends_on:
- rabbitmq
@ -86,7 +86,7 @@ services:
command: >
--log-level debug
--event-srv amqp://guest:guest@rabbitmq:5672
--redis redis:6379
--cache-srv redis://redis:6379
--default-value forbidden-hostnames="[]"
--default-value allowed-mime-types="[{\"content-type\":\"text/\",\"extensions\":[\"html\",\"php\",\"aspx\", \"htm\"]}]"
--default-value refresh-delay="{\"delay\": 0}"
@ -103,7 +103,7 @@ services:
--log-level debug
--event-srv amqp://guest:guest@rabbitmq:5672
--config-api http://configapi:8080
--redis redis:6379
--cache-srv redis://redis:6379
--tor-proxy torproxy:9050
restart: always
depends_on:

@ -4,6 +4,9 @@ import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"net/url"
"strconv"
"strings"
"time"
)
@ -13,13 +16,14 @@ type redisCache struct {
}
// NewRedisCache return a new Cache using redis as backend
func NewRedisCache(URI string, password, keyPrefix string) (Cache, error) {
func NewRedisCache(URI string, keyPrefix string) (Cache, error) {
opts, err := parseRedisOpts(URI)
if err != nil {
return nil, err
}
return &redisCache{
client: redis.NewClient(&redis.Options{
Addr: URI,
Password: password,
DB: 0,
}),
client: redis.NewClient(opts),
keyPrefix: keyPrefix,
}, nil
}
@ -104,3 +108,35 @@ func (rc *redisCache) getKey(key string) string {
return fmt.Sprintf("%s:%s", rc.keyPrefix, key)
}
func parseRedisOpts(URL string) (*redis.Options, error) {
u, err := url.Parse(URL)
if err != nil {
return nil, err
}
username := "default"
password := ""
if u := u.User; u != nil {
if u.Username() != "" {
username = u.Username()
}
if pass, exist := u.Password(); exist {
password = pass
}
}
db := 0
if u.Path != "/" {
if val, err := strconv.Atoi(strings.TrimPrefix(u.Path, "/")); err == nil {
db = val
}
}
return &redis.Options{
Addr: u.Host,
Username: username,
Password: password,
DB: db,
}, nil
}

@ -13,3 +13,41 @@ func TestRedisCache_GetKey(t *testing.T) {
t.Errorf("got %s want %s", got, "config:user")
}
}
func TestParseRedisOpts(t *testing.T) {
opts, err := parseRedisOpts("redis://redis:6379")
if err != nil {
t.FailNow()
}
if opts.Username != "default" {
t.Errorf("wrong username: (got: %s, want: %s)\n", opts.Username, "default")
}
if opts.Password != "" {
t.Errorf("wrong password: (got: %s, want: %s)\n", opts.Password, "")
}
if opts.Addr != "redis:6379" {
t.Errorf("wrong addr: (got: %s, want: %s)\n", opts.Addr, "redis:6379")
}
if opts.DB != 0 {
t.Errorf("wrong DB: (got: %d, want: %d)\n", opts.DB, 0)
}
opts, err = parseRedisOpts("redis://default:password@redis:6379/42")
if err != nil {
t.FailNow()
}
if opts.Username != "default" {
t.Errorf("wrong username: (got: %s, want: %s)\n", opts.Username, "default")
}
if opts.Password != "password" {
t.Errorf("wrong password: (got: %s, want: %s)\n", opts.Password, "password")
}
if opts.Addr != "redis:6379" {
t.Errorf("wrong addr: (got: %s, want: %s)\n", opts.Addr, "redis:6379")
}
if opts.DB != 42 {
t.Errorf("wrong DB: (got: %d, want: %d)\n", opts.DB, 42)
}
}

@ -43,8 +43,7 @@ const (
eventURIFlag = "event-srv"
configAPIURIFlag = "config-api"
redisURIFlag = "redis"
redisPassFlag = "redis-pass"
cacheSRVFlag = "cache-srv"
torURIFlag = "tor-proxy"
userAgentFlag = "user-agent"
)
@ -102,7 +101,7 @@ func (p *defaultProvider) Publisher() (event.Publisher, error) {
}
func (p *defaultProvider) Cache(keyPrefix string) (cache.Cache, error) {
return cache.NewRedisCache(p.ctx.String(redisURIFlag), p.ctx.String(redisPassFlag), keyPrefix)
return cache.NewRedisCache(p.ctx.String(cacheSRVFlag), keyPrefix)
}
func (p *defaultProvider) HTTPClient() (chttp.Client, error) {
@ -284,14 +283,10 @@ func getFeaturesFlags() map[Feature][]cli.Flag {
flags[CacheFeature] = []cli.Flag{
&cli.StringFlag{
Name: redisURIFlag,
Usage: "URI to the Redis server",
Name: cacheSRVFlag,
Usage: "URI to the cache server",
Required: true,
},
&cli.StringFlag{
Name: redisPassFlag,
Usage: "Redis server password",
},
}
flags[CrawlingFeature] = []cli.Flag{

Loading…
Cancel
Save