mirror of
https://github.com/elyby/chrly.git
synced 2024-12-22 21:19:55 +05:30
Имплементации репозиториев теперь хранятся в том же пакете, что и базовое описание фабрики репозитория
This commit is contained in:
parent
d9629b5e83
commit
d51c358ef6
19
cmd/serve.go
19
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()
|
||||
|
@ -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."
|
||||
}
|
@ -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
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package files
|
||||
|
||||
type CapeNotFoundError struct {
|
||||
Who string
|
||||
}
|
||||
|
||||
func (e CapeNotFoundError) Error() string {
|
||||
return "Cape file not found."
|
||||
}
|
@ -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
|
||||
}
|
@ -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."
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package redis
|
||||
|
||||
type SkinNotFoundError struct {
|
||||
Who string
|
||||
}
|
||||
|
||||
func (e SkinNotFoundError) Error() string {
|
||||
return "Skin data not found."
|
||||
}
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user