mirror of
https://github.com/elyby/chrly.git
synced 2024-12-22 21:19:55 +05:30
Реализована страница 404 ошибки
Реализовано переподключение к Redis в случае, если соединение упадёт
This commit is contained in:
parent
283f4e0e3f
commit
c2d0cb93cb
@ -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
|
||||
|
@ -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")
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user