Имплементации репозиториев теперь хранятся в том же пакете, что и базовое описание фабрики репозитория

This commit is contained in:
ErickSkrauch 2017-08-10 03:14:28 +03:00
parent d9629b5e83
commit d51c358ef6
10 changed files with 87 additions and 103 deletions

View File

@ -1,23 +1,20 @@
package cmd package cmd
import ( import (
"elyby/minecraft-skinsystem/daemon" "fmt"
"elyby/minecraft-skinsystem/ui"
"elyby/minecraft-skinsystem/db/skins/redis"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"elyby/minecraft-skinsystem/db/capes/files"
"fmt"
"github.com/mono83/slf/rays" "github.com/mono83/slf/rays"
"github.com/mono83/slf/recievers/ansi" "github.com/mono83/slf/recievers/ansi"
"github.com/mono83/slf/wd" "github.com/mono83/slf/wd"
"github.com/spf13/cobra" "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 // serveCmd represents the serve command
@ -34,7 +31,7 @@ var serveCmd = &cobra.Command{
// Skins repository // Skins repository
logger.Info("Connecting to redis") logger.Info("Connecting to redis")
skinsRepoCfg := &redis.RedisSkinsFactory{ skinsRepoCfg := &skins.RedisSkinsFactory{
//Addr: "redis:6379", //Addr: "redis:6379",
Addr: "localhost:16379", Addr: "localhost:16379",
PollSize: 10, PollSize: 10,
@ -48,7 +45,7 @@ var serveCmd = &cobra.Command{
// Capes repository // Capes repository
_, file, _, _ := runtime.Caller(0) _, file, _, _ := runtime.Caller(0)
capesRepoCfg := &files.FilesystemCapesFactory{ capesRepoCfg := &capes.FilesystemCapesFactory{
StoragePath: path.Join(filepath.Dir(file), "data/capes"), StoragePath: path.Join(filepath.Dir(file), "data/capes"),
} }
capesRepo, err := capesRepoCfg.Create() capesRepo, err := capesRepoCfg.Create()

View File

@ -7,3 +7,11 @@ import (
type CapesRepositoryCreator interface { type CapesRepositoryCreator interface {
Create() (repositories.CapesRepository, error) Create() (repositories.CapesRepository, error)
} }
type CapeNotFoundError struct {
Who string
}
func (e CapeNotFoundError) Error() string {
return "Cape file not found."
}

View File

@ -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
}

View File

@ -1,9 +0,0 @@
package files
type CapeNotFoundError struct {
Who string
}
func (e CapeNotFoundError) Error() string {
return "Cape file not found."
}

View File

@ -1,4 +1,4 @@
package files package capes
import ( import (
"os" "os"
@ -6,8 +6,17 @@ import (
"strings" "strings"
"elyby/minecraft-skinsystem/model" "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 { type filesDb struct {
path string path string
} }

View File

@ -7,3 +7,11 @@ import (
type SkinsRepositoryCreator interface { type SkinsRepositoryCreator interface {
Create() (repositories.SkinsRepository, error) Create() (repositories.SkinsRepository, error)
} }
type SkinNotFoundError struct {
Who string
}
func (e SkinNotFoundError) Error() string {
return "Skin data not found."
}

View File

@ -1,15 +1,37 @@
package redis package skins
import ( import (
"elyby/minecraft-skinsystem/model" "bytes"
"compress/zlib"
"encoding/json" "encoding/json"
"io"
"log" "log"
"strings"
"github.com/mediocregopher/radix.v2/pool"
"github.com/mediocregopher/radix.v2/redis" "github.com/mediocregopher/radix.v2/redis"
"github.com/mediocregopher/radix.v2/util" "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 { type redisDb struct {
conn util.Cmder conn util.Cmder
} }
@ -58,3 +80,31 @@ func (db *redisDb) FindByUserId(id int) (model.Skin, error) {
return db.FindByUsername(username) 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
}

View File

@ -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
}

View File

@ -1,9 +0,0 @@
package redis
type SkinNotFoundError struct {
Who string
}
func (e SkinNotFoundError) Error() string {
return "Skin data not found."
}

View File

@ -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
}