2016-07-05 03:58:09 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-07-07 03:13:42 +05:30
|
|
|
"runtime"
|
2016-07-28 20:47:12 +05:30
|
|
|
"time"
|
2016-07-05 03:58:09 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2016-07-08 02:11:21 +05:30
|
|
|
"github.com/mediocregopher/radix.v2/redis"
|
2016-07-05 03:58:09 +05:30
|
|
|
|
|
|
|
"elyby/minecraft-skinsystem/lib/routes"
|
|
|
|
"elyby/minecraft-skinsystem/lib/services"
|
|
|
|
)
|
|
|
|
|
2016-07-28 20:47:12 +05:30
|
|
|
const redisString string = "redis:6379"
|
|
|
|
|
2016-07-05 03:58:09 +05:30
|
|
|
func main() {
|
2016-07-07 03:13:42 +05:30
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
2016-07-28 20:47:12 +05:30
|
|
|
client, redisErr := redis.Dial("tcp", redisString)
|
2016-07-05 03:58:09 +05:30
|
|
|
if redisErr != nil {
|
|
|
|
log.Fatal("Redis unavailable")
|
|
|
|
}
|
|
|
|
|
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
2016-07-07 03:13:42 +05:30
|
|
|
router.HandleFunc("/skins/{username}", routes.Skin).Methods("GET").Name("skins")
|
2016-07-07 15:40:39 +05:30
|
|
|
router.HandleFunc("/cloaks/{username}", routes.Cape).Methods("GET").Name("cloaks")
|
2016-07-07 03:13:42 +05:30
|
|
|
router.HandleFunc("/textures/{username}", routes.Textures).Methods("GET").Name("textures")
|
2016-07-07 15:40:39 +05:30
|
|
|
// Legacy
|
|
|
|
router.HandleFunc("/minecraft.php", routes.MinecraftPHP).Methods("GET")
|
|
|
|
router.HandleFunc("/skins/", routes.SkinGET).Methods("GET")
|
|
|
|
router.HandleFunc("/cloaks/", routes.CapeGET).Methods("GET")
|
2016-07-28 20:47:12 +05:30
|
|
|
// 404
|
|
|
|
router.NotFoundHandler = http.HandlerFunc(routes.NotFound)
|
2016-07-07 15:40:39 +05:30
|
|
|
|
2016-07-06 03:55:05 +05:30
|
|
|
apiRouter := router.PathPrefix("/api").Subrouter()
|
|
|
|
apiRouter.HandleFunc("/user/{username}/skin", routes.SetSkin).Methods("POST")
|
2016-07-05 03:58:09 +05:30
|
|
|
|
2016-07-08 02:11:21 +05:30
|
|
|
services.Redis = client
|
2016-07-07 03:13:42 +05:30
|
|
|
services.Router = router
|
|
|
|
|
2016-07-28 20:47:12 +05:30
|
|
|
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");
|
2016-07-05 03:58:09 +05:30
|
|
|
log.Fatal(http.ListenAndServe(":80", router))
|
|
|
|
}
|