2016-07-05 03:58:09 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-07-07 03:13:42 +05:30
|
|
|
"runtime"
|
2016-07-29 14:54:31 +05:30
|
|
|
//"time"
|
2016-07-05 03:58:09 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2016-07-29 14:54:31 +05:30
|
|
|
"github.com/mediocregopher/radix.v2/pool"
|
2016-07-05 03:58:09 +05:30
|
|
|
|
|
|
|
"elyby/minecraft-skinsystem/lib/routes"
|
|
|
|
"elyby/minecraft-skinsystem/lib/services"
|
2016-07-29 14:54:31 +05:30
|
|
|
//"github.com/mediocregopher/radix.v2/redis"
|
2016-07-05 03:58:09 +05:30
|
|
|
)
|
|
|
|
|
2016-07-29 14:44:32 +05:30
|
|
|
const redisString string = "redis:6379"
|
|
|
|
|
2016-07-05 03:58:09 +05:30
|
|
|
func main() {
|
2016-08-27 02:15:55 +05:30
|
|
|
log.Println("Starting...")
|
|
|
|
|
2016-07-07 03:13:42 +05:30
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
2016-08-27 02:15:55 +05:30
|
|
|
log.Println("Connecting to redis")
|
|
|
|
redisPool, redisErr := pool.New("tcp", redisString, 10)
|
2016-07-05 03:58:09 +05:30
|
|
|
if redisErr != nil {
|
|
|
|
log.Fatal("Redis unavailable")
|
|
|
|
}
|
2016-08-27 02:15:55 +05:30
|
|
|
log.Println("Connected to redis")
|
2016-07-05 03:58:09 +05:30
|
|
|
|
|
|
|
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-29 14:44:32 +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-08-27 02:15:55 +05:30
|
|
|
services.RedisPool = redisPool
|
2016-07-07 03:13:42 +05:30
|
|
|
services.Router = router
|
|
|
|
|
2016-07-29 14:54:31 +05:30
|
|
|
/*go func() {
|
2016-07-29 14:44:32 +05:30
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 14:54:31 +05:30
|
|
|
}()*/
|
2016-07-29 14:44:32 +05:30
|
|
|
|
|
|
|
log.Println("Started");
|
2016-07-05 03:58:09 +05:30
|
|
|
log.Fatal(http.ListenAndServe(":80", router))
|
|
|
|
}
|