2016-07-05 03:58:09 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-07-07 03:13:42 +05:30
|
|
|
"runtime"
|
2016-09-16 22:05:58 +05:30
|
|
|
"time"
|
2016-07-05 03:58:09 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2016-09-16 22:05:58 +05:30
|
|
|
"github.com/streadway/amqp"
|
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-09-15 03:52:57 +05:30
|
|
|
"elyby/minecraft-skinsystem/lib/worker"
|
2016-09-22 00:14:52 +05:30
|
|
|
"path/filepath"
|
2016-07-05 03:58:09 +05:30
|
|
|
)
|
|
|
|
|
2016-07-29 14:44:32 +05:30
|
|
|
const redisString string = "redis:6379"
|
2016-09-16 22:05:58 +05:30
|
|
|
const redisPoolSize int = 10
|
|
|
|
|
2016-09-15 03:52:57 +05:30
|
|
|
const rabbitmqString string = "amqp://ely-skinsystem-app:ely-skinsystem-app-password@rabbitmq:5672/%2fely"
|
2016-07-29 14:44:32 +05:30
|
|
|
|
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")
|
2016-09-16 22:05:58 +05:30
|
|
|
redisPool, redisErr := pool.New("tcp", redisString, redisPoolSize)
|
|
|
|
if (redisErr != nil) {
|
2016-07-05 03:58:09 +05:30
|
|
|
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
|
|
|
|
2016-09-15 03:52:57 +05:30
|
|
|
log.Println("Connecting to rabbitmq")
|
|
|
|
// TODO: rabbitmq становится доступен не сразу. Нужно дождаться, пока он станет доступен, периодически повторяя запросы
|
|
|
|
rabbitConnection, rabbitmqErr := amqp.Dial(rabbitmqString)
|
2016-09-16 22:05:58 +05:30
|
|
|
if (rabbitmqErr != nil) {
|
2016-09-15 03:52:57 +05:30
|
|
|
log.Fatalf("%s", rabbitmqErr)
|
|
|
|
}
|
|
|
|
log.Println("Connected to rabbitmq. Trying to open a channel")
|
|
|
|
rabbitChannel, rabbitmqErr := rabbitConnection.Channel()
|
2016-09-16 22:05:58 +05:30
|
|
|
if (rabbitmqErr != nil) {
|
2016-09-15 03:52:57 +05:30
|
|
|
log.Fatalf("%s", rabbitmqErr)
|
|
|
|
}
|
|
|
|
log.Println("Connected to rabbitmq channel")
|
|
|
|
|
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-09-21 23:22:28 +05:30
|
|
|
router.HandleFunc("/skins/{username}/face", routes.Face).Methods("GET").Name("faces")
|
|
|
|
router.HandleFunc("/skins/{username}/face.png", routes.Face).Methods("GET").Name("faces")
|
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-09-15 03:52:57 +05:30
|
|
|
services.RabbitMQChannel = rabbitChannel
|
2016-07-07 03:13:42 +05:30
|
|
|
|
2016-09-22 00:14:52 +05:30
|
|
|
_, file, _, _ := runtime.Caller(0)
|
|
|
|
services.RootFolder = filepath.Dir(file)
|
|
|
|
|
2016-09-16 22:05:58 +05:30
|
|
|
go func() {
|
|
|
|
period := 5
|
2016-07-29 14:44:32 +05:30
|
|
|
for {
|
2016-09-16 22:05:58 +05:30
|
|
|
time.Sleep(time.Duration(period) * time.Second)
|
|
|
|
|
|
|
|
resp := services.RedisPool.Cmd("PING")
|
|
|
|
if (resp.Err == nil) {
|
|
|
|
// Если редис успешно пинганулся, значит всё хорошо
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Redis not pinged. Try to reconnect")
|
|
|
|
newPool, redisErr := pool.New("tcp", redisString, redisPoolSize)
|
|
|
|
if (redisErr != nil) {
|
|
|
|
log.Printf("Cannot reconnect to redis, waiting %d seconds\n", period)
|
|
|
|
} else {
|
|
|
|
services.RedisPool = newPool
|
|
|
|
log.Println("Reconnected")
|
2016-07-29 14:44:32 +05:30
|
|
|
}
|
|
|
|
}
|
2016-09-16 22:05:58 +05:30
|
|
|
}()
|
2016-07-29 14:44:32 +05:30
|
|
|
|
2016-09-15 03:52:57 +05:30
|
|
|
go worker.Listen()
|
|
|
|
|
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))
|
|
|
|
}
|