From 376ae5a099d6bf1952bb54758e7dad085e625aa4 Mon Sep 17 00:00:00 2001 From: Naoki Kosaka Date: Sat, 16 Mar 2019 14:36:03 +0900 Subject: [PATCH] Use RedisUrl correctly. (#17) --- .circleci/config.yml | 2 +- State/state_test.go | 8 +++++--- cli/cli.go | 17 +++++++++++------ cli/cli_test.go | 4 ++-- main.go | 17 +++++++++++------ main_test.go | 4 ++-- readme.md | 2 +- worker/worker.go | 17 +++++++++++------ worker/worker_test.go | 4 ++-- 9 files changed, 46 insertions(+), 29 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5fb155d..b3bb30a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,7 +5,7 @@ jobs: docker: - image: circleci/golang environment: - REDIS_URL: localhost:6379 + REDIS_URL: redis://localhost:6379/0 - image: redis:alpine steps: - checkout diff --git a/State/state_test.go b/State/state_test.go index ac9a1e7..204db41 100644 --- a/State/state_test.go +++ b/State/state_test.go @@ -12,9 +12,11 @@ var redisClient *redis.Client func TestMain(m *testing.M) { viper.BindEnv("redis_url") - redisClient = redis.NewClient(&redis.Options{ - Addr: viper.GetString("redis_url"), - }) + redisOption, err := redis.ParseURL(viper.GetString("redis_url")) + if err != nil { + panic(err) + } + redisClient = redis.NewClient(redisOption) code := m.Run() os.Exit(code) diff --git a/cli/cli.go b/cli/cli.go index ec0a614..530a523 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -24,16 +24,21 @@ func initConfig() { viper.BindEnv("redis_url") hostkey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem")) hostname, _ = url.Parse("https://" + viper.GetString("relay_domain")) - redClient := redis.NewClient(&redis.Options{ - Addr: viper.GetString("redis_url"), - }) + redOption, err := redis.ParseURL(viper.GetString("redis_url")) + if err != nil { + panic(err) + } + redClient := redis.NewClient(redOption) var macConfig = &config.Config{ - Broker: "redis://" + viper.GetString("redis_url"), + Broker: viper.GetString("redis_url"), DefaultQueue: "relay", - ResultBackend: "redis://" + viper.GetString("redis_url"), + ResultBackend: viper.GetString("redis_url"), ResultsExpireIn: 5, } - macServer, _ = machinery.NewServer(macConfig) + macServer, err = machinery.NewServer(macConfig) + if err != nil { + panic(err) + } relayState = state.NewState(redClient) } diff --git a/cli/cli_test.go b/cli/cli_test.go index 24b48e8..7d47997 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -8,8 +8,8 @@ import ( ) func TestMain(m *testing.M) { - viper.Set("Actor_pem", "misc/testKey.pem") - viper.Set("Relay_domain", "relay.yukimochi.example.org") + viper.Set("actor_pem", "misc/testKey.pem") + viper.Set("relay_domain", "relay.yukimochi.example.org") initConfig() relayState.RedisClient.FlushAll().Result() diff --git a/main.go b/main.go index e7092c9..df54179 100644 --- a/main.go +++ b/main.go @@ -37,16 +37,21 @@ func initConfig() { viper.BindEnv("redis_url") hostURL, _ = url.Parse("https://" + viper.GetString("relay_domain")) hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem")) - redisClient := redis.NewClient(&redis.Options{ - Addr: viper.GetString("redis_url"), - }) + redisOption, err := redis.ParseURL(viper.GetString("redis_url")) + if err != nil { + panic(err) + } + redisClient := redis.NewClient(redisOption) machineryConfig := &config.Config{ - Broker: "redis://" + viper.GetString("redis_url"), + Broker: viper.GetString("redis_url"), DefaultQueue: "relay", - ResultBackend: "redis://" + viper.GetString("redis_url"), + ResultBackend: viper.GetString("redis_url"), ResultsExpireIn: 5, } - machineryServer, _ = machinery.NewServer(machineryConfig) + machineryServer, err = machinery.NewServer(machineryConfig) + if err != nil { + panic(err) + } uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.2; " + hostURL.Host + ")" relayState = state.NewState(redisClient) actorCache = cache.New(5*time.Minute, 10*time.Minute) diff --git a/main_test.go b/main_test.go index 3d3744d..7eef040 100644 --- a/main_test.go +++ b/main_test.go @@ -8,8 +8,8 @@ import ( ) func TestMain(m *testing.M) { - viper.Set("Actor_pem", "misc/testKey.pem") - viper.Set("Relay_domain", "relay.yukimochi.example.org") + viper.Set("actor_pem", "misc/testKey.pem") + viper.Set("relay_domain", "relay.yukimochi.example.org") initConfig() // Load Config diff --git a/readme.md b/readme.md index 24ead19..f336d71 100644 --- a/readme.md +++ b/readme.md @@ -28,7 +28,7 @@ See [GitHub wiki](https://github.com/yukimochi/Activity-Relay/wiki) - `RELAY_DOMAIN` (ex. `relay.toot.yukimochi.jp`) - `RELAY_SERVICENAME` (ex. `YUKIMOCHI Toot Relay Service`) - `RELAY_BIND` (ex. `0.0.0.0:8080`) - - `REDIS_URL` (ex. `127.0.0.1:6379`) + - `REDIS_URL` (ex. `redis://127.0.0.1:6379/0`) ## License diff --git a/worker/worker.go b/worker/worker.go index 67b1ebc..b5c3c1b 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -54,16 +54,21 @@ func initConfig() { viper.BindEnv("redis_url") hostURL, _ = url.Parse("https://" + viper.GetString("relay_domain")) hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem")) - redisClient = redis.NewClient(&redis.Options{ - Addr: viper.GetString("redis_url"), - }) + redisOption, err := redis.ParseURL(viper.GetString("redis_url")) + if err != nil { + panic(err) + } + redisClient = redis.NewClient(redisOption) machineryConfig := &config.Config{ - Broker: "redis://" + viper.GetString("redis_url"), + Broker: viper.GetString("redis_url"), DefaultQueue: "relay", - ResultBackend: "redis://" + viper.GetString("redis_url"), + ResultBackend: viper.GetString("redis_url"), ResultsExpireIn: 5, } - machineryServer, _ = machinery.NewServer(machineryConfig) + machineryServer, err = machinery.NewServer(machineryConfig) + if err != nil { + panic(err) + } newNullLogger := NewNullLogger() log.DEBUG = newNullLogger uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.2; " + hostURL.Host + ")" diff --git a/worker/worker_test.go b/worker/worker_test.go index dd9d16b..4293cdd 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -12,8 +12,8 @@ import ( ) func TestMain(m *testing.M) { - viper.Set("Actor_pem", "../misc/testKey.pem") - viper.Set("Relay_domain", "relay.yukimochi.example.org") + viper.Set("actor_pem", "../misc/testKey.pem") + viper.Set("relay_domain", "relay.yukimochi.example.org") initConfig() redisClient.FlushAll().Result()