2016-07-05 03:58:09 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-12 23:13:10 +05:30
|
|
|
"os"
|
2016-07-05 03:58:09 +05:30
|
|
|
"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"
|
2016-10-11 20:31:33 +05:30
|
|
|
"path/filepath"
|
2016-07-05 03:58:09 +05:30
|
|
|
|
|
|
|
"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-12-03 04:27:55 +05:30
|
|
|
"github.com/mono83/slf/wd"
|
|
|
|
"github.com/mono83/slf/rays"
|
|
|
|
"github.com/mono83/slf/recievers/ansi"
|
|
|
|
"github.com/mono83/slf/recievers/statsd"
|
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-07-05 03:58:09 +05:30
|
|
|
)
|
|
|
|
|
2016-09-16 22:05:58 +05:30
|
|
|
const redisPoolSize int = 10
|
|
|
|
|
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-10-12 23:13:10 +05:30
|
|
|
|
|
|
|
var redisString = os.Getenv("REDIS_ADDR")
|
|
|
|
if (redisString == "") {
|
|
|
|
redisString = "redis:6379"
|
|
|
|
}
|
|
|
|
|
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 становится доступен не сразу. Нужно дождаться, пока он станет доступен, периодически повторяя запросы
|
2016-10-12 23:13:10 +05:30
|
|
|
|
|
|
|
var rabbitmqString = os.Getenv("RABBITMQ_ADDR")
|
|
|
|
if (rabbitmqString == "") {
|
|
|
|
rabbitmqString = "amqp://ely-skinsystem-app:ely-skinsystem-app-password@rabbitmq:5672/%2fely"
|
|
|
|
}
|
|
|
|
|
2016-09-15 03:52:57 +05:30
|
|
|
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-12-03 04:27:55 +05:30
|
|
|
// statsd
|
|
|
|
var statsdString = os.Getenv("STATSD_ADDR")
|
|
|
|
if (statsdString != "") {
|
2017-03-25 18:25:57 +05:30
|
|
|
log.Println("Connecting to statsd")
|
2016-12-03 04:27:55 +05:30
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
statsdReceiver, err := statsd.NewReceiver(statsd.Config{
|
|
|
|
Address: statsdString,
|
2017-03-25 18:25:57 +05:30
|
|
|
Prefix: "ely.skinsystem." + hostname + ".app.",
|
|
|
|
FlushEvery: 1,
|
2016-12-03 04:27:55 +05:30
|
|
|
})
|
|
|
|
if (err != nil) {
|
|
|
|
log.Fatal("statsd connection error")
|
|
|
|
}
|
|
|
|
|
|
|
|
wd.AddReceiver(statsdReceiver)
|
|
|
|
} else {
|
|
|
|
wd.AddReceiver(ansi.New(true, true, false))
|
2016-08-27 00:38:53 +05:30
|
|
|
}
|
|
|
|
|
2016-12-03 04:27:55 +05:30
|
|
|
logger := wd.New("", "").WithParams(rays.Host)
|
2016-08-27 00:38:53 +05:30
|
|
|
|
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")
|
2017-04-01 14:48:57 +05:30
|
|
|
router.HandleFunc("/textures/signed/{username}", routes.SignedTextures).Methods("GET").Name("signedTextures")
|
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-09-22 22:02:00 +05:30
|
|
|
services.Router = router
|
2016-08-27 02:15:55 +05:30
|
|
|
services.RedisPool = redisPool
|
2016-09-15 03:52:57 +05:30
|
|
|
services.RabbitMQChannel = rabbitChannel
|
2016-12-03 04:27:55 +05:30
|
|
|
services.Logger = logger
|
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))
|
|
|
|
}
|