Применены рекомендации от index0h

This commit is contained in:
ErickSkrauch 2017-07-02 03:35:38 +03:00
parent 07903cf9c8
commit 676ba03c37
9 changed files with 22 additions and 27 deletions

View File

@ -34,12 +34,12 @@ var serveCmd = &cobra.Command{
// Skins repository
logger.Info("Connecting to redis")
skinsRepoCfg := &redis.Config{
skinsRepoCfg := &redis.RedisSkinsFactory{
//Addr: "redis:6379",
Addr: "localhost:16379",
PollSize: 10,
}
skinsRepo, err := skinsRepoCfg.CreateRepo()
skinsRepo, err := skinsRepoCfg.Create()
if err != nil {
logger.Emergency(fmt.Sprintf("Error on creating skins repo: %v", err))
return
@ -48,10 +48,10 @@ var serveCmd = &cobra.Command{
// Capes repository
_, file, _, _ := runtime.Caller(0)
capesRepoCfg := &files.Config{
capesRepoCfg := &files.FilesystemCapesFactory{
StoragePath: path.Join(filepath.Dir(file), "data/capes"),
}
capesRepo, err := capesRepoCfg.CreateRepo()
capesRepo, err := capesRepoCfg.Create()
if err != nil {
logger.Emergency(fmt.Sprintf("Error on creating capes repo: %v", err))
return

View File

@ -2,6 +2,6 @@ package capes
import "elyby/minecraft-skinsystem/model"
type CapesRepositoryConfig interface {
CreateRepo() (model.CapesRepository, error)
type CapesRepositoryCreator interface {
Create() (model.CapesRepository, error)
}

View File

@ -2,10 +2,10 @@ package files
import "elyby/minecraft-skinsystem/model"
type Config struct {
type FilesystemCapesFactory struct {
StoragePath string
}
func (cfg *Config) CreateRepo() (model.CapesRepository, error) {
func (cfg *FilesystemCapesFactory) Create() (model.CapesRepository, error) {
return &filesDb{path: cfg.StoragePath}, nil
}

View File

@ -1,11 +1,9 @@
package files
import "fmt"
type CapeNotFound struct {
type CapeNotFoundError struct {
Who string
}
func (e CapeNotFound) Error() string {
return fmt.Sprintf("Cape file not found. Required username \"%v\"", e.Who)
func (e CapeNotFoundError) Error() string {
return "Cape file not found."
}

View File

@ -17,7 +17,7 @@ func (repository *filesDb) FindByUsername(username string) (model.Cape, error) {
capePath := path.Join(repository.path, strings.ToLower(username) + ".png")
file, err := os.Open(capePath)
if err != nil {
return record, CapeNotFound{username}
return record, CapeNotFoundError{username}
}
record.File = file

View File

@ -2,6 +2,6 @@ package skins
import "elyby/minecraft-skinsystem/model"
type SkinsRepositoryConfig interface {
CreateRepo() (model.SkinsRepository, error)
type SkinsRepositoryCreator interface {
Create() (model.SkinsRepository, error)
}

View File

@ -21,7 +21,7 @@ func (db *redisDb) FindByUsername(username string) (model.Skin, error) {
redisKey := buildKey(username)
response := db.conn.Cmd("GET", redisKey)
if response.IsType(redis.Nil) {
return record, SkinNotFound{username}
return record, SkinNotFoundError{username}
}
encodedResult, err := response.Bytes()
@ -49,7 +49,7 @@ func (db *redisDb) FindByUsername(username string) (model.Skin, error) {
func (db *redisDb) FindByUserId(id int) (model.Skin, error) {
response := db.conn.Cmd("HGET", accountIdToUsernameKey, id)
if response.IsType(redis.Nil) {
return model.Skin{}, SkinNotFound{"unknown"}
return model.Skin{}, SkinNotFoundError{"unknown"}
}
username, _ := response.Str()

View File

@ -6,12 +6,12 @@ import (
"github.com/mediocregopher/radix.v2/pool"
)
type Config struct {
type RedisSkinsFactory struct {
Addr string
PollSize int
}
func (cfg *Config) CreateRepo() (model.SkinsRepository, error) {
func (cfg *RedisSkinsFactory) Create() (model.SkinsRepository, error) {
conn, err := pool.New("tcp", cfg.Addr, cfg.PollSize)
if err != nil {
return nil, err
@ -19,5 +19,5 @@ func (cfg *Config) CreateRepo() (model.SkinsRepository, error) {
// TODO: здесь можно запустить горутину по восстановлению соединения
return &redisDb{conn: conn}, err
return &redisDb{conn: conn}, nil
}

View File

@ -1,12 +1,9 @@
package redis
import "fmt"
type SkinNotFound struct {
type SkinNotFoundError struct {
Who string
}
func (e SkinNotFound) Error() string {
return fmt.Sprintf("Skin data not found. Required username \"%v\"", e.Who)
func (e SkinNotFoundError) Error() string {
return "Skin data not found."
}