Merge remote-tracking branch 'origin/develop' into 66-add-kubernetes-config-files

pull/142/head
Aloïs Micard 3 years ago
commit 66481a3f55
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -0,0 +1,16 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [1.0.0-rc1] - 2021-01-12
Initial stable release candidate.
[unreleased]: https://github.com/creekorful/bathyscaphe/compare/v1.0.0-rc1...HEAD
[v1.0.0-rc1]: https://github.com/creekorful/bathyscaphe/releases/tag/v1.0.0-rc1

@ -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"
)
@ -14,11 +17,13 @@ type redisCache struct {
// NewRedisCache return a new Cache using redis as backend
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,
DB: 0,
}),
client: redis.NewClient(opts),
keyPrefix: keyPrefix,
}, nil
}
@ -103,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)
}
}

@ -27,7 +27,7 @@ import (
type Feature int
const (
version = "0.11.0"
version = "1.0.0-rc1"
// EventFeature is the feature to plug the process to the event server
EventFeature Feature = iota
@ -43,7 +43,7 @@ const (
eventURIFlag = "event-srv"
configAPIURIFlag = "config-api"
redisURIFlag = "redis"
cacheSRVFlag = "cache-srv"
torURIFlag = "tor-proxy"
userAgentFlag = "user-agent"
)
@ -101,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), keyPrefix)
return cache.NewRedisCache(p.ctx.String(cacheSRVFlag), keyPrefix)
}
func (p *defaultProvider) HTTPClient() (chttp.Client, error) {
@ -283,8 +283,8 @@ 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,
},
}

Loading…
Cancel
Save