From 323b7f4032d19c06523f2eaaf1d20d32e1330b54 Mon Sep 17 00:00:00 2001 From: jloup Date: Mon, 30 Apr 2018 22:28:59 +0200 Subject: [PATCH] Redis connection. --- Gopkg.lock | 17 ++++++++++++++++- Gopkg.toml | 4 ++++ cmd/app/conf.toml | 4 ++++ cmd/app/main.go | 9 +++++---- db/db.go | 30 +++++++++++++++++++++++++++--- 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 8d11158..d8ceccf 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -70,6 +70,21 @@ revision = "24dfe0572921e42ffe1035f7afbd40f9d97cb8c8" version = "v6.10.0" +[[projects]] + name = "github.com/go-redis/redis" + packages = [ + ".", + "internal", + "internal/consistenthash", + "internal/hashtag", + "internal/pool", + "internal/proto", + "internal/singleflight", + "internal/util" + ] + revision = "877867d2845fbaf86798befe410b6ceb6f5c29a3" + version = "v6.10.2" + [[projects]] name = "github.com/golang/protobuf" packages = ["proto"] @@ -162,6 +177,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "e3fa83f0fc133d3c408abdc47e5c2ca08062fec7c34ea04c21ea122492ab863d" + inputs-digest = "587bbc93998cd884863b3559ec6ef87ebf381b7bca10587a7e79c2c227c64ead" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 4feee4d..9721956 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -64,3 +64,7 @@ [[constraint]] branch = "master" name = "github.com/shopspring/decimal" + +[[constraint]] + name = "github.com/go-redis/redis" + version = "6.10.2" diff --git a/cmd/app/conf.toml b/cmd/app/conf.toml index 47964b4..bdbf58b 100644 --- a/cmd/app/conf.toml +++ b/cmd/app/conf.toml @@ -8,6 +8,10 @@ password="cryptoportfolio-dev" database="cryptoportfolio" address="localhost:5432" +[redis] +type="tcp" +address="cryptoportfolio.immae.eu:7617" + [api] domain="localhost" jwt_secret="secret" diff --git a/cmd/app/main.go b/cmd/app/main.go index 4bd77bd..65e8b5a 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -26,9 +26,10 @@ type ApiConfig struct { } type Config struct { - App AppConfig - Api ApiConfig - Db db.DBConfig + App AppConfig + Api ApiConfig + Db db.DBConfig + Redis db.RedisConfig utils.LogConfiguration Address string @@ -64,7 +65,7 @@ func init() { api.SetJwtSecretKey(C.Api.JwtSecret) - db.Init(C.Db) + db.Init(C.Db, C.Redis) if C.Mode == "production" { gin.SetMode(gin.ReleaseMode) diff --git a/db/db.go b/db/db.go index 7f87201..078cd58 100644 --- a/db/db.go +++ b/db/db.go @@ -4,13 +4,17 @@ import ( migrate "github.com/go-pg/migrations" "github.com/go-pg/pg" "github.com/go-pg/pg/orm" + "github.com/go-redis/redis" "github.com/jloup/utils" ) -var DB *pg.DB - var log = utils.StandardL().WithField("module", "db") +var ( + DB *pg.DB + Redis *redis.Client +) + type DBConfig struct { Address string Database string @@ -18,7 +22,13 @@ type DBConfig struct { Password string } -func Init(config DBConfig) { +type RedisConfig struct { + Type string // tcp or unix + Address string + Password string +} + +func Init(config DBConfig, redisConfig RedisConfig) { var err error DB = connect(config) @@ -27,6 +37,20 @@ func Init(config DBConfig) { if err != nil { log.Fatalf("cannot migratedb '%v'\n", err) } + + Redis = redis.NewClient(&redis.Options{ + Network: redisConfig.Type, + Addr: redisConfig.Address, + Password: redisConfig.Password, + DB: 0, + }) + + _, err = Redis.Ping().Result() + + if err != nil { + log.Fatalf("redis init error %s", err) + } + } func migratedb() error { -- 2.41.0