2017-06-30 21:10:25 +05:30
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-08-10 05:44:28 +05:30
|
|
|
"fmt"
|
2017-08-16 17:53:03 +05:30
|
|
|
"log"
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2019-11-24 06:37:56 +05:30
|
|
|
"github.com/mono83/slf/wd"
|
2017-06-30 21:10:25 +05:30
|
|
|
"github.com/spf13/cobra"
|
2017-08-14 23:36:22 +05:30
|
|
|
"github.com/spf13/viper"
|
2017-08-10 05:44:28 +05:30
|
|
|
|
2019-04-27 04:16:15 +05:30
|
|
|
"github.com/elyby/chrly/auth"
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/bootstrap"
|
|
|
|
"github.com/elyby/chrly/db"
|
|
|
|
"github.com/elyby/chrly/http"
|
2019-11-21 04:03:13 +05:30
|
|
|
"github.com/elyby/chrly/mojangtextures"
|
2017-06-30 21:10:25 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
2020-01-03 03:21:57 +05:30
|
|
|
Short: "Starts HTTP handler for the skins system",
|
2017-06-30 21:10:25 +05:30
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-24 06:37:56 +05:30
|
|
|
// TODO: this is a mess, need to organize this code somehow to make services initialization more compact
|
2017-08-27 20:40:03 +05:30
|
|
|
logger, err := bootstrap.CreateLogger(viper.GetString("statsd.addr"), viper.GetString("sentry.dsn"))
|
2017-08-16 17:53:03 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal(fmt.Printf("Cannot initialize logger: %v", err))
|
|
|
|
}
|
|
|
|
logger.Info("Logger successfully initialized")
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-14 23:36:22 +05:30
|
|
|
storageFactory := db.StorageFactory{Config: viper.GetViper()}
|
|
|
|
|
|
|
|
logger.Info("Initializing skins repository")
|
2019-04-27 04:16:15 +05:30
|
|
|
redisFactory := storageFactory.CreateFactory("redis")
|
|
|
|
skinsRepo, err := redisFactory.CreateSkinsRepository()
|
2017-06-30 21:10:25 +05:30
|
|
|
if err != nil {
|
2017-08-14 23:36:22 +05:30
|
|
|
logger.Emergency(fmt.Sprintf("Error on creating skins repo: %+v", err))
|
2017-06-30 21:10:25 +05:30
|
|
|
return
|
|
|
|
}
|
2017-08-14 23:36:22 +05:30
|
|
|
logger.Info("Skins repository successfully initialized")
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-14 23:36:22 +05:30
|
|
|
logger.Info("Initializing capes repository")
|
2019-04-27 04:16:15 +05:30
|
|
|
filesystemFactory := storageFactory.CreateFactory("filesystem")
|
|
|
|
capesRepo, err := filesystemFactory.CreateCapesRepository()
|
2017-06-30 21:10:25 +05:30
|
|
|
if err != nil {
|
|
|
|
logger.Emergency(fmt.Sprintf("Error on creating capes repo: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2017-08-14 23:36:22 +05:30
|
|
|
logger.Info("Capes repository successfully initialized")
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2019-04-27 04:16:15 +05:30
|
|
|
logger.Info("Preparing Mojang's textures queue")
|
|
|
|
mojangUuidsRepository, err := redisFactory.CreateMojangUuidsRepository()
|
|
|
|
if err != nil {
|
|
|
|
logger.Emergency(fmt.Sprintf("Error on creating mojang uuids repo: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:58:10 +05:30
|
|
|
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(nil)
|
2020-01-03 03:21:57 +05:30
|
|
|
if err != nil {
|
|
|
|
logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err))
|
|
|
|
return
|
2019-11-24 06:37:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
texturesStorage := mojangtextures.NewInMemoryTexturesStorage()
|
|
|
|
texturesStorage.Start()
|
|
|
|
mojangTexturesProvider := &mojangtextures.Provider{
|
2020-02-08 15:58:10 +05:30
|
|
|
// TODO: configure emitter
|
2020-01-03 03:21:57 +05:30
|
|
|
UUIDsProvider: uuidsProvider,
|
2019-11-21 04:03:13 +05:30
|
|
|
TexturesProvider: &mojangtextures.MojangApiTexturesProvider{
|
2020-02-08 15:58:10 +05:30
|
|
|
// TODO: configure emitter
|
2019-11-21 04:03:13 +05:30
|
|
|
},
|
|
|
|
Storage: &mojangtextures.SeparatedStorage{
|
2019-04-27 04:16:15 +05:30
|
|
|
UuidsStorage: mojangUuidsRepository,
|
2019-06-19 03:26:09 +05:30
|
|
|
TexturesStorage: texturesStorage,
|
2019-04-27 04:16:15 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
logger.Info("Mojang's textures queue is successfully initialized")
|
|
|
|
|
2020-01-29 04:04:15 +05:30
|
|
|
address := fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port"))
|
|
|
|
handler := (&http.Skinsystem{
|
2020-01-06 02:46:38 +05:30
|
|
|
SkinsRepo: skinsRepo,
|
|
|
|
CapesRepo: capesRepo,
|
|
|
|
MojangTexturesProvider: mojangTexturesProvider,
|
|
|
|
Auth: &auth.JwtAuth{Key: []byte(viper.GetString("chrly.secret"))},
|
|
|
|
TexturesExtraParamName: viper.GetString("textures.extra_param_name"),
|
|
|
|
TexturesExtraParamValue: viper.GetString("textures.extra_param_value"),
|
2020-01-29 04:04:15 +05:30
|
|
|
}).CreateHandler()
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2020-01-05 23:09:17 +05:30
|
|
|
finishChan := make(chan bool)
|
|
|
|
go func() {
|
2020-01-29 04:04:15 +05:30
|
|
|
logger.Info(fmt.Sprintf("Starting the app, HTTP on: %s", address))
|
|
|
|
if err := http.Serve(address, handler); err != nil {
|
2020-01-05 23:09:17 +05:30
|
|
|
logger.Error(fmt.Sprintf("Error in main(): %v", err))
|
|
|
|
finishChan <- true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
s := waitForExitSignal()
|
|
|
|
logger.Info(fmt.Sprintf("Got signal: %v, exiting.", s))
|
|
|
|
finishChan <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-finishChan
|
2017-06-30 21:10:25 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(serveCmd)
|
2018-02-15 16:50:17 +05:30
|
|
|
viper.SetDefault("server.host", "")
|
|
|
|
viper.SetDefault("server.port", 80)
|
|
|
|
viper.SetDefault("storage.redis.host", "localhost")
|
|
|
|
viper.SetDefault("storage.redis.port", 6379)
|
|
|
|
viper.SetDefault("storage.redis.poll", 10)
|
|
|
|
viper.SetDefault("storage.filesystem.basePath", "data")
|
|
|
|
viper.SetDefault("storage.filesystem.capesDirName", "capes")
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|