mirror of
https://github.com/elyby/chrly.git
synced 2025-05-31 14:11:51 +05:30
Немного реструктуризации
Добавлен роут для смены скина
This commit is contained in:
37
lib/data/SkinItem.go
Normal file
37
lib/data/SkinItem.go
Normal 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
|
||||||
|
}
|
15
lib/data/TexturesResponse.go
Normal file
15
lib/data/TexturesResponse.go
Normal 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
30
lib/routes/SetSkin.go
Normal 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"))
|
||||||
|
}
|
@@ -1,16 +1,19 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
"elyby/minecraft-skinsystem/lib/tools"
|
"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"])
|
username := tools.ParseUsername(mux.Vars(r)["username"])
|
||||||
log.Println("request skin for username " + username);
|
log.Println("request skin for username " + username);
|
||||||
rec, err := tools.FindRecord(username)
|
rec, err := data.FindRecord(username)
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
http.Redirect(w, r, "http://skins.minecraft.net/MinecraftSkins/" + username + ".png", 301)
|
http.Redirect(w, r, "http://skins.minecraft.net/MinecraftSkins/" + username + ".png", 301)
|
||||||
log.Println("Cannot get skin for username " + username)
|
log.Println("Cannot get skin for username " + username)
|
@@ -1,33 +1,35 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"log"
|
"log"
|
||||||
"elyby/minecraft-skinsystem/lib/structures"
|
"net/http"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
|
"elyby/minecraft-skinsystem/lib/data"
|
||||||
"elyby/minecraft-skinsystem/lib/tools"
|
"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"])
|
username := tools.ParseUsername(mux.Vars(r)["username"])
|
||||||
log.Println("request textures for username " + username)
|
log.Println("request textures for username " + username)
|
||||||
|
|
||||||
rec, err := tools.FindRecord(username)
|
rec, err := data.FindRecord(username)
|
||||||
if (err != nil || rec.SkinId == 0) {
|
if (err != nil || rec.SkinId == 0) {
|
||||||
rec.Url = "http://skins.minecraft.net/MinecraftSkins/" + username + ".png"
|
rec.Url = "http://skins.minecraft.net/MinecraftSkins/" + username + ".png"
|
||||||
rec.Hash = string(tools.BuildNonElyTexturesHash(username))
|
rec.Hash = string(tools.BuildNonElyTexturesHash(username))
|
||||||
}
|
}
|
||||||
|
|
||||||
textures := structures.TexturesResponse{
|
textures := data.TexturesResponse{
|
||||||
Skin: &structures.Skin{
|
Skin: &data.Skin{
|
||||||
Url: rec.Url,
|
Url: rec.Url,
|
||||||
Hash: rec.Hash,
|
Hash: rec.Hash,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rec.IsSlim) {
|
if (rec.IsSlim) {
|
||||||
textures.Skin.Metadata = &structures.SkinMetadata{
|
textures.Skin.Metadata = &data.SkinMetadata{
|
||||||
Model: "slim",
|
Model: "slim",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +0,0 @@
|
|||||||
package structures
|
|
||||||
|
|
||||||
type Skin struct {
|
|
||||||
Url string `json:"url"`
|
|
||||||
Hash string `json:"hash"`
|
|
||||||
Metadata *SkinMetadata `json:"metadata,omitempty"`
|
|
||||||
}
|
|
@@ -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"`
|
|
||||||
}
|
|
@@ -1,5 +0,0 @@
|
|||||||
package structures
|
|
||||||
|
|
||||||
type SkinMetadata struct {
|
|
||||||
Model string `json:"model"`
|
|
||||||
}
|
|
@@ -1,5 +0,0 @@
|
|||||||
package structures
|
|
||||||
|
|
||||||
type TexturesResponse struct {
|
|
||||||
Skin *Skin `json:"SKIN"`
|
|
||||||
}
|
|
@@ -6,10 +6,6 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"strconv"
|
"strconv"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"elyby/minecraft-skinsystem/lib/structures"
|
|
||||||
"elyby/minecraft-skinsystem/lib/services"
|
|
||||||
"encoding/json"
|
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseUsername(username string) string {
|
func ParseUsername(username string) string {
|
||||||
@@ -30,19 +26,6 @@ func BuildNonElyTexturesHash(username string) string {
|
|||||||
return hex.EncodeToString(hasher.Sum(nil))
|
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 {
|
func BuildKey(username string) string {
|
||||||
return "username:" + strings.ToLower(username)
|
return "username:" + strings.ToLower(username)
|
||||||
}
|
}
|
||||||
|
22
lib/tools/tools_test.go
Normal file
22
lib/tools/tools_test.go
Normal 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:")
|
||||||
|
}
|
||||||
|
}
|
@@ -21,11 +21,12 @@ func main() {
|
|||||||
services.Redis = client
|
services.Redis = client
|
||||||
|
|
||||||
router := mux.NewRouter().StrictSlash(true)
|
router := mux.NewRouter().StrictSlash(true)
|
||||||
router.HandleFunc("/skins/{username}", routes.GetSkin)
|
router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET")
|
||||||
router.HandleFunc("/textures/{username}", routes.GetTextures)
|
router.HandleFunc("/textures/{username}", routes.Textures).Methods("GET")
|
||||||
router.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
|
router.HandleFunc("/system/setSkin", routes.SetSkin).Methods("POST") // TODO: убрать этого, т.к. он стар
|
||||||
w.Write([]byte("Hello"))
|
|
||||||
})
|
apiRouter := router.PathPrefix("/api").Subrouter()
|
||||||
|
apiRouter.HandleFunc("/user/{username}/skin", routes.SetSkin).Methods("POST")
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":80", router))
|
log.Fatal(http.ListenAndServe(":80", router))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user