From c2d0cb93cbf49c7409c763b773c5cf3e5cc072e5 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 28 Jul 2016 18:17:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B0=20=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=86=D0=B0=20404=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BF=D0=BE=D0=B4=D0=BA=D0=BB?= =?UTF-8?q?=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=20Redis=20=D0=B2?= =?UTF-8?q?=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5,=20=D0=B5=D1=81=D0=BB?= =?UTF-8?q?=D0=B8=20=D1=81=D0=BE=D0=B5=D0=B4=D0=B8=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=83=D0=BF=D0=B0=D0=B4=D1=91=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/SkinItem.go | 2 ++ lib/routes/NotFound.go | 2 +- minecraft-skinsystem.go | 27 +++++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/data/SkinItem.go b/lib/data/SkinItem.go index 3916ccd..fdad40a 100644 --- a/lib/data/SkinItem.go +++ b/lib/data/SkinItem.go @@ -31,6 +31,8 @@ func FindRecord(username string) (SkinItem, error) { if (decodeErr != nil) { log.Println("Cannot decode record data") } + } else { + log.Println("Error on request user data") } return record, err diff --git a/lib/routes/NotFound.go b/lib/routes/NotFound.go index 8460956..9cd1a44 100644 --- a/lib/routes/NotFound.go +++ b/lib/routes/NotFound.go @@ -9,7 +9,7 @@ func NotFound(w http.ResponseWriter, r *http.Request) { json, _ := json.Marshal(map[string]string{ "status": "404", "message": "Not Found", - "link": "http://docs.ely.by", + "link": "http://docs.ely.by/skin-system.html", }) w.Header().Set("Content-Type", "application/json") diff --git a/minecraft-skinsystem.go b/minecraft-skinsystem.go index 0090ff5..044e776 100644 --- a/minecraft-skinsystem.go +++ b/minecraft-skinsystem.go @@ -3,6 +3,7 @@ package main import ( "log" "runtime" + "time" "net/http" "github.com/gorilla/mux" @@ -12,16 +13,17 @@ import ( "elyby/minecraft-skinsystem/lib/services" ) +const redisString string = "redis:6379" + func main() { runtime.GOMAXPROCS(runtime.NumCPU()) - client, redisErr := redis.Dial("tcp", "redis:6379") + client, redisErr := redis.Dial("tcp", redisString) if redisErr != nil { log.Fatal("Redis unavailable") } router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/", routes.NotFound) router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET").Name("skins") router.HandleFunc("/cloaks/{username}", routes.Cape).Methods("GET").Name("cloaks") router.HandleFunc("/textures/{username}", routes.Textures).Methods("GET").Name("textures") @@ -29,6 +31,8 @@ func main() { router.HandleFunc("/minecraft.php", routes.MinecraftPHP).Methods("GET") router.HandleFunc("/skins/", routes.SkinGET).Methods("GET") router.HandleFunc("/cloaks/", routes.CapeGET).Methods("GET") + // 404 + router.NotFoundHandler = http.HandlerFunc(routes.NotFound) // TODO: убрать этого, т.к. он стар router.HandleFunc("/system/setSkin", routes.SetSkin).Methods("POST") @@ -39,5 +43,24 @@ func main() { services.Redis = client services.Router = router + go func() { + for { + time.Sleep(5 * time.Second) + + resp := services.Redis.Cmd("PING") + if (resp.Err != nil) { + log.Println("Redis not pinged. Try to reconnect") + newClient, redisErr := redis.Dial("tcp", redisString) + if (redisErr != nil) { + log.Println("Cannot reconnect to redis") + } else { + services.Redis = newClient + log.Println("Reconnected") + } + } + } + }() + + log.Println("Started"); log.Fatal(http.ListenAndServe(":80", router)) }