Переработка структуры проекта

This commit is contained in:
ErickSkrauch
2017-06-30 18:40:25 +03:00
parent e090d04dc7
commit 07903cf9c8
48 changed files with 894 additions and 1061 deletions

7
db/capes/config.go Normal file
View File

@@ -0,0 +1,7 @@
package capes
import "elyby/minecraft-skinsystem/model"
type CapesRepositoryConfig interface {
CreateRepo() (model.CapesRepository, error)
}

11
db/capes/files/db.go Normal file
View File

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

11
db/capes/files/errors.go Normal file
View File

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

View File

@@ -0,0 +1,26 @@
package files
import (
"os"
"path"
"strings"
"elyby/minecraft-skinsystem/model"
)
type filesDb struct {
path string
}
func (repository *filesDb) FindByUsername(username string) (model.Cape, error) {
var record model.Cape
capePath := path.Join(repository.path, strings.ToLower(username) + ".png")
file, err := os.Open(capePath)
if err != nil {
return record, CapeNotFound{username}
}
record.File = file
return record, nil
}

7
db/skins/config.go Normal file
View File

@@ -0,0 +1,7 @@
package skins
import "elyby/minecraft-skinsystem/model"
type SkinsRepositoryConfig interface {
CreateRepo() (model.SkinsRepository, error)
}

View File

@@ -0,0 +1,58 @@
package redis
import (
"elyby/minecraft-skinsystem/model"
"encoding/json"
"log"
"github.com/mediocregopher/radix.v2/redis"
"github.com/mediocregopher/radix.v2/util"
)
type redisDb struct {
conn util.Cmder
}
const accountIdToUsernameKey string = "hash:username-to-account-id"
func (db *redisDb) FindByUsername(username string) (model.Skin, error) {
var record model.Skin
redisKey := buildKey(username)
response := db.conn.Cmd("GET", redisKey)
if response.IsType(redis.Nil) {
return record, SkinNotFound{username}
}
encodedResult, err := response.Bytes()
if err == nil {
result, err := zlibDecode(encodedResult)
if err != nil {
log.Println("Cannot uncompress zlib for key " + redisKey)
goto finish
}
err = json.Unmarshal(result, &record)
if err != nil {
log.Println("Cannot decode record data for key" + redisKey)
goto finish
}
record.OldUsername = record.Username
}
finish:
return record, err
}
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"}
}
username, _ := response.Str()
return db.FindByUsername(username)
}

23
db/skins/redis/db.go Normal file
View File

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

12
db/skins/redis/errors.go Normal file
View File

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

35
db/skins/redis/tools.go Normal file
View File

@@ -0,0 +1,35 @@
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
}