mirror of
https://github.com/elyby/chrly.git
synced 2024-12-23 21:50:03 +05:30
Применены рекомендации от index0h
This commit is contained in:
parent
07903cf9c8
commit
676ba03c37
@ -34,12 +34,12 @@ var serveCmd = &cobra.Command{
|
|||||||
|
|
||||||
// Skins repository
|
// Skins repository
|
||||||
logger.Info("Connecting to redis")
|
logger.Info("Connecting to redis")
|
||||||
skinsRepoCfg := &redis.Config{
|
skinsRepoCfg := &redis.RedisSkinsFactory{
|
||||||
//Addr: "redis:6379",
|
//Addr: "redis:6379",
|
||||||
Addr: "localhost:16379",
|
Addr: "localhost:16379",
|
||||||
PollSize: 10,
|
PollSize: 10,
|
||||||
}
|
}
|
||||||
skinsRepo, err := skinsRepoCfg.CreateRepo()
|
skinsRepo, err := skinsRepoCfg.Create()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Emergency(fmt.Sprintf("Error on creating skins repo: %v", err))
|
logger.Emergency(fmt.Sprintf("Error on creating skins repo: %v", err))
|
||||||
return
|
return
|
||||||
@ -48,10 +48,10 @@ var serveCmd = &cobra.Command{
|
|||||||
|
|
||||||
// Capes repository
|
// Capes repository
|
||||||
_, file, _, _ := runtime.Caller(0)
|
_, file, _, _ := runtime.Caller(0)
|
||||||
capesRepoCfg := &files.Config{
|
capesRepoCfg := &files.FilesystemCapesFactory{
|
||||||
StoragePath: path.Join(filepath.Dir(file), "data/capes"),
|
StoragePath: path.Join(filepath.Dir(file), "data/capes"),
|
||||||
}
|
}
|
||||||
capesRepo, err := capesRepoCfg.CreateRepo()
|
capesRepo, err := capesRepoCfg.Create()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Emergency(fmt.Sprintf("Error on creating capes repo: %v", err))
|
logger.Emergency(fmt.Sprintf("Error on creating capes repo: %v", err))
|
||||||
return
|
return
|
||||||
|
@ -2,6 +2,6 @@ package capes
|
|||||||
|
|
||||||
import "elyby/minecraft-skinsystem/model"
|
import "elyby/minecraft-skinsystem/model"
|
||||||
|
|
||||||
type CapesRepositoryConfig interface {
|
type CapesRepositoryCreator interface {
|
||||||
CreateRepo() (model.CapesRepository, error)
|
Create() (model.CapesRepository, error)
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,10 @@ package files
|
|||||||
|
|
||||||
import "elyby/minecraft-skinsystem/model"
|
import "elyby/minecraft-skinsystem/model"
|
||||||
|
|
||||||
type Config struct {
|
type FilesystemCapesFactory struct {
|
||||||
StoragePath string
|
StoragePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) CreateRepo() (model.CapesRepository, error) {
|
func (cfg *FilesystemCapesFactory) Create() (model.CapesRepository, error) {
|
||||||
return &filesDb{path: cfg.StoragePath}, nil
|
return &filesDb{path: cfg.StoragePath}, nil
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package files
|
package files
|
||||||
|
|
||||||
import "fmt"
|
type CapeNotFoundError struct {
|
||||||
|
|
||||||
type CapeNotFound struct {
|
|
||||||
Who string
|
Who string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e CapeNotFound) Error() string {
|
func (e CapeNotFoundError) Error() string {
|
||||||
return fmt.Sprintf("Cape file not found. Required username \"%v\"", e.Who)
|
return "Cape file not found."
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ func (repository *filesDb) FindByUsername(username string) (model.Cape, error) {
|
|||||||
capePath := path.Join(repository.path, strings.ToLower(username) + ".png")
|
capePath := path.Join(repository.path, strings.ToLower(username) + ".png")
|
||||||
file, err := os.Open(capePath)
|
file, err := os.Open(capePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return record, CapeNotFound{username}
|
return record, CapeNotFoundError{username}
|
||||||
}
|
}
|
||||||
|
|
||||||
record.File = file
|
record.File = file
|
||||||
|
@ -2,6 +2,6 @@ package skins
|
|||||||
|
|
||||||
import "elyby/minecraft-skinsystem/model"
|
import "elyby/minecraft-skinsystem/model"
|
||||||
|
|
||||||
type SkinsRepositoryConfig interface {
|
type SkinsRepositoryCreator interface {
|
||||||
CreateRepo() (model.SkinsRepository, error)
|
Create() (model.SkinsRepository, error)
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func (db *redisDb) FindByUsername(username string) (model.Skin, error) {
|
|||||||
redisKey := buildKey(username)
|
redisKey := buildKey(username)
|
||||||
response := db.conn.Cmd("GET", redisKey)
|
response := db.conn.Cmd("GET", redisKey)
|
||||||
if response.IsType(redis.Nil) {
|
if response.IsType(redis.Nil) {
|
||||||
return record, SkinNotFound{username}
|
return record, SkinNotFoundError{username}
|
||||||
}
|
}
|
||||||
|
|
||||||
encodedResult, err := response.Bytes()
|
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) {
|
func (db *redisDb) FindByUserId(id int) (model.Skin, error) {
|
||||||
response := db.conn.Cmd("HGET", accountIdToUsernameKey, id)
|
response := db.conn.Cmd("HGET", accountIdToUsernameKey, id)
|
||||||
if response.IsType(redis.Nil) {
|
if response.IsType(redis.Nil) {
|
||||||
return model.Skin{}, SkinNotFound{"unknown"}
|
return model.Skin{}, SkinNotFoundError{"unknown"}
|
||||||
}
|
}
|
||||||
|
|
||||||
username, _ := response.Str()
|
username, _ := response.Str()
|
||||||
|
@ -6,12 +6,12 @@ import (
|
|||||||
"github.com/mediocregopher/radix.v2/pool"
|
"github.com/mediocregopher/radix.v2/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type RedisSkinsFactory struct {
|
||||||
Addr string
|
Addr string
|
||||||
PollSize int
|
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)
|
conn, err := pool.New("tcp", cfg.Addr, cfg.PollSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -19,5 +19,5 @@ func (cfg *Config) CreateRepo() (model.SkinsRepository, error) {
|
|||||||
|
|
||||||
// TODO: здесь можно запустить горутину по восстановлению соединения
|
// TODO: здесь можно запустить горутину по восстановлению соединения
|
||||||
|
|
||||||
return &redisDb{conn: conn}, err
|
return &redisDb{conn: conn}, nil
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import "fmt"
|
type SkinNotFoundError struct {
|
||||||
|
|
||||||
type SkinNotFound struct {
|
|
||||||
Who string
|
Who string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e SkinNotFound) Error() string {
|
func (e SkinNotFoundError) Error() string {
|
||||||
return fmt.Sprintf("Skin data not found. Required username \"%v\"", e.Who)
|
return "Skin data not found."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user