From d51c358ef6d37ff334aa4eee7e3cd65d3e476bf9 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 10 Aug 2017 03:14:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BC=D0=BF=D0=BB=D0=B5=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D0=B0=D1=86=D0=B8=D0=B8=20=D1=80=D0=B5=D0=BF=D0=BE?= =?UTF-8?q?=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8=D0=B5=D0=B2=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D1=8C=20=D1=85=D1=80=D0=B0=D0=BD=D1=8F=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D0=B2=20=D1=82=D0=BE=D0=BC=20=D0=B6=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=B5,=20=D1=87=D1=82=D0=BE=20?= =?UTF-8?q?=D0=B8=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=BE=D0=B5=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=84=D0=B0=D0=B1?= =?UTF-8?q?=D1=80=D0=B8=D0=BA=D0=B8=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/serve.go | 19 +++---- db/capes/{config.go => base.go} | 8 +++ db/capes/files/db.go | 13 ----- db/capes/files/errors.go | 9 --- .../{files/repository.go => filesystem.go} | 11 +++- db/skins/{config.go => base.go} | 8 +++ db/skins/{redis/commands.go => redis.go} | 56 ++++++++++++++++++- db/skins/redis/db.go | 22 -------- db/skins/redis/errors.go | 9 --- db/skins/redis/tools.go | 35 ------------ 10 files changed, 87 insertions(+), 103 deletions(-) rename db/capes/{config.go => base.go} (56%) delete mode 100644 db/capes/files/db.go delete mode 100644 db/capes/files/errors.go rename db/capes/{files/repository.go => filesystem.go} (67%) rename db/skins/{config.go => base.go} (56%) rename db/skins/{redis/commands.go => redis.go} (54%) delete mode 100644 db/skins/redis/db.go delete mode 100644 db/skins/redis/errors.go delete mode 100644 db/skins/redis/tools.go diff --git a/cmd/serve.go b/cmd/serve.go index 1165de3..d505a83 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -1,23 +1,20 @@ package cmd import ( - "elyby/minecraft-skinsystem/daemon" - "elyby/minecraft-skinsystem/ui" - - "elyby/minecraft-skinsystem/db/skins/redis" - + "fmt" "path" "path/filepath" "runtime" - "elyby/minecraft-skinsystem/db/capes/files" - - "fmt" - "github.com/mono83/slf/rays" "github.com/mono83/slf/recievers/ansi" "github.com/mono83/slf/wd" "github.com/spf13/cobra" + + "elyby/minecraft-skinsystem/daemon" + "elyby/minecraft-skinsystem/db/capes" + "elyby/minecraft-skinsystem/db/skins" + "elyby/minecraft-skinsystem/ui" ) // serveCmd represents the serve command @@ -34,7 +31,7 @@ var serveCmd = &cobra.Command{ // Skins repository logger.Info("Connecting to redis") - skinsRepoCfg := &redis.RedisSkinsFactory{ + skinsRepoCfg := &skins.RedisSkinsFactory{ //Addr: "redis:6379", Addr: "localhost:16379", PollSize: 10, @@ -48,7 +45,7 @@ var serveCmd = &cobra.Command{ // Capes repository _, file, _, _ := runtime.Caller(0) - capesRepoCfg := &files.FilesystemCapesFactory{ + capesRepoCfg := &capes.FilesystemCapesFactory{ StoragePath: path.Join(filepath.Dir(file), "data/capes"), } capesRepo, err := capesRepoCfg.Create() diff --git a/db/capes/config.go b/db/capes/base.go similarity index 56% rename from db/capes/config.go rename to db/capes/base.go index 1f3fa34..d0903fc 100644 --- a/db/capes/config.go +++ b/db/capes/base.go @@ -7,3 +7,11 @@ import ( type CapesRepositoryCreator interface { Create() (repositories.CapesRepository, error) } + +type CapeNotFoundError struct { + Who string +} + +func (e CapeNotFoundError) Error() string { + return "Cape file not found." +} diff --git a/db/capes/files/db.go b/db/capes/files/db.go deleted file mode 100644 index f220ee2..0000000 --- a/db/capes/files/db.go +++ /dev/null @@ -1,13 +0,0 @@ -package files - -import ( - "elyby/minecraft-skinsystem/repositories" -) - -type FilesystemCapesFactory struct { - StoragePath string -} - -func (cfg *FilesystemCapesFactory) Create() (repositories.CapesRepository, error) { - return &filesDb{path: cfg.StoragePath}, nil -} diff --git a/db/capes/files/errors.go b/db/capes/files/errors.go deleted file mode 100644 index 8c3aa5e..0000000 --- a/db/capes/files/errors.go +++ /dev/null @@ -1,9 +0,0 @@ -package files - -type CapeNotFoundError struct { - Who string -} - -func (e CapeNotFoundError) Error() string { - return "Cape file not found." -} diff --git a/db/capes/files/repository.go b/db/capes/filesystem.go similarity index 67% rename from db/capes/files/repository.go rename to db/capes/filesystem.go index 9bbb3a9..f791305 100644 --- a/db/capes/files/repository.go +++ b/db/capes/filesystem.go @@ -1,4 +1,4 @@ -package files +package capes import ( "os" @@ -6,8 +6,17 @@ import ( "strings" "elyby/minecraft-skinsystem/model" + "elyby/minecraft-skinsystem/repositories" ) +type FilesystemCapesFactory struct { + StoragePath string +} + +func (cfg *FilesystemCapesFactory) Create() (repositories.CapesRepository, error) { + return &filesDb{path: cfg.StoragePath}, nil +} + type filesDb struct { path string } diff --git a/db/skins/config.go b/db/skins/base.go similarity index 56% rename from db/skins/config.go rename to db/skins/base.go index 48ab60a..9da5fbc 100644 --- a/db/skins/config.go +++ b/db/skins/base.go @@ -7,3 +7,11 @@ import ( type SkinsRepositoryCreator interface { Create() (repositories.SkinsRepository, error) } + +type SkinNotFoundError struct { + Who string +} + +func (e SkinNotFoundError) Error() string { + return "Skin data not found." +} diff --git a/db/skins/redis/commands.go b/db/skins/redis.go similarity index 54% rename from db/skins/redis/commands.go rename to db/skins/redis.go index 188b29a..56e0ad6 100644 --- a/db/skins/redis/commands.go +++ b/db/skins/redis.go @@ -1,15 +1,37 @@ -package redis +package skins import ( - "elyby/minecraft-skinsystem/model" - + "bytes" + "compress/zlib" "encoding/json" + "io" "log" + "strings" + "github.com/mediocregopher/radix.v2/pool" "github.com/mediocregopher/radix.v2/redis" "github.com/mediocregopher/radix.v2/util" + + "elyby/minecraft-skinsystem/model" + "elyby/minecraft-skinsystem/repositories" ) +type RedisSkinsFactory struct { + Addr string + PollSize int +} + +func (cfg *RedisSkinsFactory) Create() (repositories.SkinsRepository, error) { + conn, err := pool.New("tcp", cfg.Addr, cfg.PollSize) + if err != nil { + return nil, err + } + + // TODO: здесь можно запустить горутину по восстановлению соединения + + return &redisDb{conn: conn}, nil +} + type redisDb struct { conn util.Cmder } @@ -58,3 +80,31 @@ func (db *redisDb) FindByUserId(id int) (model.Skin, error) { return db.FindByUsername(username) } + +func buildKey(username string) string { + return "username:" + strings.ToLower(username) +} + +//noinspection GoUnusedFunction +func zlibEncode(str []byte) []byte { + var buff bytes.Buffer + writer := zlib.NewWriter(&buff) + writer.Write(str) + writer.Close() + + return buff.Bytes() +} + +func zlibDecode(bts []byte) ([]byte, error) { + buff := bytes.NewReader(bts) + reader, readError := zlib.NewReader(buff) + if readError != nil { + return nil, readError + } + + resultBuffer := new(bytes.Buffer) + io.Copy(resultBuffer, reader) + reader.Close() + + return resultBuffer.Bytes(), nil +} diff --git a/db/skins/redis/db.go b/db/skins/redis/db.go deleted file mode 100644 index 2df1e01..0000000 --- a/db/skins/redis/db.go +++ /dev/null @@ -1,22 +0,0 @@ -package redis - -import ( - "elyby/minecraft-skinsystem/repositories" - "github.com/mediocregopher/radix.v2/pool" -) - -type RedisSkinsFactory struct { - Addr string - PollSize int -} - -func (cfg *RedisSkinsFactory) Create() (repositories.SkinsRepository, error) { - conn, err := pool.New("tcp", cfg.Addr, cfg.PollSize) - if err != nil { - return nil, err - } - - // TODO: здесь можно запустить горутину по восстановлению соединения - - return &redisDb{conn: conn}, nil -} diff --git a/db/skins/redis/errors.go b/db/skins/redis/errors.go deleted file mode 100644 index a4388bb..0000000 --- a/db/skins/redis/errors.go +++ /dev/null @@ -1,9 +0,0 @@ -package redis - -type SkinNotFoundError struct { - Who string -} - -func (e SkinNotFoundError) Error() string { - return "Skin data not found." -} diff --git a/db/skins/redis/tools.go b/db/skins/redis/tools.go deleted file mode 100644 index 6de1f52..0000000 --- a/db/skins/redis/tools.go +++ /dev/null @@ -1,35 +0,0 @@ -package redis - -import ( - "bytes" - "compress/zlib" - "io" - "strings" -) - -func buildKey(username string) string { - return "username:" + strings.ToLower(username) -} - -func zlibEncode(str []byte) []byte { - var buff bytes.Buffer - writer := zlib.NewWriter(&buff) - writer.Write(str) - writer.Close() - - return buff.Bytes() -} - -func zlibDecode(bts []byte) ([]byte, error) { - buff := bytes.NewReader(bts) - reader, readError := zlib.NewReader(buff) - if readError != nil { - return nil, readError - } - - resultBuffer := new(bytes.Buffer) - io.Copy(resultBuffer, reader) - reader.Close() - - return resultBuffer.Bytes(), nil -}