diff --git a/lib/data/DataNotFound.go b/lib/data/DataNotFound.go new file mode 100644 index 0000000..0f5e5c2 --- /dev/null +++ b/lib/data/DataNotFound.go @@ -0,0 +1,11 @@ +package data + +import "fmt" + +type DataNotFound struct { + Who string +} + +func (e DataNotFound) Error() string { + return fmt.Sprintf("Skin data not found. Required username \"%v\"", e.Who) +} diff --git a/lib/data/SkinItem.go b/lib/data/SkinItem.go index c381ed5..ee666e1 100644 --- a/lib/data/SkinItem.go +++ b/lib/data/SkinItem.go @@ -6,6 +6,8 @@ import ( "elyby/minecraft-skinsystem/lib/services" "elyby/minecraft-skinsystem/lib/tools" + + "github.com/mediocregopher/radix.v2/redis" ) type SkinItem struct { @@ -25,14 +27,17 @@ func (s *SkinItem) Save() { func FindRecord(username string) (SkinItem, error) { var record SkinItem; - result, err := services.RedisPool.Cmd("GET", tools.BuildKey(username)).Str(); + response := services.RedisPool.Cmd("GET", tools.BuildKey(username)); + if (response.IsType(redis.Nil)) { + return record, DataNotFound{username} + } + + result, err := response.Str() if (err == nil) { decodeErr := json.Unmarshal([]byte(result), &record) if (decodeErr != nil) { log.Println("Cannot decode record data") } - } else { - log.Println("Error on request user data: " + err.Error()) } return record, err diff --git a/lib/routes/Cape.go b/lib/routes/Cape.go index ff58a28..20020b6 100644 --- a/lib/routes/Cape.go +++ b/lib/routes/Cape.go @@ -1,6 +1,7 @@ package routes import ( + "log" "net/http" "github.com/gorilla/mux" @@ -10,6 +11,7 @@ import ( func Cape(w http.ResponseWriter, r *http.Request) { username := tools.ParseUsername(mux.Vars(r)["username"]) + log.Println("request cape for username " + username) http.Redirect(w, r, "http://skins.minecraft.net/MinecraftCloaks/" + username + ".png", 301) } diff --git a/minecraft-skinsystem.go b/minecraft-skinsystem.go index 04c2b0b..49df237 100644 --- a/minecraft-skinsystem.go +++ b/minecraft-skinsystem.go @@ -17,12 +17,16 @@ import ( const redisString string = "redis:6379" func main() { + log.Println("Starting...") + runtime.GOMAXPROCS(runtime.NumCPU()) - pool, redisErr := pool.New("tcp", redisString, 10) + log.Println("Connecting to redis") + redisPool, redisErr := pool.New("tcp", redisString, 10) if redisErr != nil { log.Fatal("Redis unavailable") } + log.Println("Connected to redis") router := mux.NewRouter().StrictSlash(true) router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET").Name("skins") @@ -38,7 +42,7 @@ func main() { apiRouter := router.PathPrefix("/api").Subrouter() apiRouter.HandleFunc("/user/{username}/skin", routes.SetSkin).Methods("POST") - services.RedisPool = pool + services.RedisPool = redisPool services.Router = router /*go func() {