Проект разбит на более мелкие части

This commit is contained in:
ErickSkrauch
2016-07-05 01:28:09 +03:00
parent baa1cd3010
commit 6a7cc9ae77
12 changed files with 181 additions and 135 deletions

21
lib/routes/Skins.go Normal file
View File

@@ -0,0 +1,21 @@
package routes
import (
"net/http"
"github.com/gorilla/mux"
"log"
"elyby/minecraft-skinsystem/lib/tools"
)
func GetSkin(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)
if (err != nil) {
http.Redirect(w, r, "http://skins.minecraft.net/MinecraftSkins/" + username + ".png", 301)
log.Println("Cannot get skin for username " + username)
return
}
http.Redirect(w, r, rec.Url, 301);
}

38
lib/routes/Textures.go Normal file
View File

@@ -0,0 +1,38 @@
package routes
import (
"net/http"
"github.com/gorilla/mux"
"log"
"elyby/minecraft-skinsystem/lib/structures"
"encoding/json"
"elyby/minecraft-skinsystem/lib/tools"
)
func GetTextures(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)
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{
Url: rec.Url,
Hash: rec.Hash,
},
}
if (rec.IsSlim) {
textures.Skin.Metadata = &structures.SkinMetadata{
Model: "slim",
}
}
response,_ := json.Marshal(textures)
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}

7
lib/services/services.go Normal file
View File

@@ -0,0 +1,7 @@
package services
import (
"github.com/mediocregopher/radix.v2/redis"
)
var Redis *redis.Client

7
lib/structures/Skin.go Normal file
View File

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

View File

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

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

View File

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

48
lib/tools/tools.go Normal file
View File

@@ -0,0 +1,48 @@
package tools
import (
"strings"
"time"
"crypto/md5"
"strconv"
"encoding/hex"
"elyby/minecraft-skinsystem/lib/structures"
"elyby/minecraft-skinsystem/lib/services"
"encoding/json"
"log"
)
func ParseUsername(username string) string {
const suffix = ".png"
if strings.HasSuffix(username, suffix) {
username = strings.TrimSuffix(username, suffix)
}
return username
}
func BuildNonElyTexturesHash(username string) string {
n := time.Now()
hour := time.Date(n.Year(), n.Month(), n.Day(), n.Hour(), 0, 0, 0, time.UTC).Unix()
hasher := md5.New()
hasher.Write([]byte("non-ely-" + strconv.FormatInt(hour, 10) + "-" + username))
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)
}