Немного реструктуризации

Добавлен роут для смены скина
This commit is contained in:
ErickSkrauch 2016-07-06 01:25:05 +03:00
parent 87ca1191eb
commit c4cd95cddc
12 changed files with 127 additions and 62 deletions

37
lib/data/SkinItem.go Normal file
View File

@ -0,0 +1,37 @@
package data
import (
"log"
"encoding/json"
"elyby/minecraft-skinsystem/lib/services"
"elyby/minecraft-skinsystem/lib/tools"
)
type SkinItem struct {
UserId int `json:"userId"`
Nickname string `json:"nickname"`
SkinId int `json:"skinId"`
Url string `json:"url"`
Is1_8 bool `json:"is1_8"`
IsSlim bool `json:"isSlim"`
Hash string `json:"hash"`
}
func (s *SkinItem) Save() {
str, _ := json.Marshal(s)
services.Redis.Cmd("SET", tools.BuildKey(s.Nickname), str)
}
func FindRecord(username string) (SkinItem, error) {
var record SkinItem;
result, err := services.Redis.Cmd("GET", tools.BuildKey(username)).Str();
if (err == nil) {
decodeErr := json.Unmarshal([]byte(result), &record)
if (decodeErr != nil) {
log.Println("Cannot decode record data")
}
}
return record, err
}

View File

@ -0,0 +1,15 @@
package data
type TexturesResponse struct {
Skin *Skin `json:"SKIN"`
}
type Skin struct {
Url string `json:"url"`
Hash string `json:"hash"`
Metadata *SkinMetadata `json:"metadata,omitempty"`
}
type SkinMetadata struct {
Model string `json:"model"`
}

30
lib/routes/SetSkin.go Normal file
View File

@ -0,0 +1,30 @@
package routes
import (
"net/http"
"strings"
"strconv"
"elyby/minecraft-skinsystem/lib/data"
)
func SetSkin(w http.ResponseWriter, r *http.Request) {
key := r.Header.Get("X-Ely-key")
if key != "43fd2ce61b3f5704dfd729c1f2d6ffdb" {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Nice try"))
return
}
skin := new(data.SkinItem)
skin.Nickname = strings.ToLower(r.PostFormValue("nickname"))
skin.UserId, _ = strconv.Atoi(r.PostFormValue("userId"))
skin.SkinId, _ = strconv.Atoi(r.PostFormValue("skinId"))
skin.Hash = r.PostFormValue("hash")
skin.Is1_8, _ = strconv.ParseBool(r.PostFormValue("is1_8"))
skin.IsSlim, _ = strconv.ParseBool(r.PostFormValue("isSlim"))
skin.Url = r.PostFormValue("url")
skin.Save()
w.Write([]byte("OK"))
}

View File

@ -1,16 +1,19 @@
package routes
import (
"net/http"
"github.com/gorilla/mux"
"log"
"net/http"
"github.com/gorilla/mux"
"elyby/minecraft-skinsystem/lib/tools"
"elyby/minecraft-skinsystem/lib/data"
)
func GetSkin(w http.ResponseWriter, r *http.Request) {
func Skin(w http.ResponseWriter, r *http.Request) {
username := tools.ParseUsername(mux.Vars(r)["username"])
log.Println("request skin for username " + username);
rec, err := tools.FindRecord(username)
rec, err := data.FindRecord(username)
if (err != nil) {
http.Redirect(w, r, "http://skins.minecraft.net/MinecraftSkins/" + username + ".png", 301)
log.Println("Cannot get skin for username " + username)

View File

@ -1,33 +1,35 @@
package routes
import (
"net/http"
"github.com/gorilla/mux"
"log"
"elyby/minecraft-skinsystem/lib/structures"
"net/http"
"encoding/json"
"github.com/gorilla/mux"
"elyby/minecraft-skinsystem/lib/data"
"elyby/minecraft-skinsystem/lib/tools"
)
func GetTextures(w http.ResponseWriter, r *http.Request) {
func Textures(w http.ResponseWriter, r *http.Request) {
username := tools.ParseUsername(mux.Vars(r)["username"])
log.Println("request textures for username " + username)
rec, err := tools.FindRecord(username)
rec, err := data.FindRecord(username)
if (err != nil || rec.SkinId == 0) {
rec.Url = "http://skins.minecraft.net/MinecraftSkins/" + username + ".png"
rec.Hash = string(tools.BuildNonElyTexturesHash(username))
}
textures := structures.TexturesResponse{
Skin: &structures.Skin{
textures := data.TexturesResponse{
Skin: &data.Skin{
Url: rec.Url,
Hash: rec.Hash,
},
}
if (rec.IsSlim) {
textures.Skin.Metadata = &structures.SkinMetadata{
textures.Skin.Metadata = &data.SkinMetadata{
Model: "slim",
}
}

View File

@ -1,7 +0,0 @@
package structures
type Skin struct {
Url string `json:"url"`
Hash string `json:"hash"`
Metadata *SkinMetadata `json:"metadata,omitempty"`
}

View File

@ -1,11 +0,0 @@
package structures
type SkinItem struct {
UserId int `json:"userId"`
Nickname string `json:"nickname"`
SkinId int `json:"skinId"`
Url string `json:"url"`
Is1_8 bool `json:"is1_8"`
IsSlim bool `json:"isSlim"`
Hash string `json:"hash"`
}

View File

@ -1,5 +0,0 @@
package structures
type SkinMetadata struct {
Model string `json:"model"`
}

View File

@ -1,5 +0,0 @@
package structures
type TexturesResponse struct {
Skin *Skin `json:"SKIN"`
}

View File

@ -6,10 +6,6 @@ import (
"crypto/md5"
"strconv"
"encoding/hex"
"elyby/minecraft-skinsystem/lib/structures"
"elyby/minecraft-skinsystem/lib/services"
"encoding/json"
"log"
)
func ParseUsername(username string) string {
@ -30,19 +26,6 @@ func BuildNonElyTexturesHash(username string) string {
return hex.EncodeToString(hasher.Sum(nil))
}
func FindRecord(username string) (structures.SkinItem, error) {
var record structures.SkinItem;
result, err := services.Redis.Cmd("GET", BuildKey(username)).Str();
if (err == nil) {
decodeErr := json.Unmarshal([]byte(result), &record)
if (decodeErr != nil) {
log.Println("Cannot decode record data")
}
}
return record, err
}
func BuildKey(username string) string {
return "username:" + strings.ToLower(username)
}

22
lib/tools/tools_test.go Normal file
View File

@ -0,0 +1,22 @@
package tools_test
import (
"testing"
. "elyby/minecraft-skinsystem/lib/tools"
)
func TestParseUsername(t *testing.T) {
if ParseUsername("test.png") != "test" {
t.Error("Function should trim .png at end")
}
if ParseUsername("test") != "test" {
t.Error("Function should return string itself, if it not contains .png at end")
}
}
func TestBuildKey(t *testing.T) {
if BuildKey("Test") != "username:test" {
t.Error("Function shound convert string to lover case and concatenate it with usernmae:")
}
}

View File

@ -21,11 +21,12 @@ func main() {
services.Redis = client
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/skins/{username}", routes.GetSkin)
router.HandleFunc("/textures/{username}", routes.GetTextures)
router.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
})
router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET")
router.HandleFunc("/textures/{username}", routes.Textures).Methods("GET")
router.HandleFunc("/system/setSkin", routes.SetSkin).Methods("POST") // TODO: убрать этого, т.к. он стар
apiRouter := router.PathPrefix("/api").Subrouter()
apiRouter.HandleFunc("/user/{username}/skin", routes.SetSkin).Methods("POST")
log.Fatal(http.ListenAndServe(":80", router))
}