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"
|
2020-04-04 21:44:12 +05:30
|
|
|
"os"
|
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
|
|
|
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/bootstrap"
|
|
|
|
"github.com/elyby/chrly/db"
|
2020-04-04 21:44:12 +05:30
|
|
|
"github.com/elyby/chrly/eventsubscribers"
|
2018-02-16 02:43:57 +05:30
|
|
|
"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) {
|
2020-04-04 21:44:12 +05:30
|
|
|
dispatcher := bootstrap.CreateEventDispatcher()
|
|
|
|
|
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
|
2020-04-04 21:44:12 +05:30
|
|
|
logger, err := bootstrap.CreateLogger(viper.GetString("sentry.dsn"))
|
2017-08-16 17:53:03 +05:30
|
|
|
if err != nil {
|
2020-04-04 21:44:12 +05:30
|
|
|
log.Fatalf("Cannot initialize logger: %v", err)
|
2017-08-16 17:53:03 +05:30
|
|
|
}
|
|
|
|
logger.Info("Logger successfully initialized")
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2020-04-04 21:44:12 +05:30
|
|
|
(&eventsubscribers.Logger{Logger: logger}).ConfigureWithDispatcher(dispatcher)
|
|
|
|
|
|
|
|
statsdAddr := viper.GetString("statsd.addr")
|
|
|
|
if statsdAddr != "" {
|
|
|
|
statsdReporter, err := bootstrap.CreateStatsReceiver(statsdAddr)
|
|
|
|
if err != nil {
|
|
|
|
logger.Emergency("Invalid statsd configuration :err", wd.ErrParam(err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
(&eventsubscribers.StatsReporter{StatsReporter: statsdReporter}).ConfigureWithDispatcher(dispatcher)
|
|
|
|
}
|
2020-02-08 17:01:47 +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 {
|
2020-04-04 21:44:12 +05:30
|
|
|
logger.Emergency("Error on creating skins repo: :err", wd.ErrParam(err))
|
|
|
|
os.Exit(1)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
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 {
|
2020-04-04 21:44:12 +05:30
|
|
|
logger.Emergency("Error on creating capes repo: :err", wd.ErrParam(err))
|
|
|
|
os.Exit(1)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
2017-08-14 23:36:22 +05:30
|
|
|
logger.Info("Capes repository successfully initialized")
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2020-04-06 19:45:40 +05:30
|
|
|
var mojangTexturesProvider http.MojangTexturesProvider
|
|
|
|
if viper.GetBool("mojang_textures.enabled") {
|
|
|
|
logger.Info("Preparing Mojang's textures queue")
|
|
|
|
mojangUuidsRepository, err := redisFactory.CreateMojangUuidsRepository()
|
|
|
|
if err != nil {
|
|
|
|
logger.Emergency("Error on creating mojang uuids repo: :err", wd.ErrParam(err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-04-27 04:16:15 +05:30
|
|
|
|
2020-04-06 19:45:40 +05:30
|
|
|
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(dispatcher)
|
|
|
|
if err != nil {
|
|
|
|
logger.Emergency("Unable to create mojang uuids provider: :err", wd.ErrParam(err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-11-24 06:37:56 +05:30
|
|
|
|
2020-04-06 19:45:40 +05:30
|
|
|
texturesStorage := mojangtextures.NewInMemoryTexturesStorage()
|
|
|
|
texturesStorage.Start()
|
|
|
|
mojangTexturesProvider = &mojangtextures.Provider{
|
|
|
|
Emitter: dispatcher,
|
|
|
|
UUIDsProvider: uuidsProvider,
|
|
|
|
TexturesProvider: &mojangtextures.MojangApiTexturesProvider{
|
|
|
|
Emitter: dispatcher,
|
|
|
|
},
|
|
|
|
Storage: &mojangtextures.SeparatedStorage{
|
|
|
|
UuidsStorage: mojangUuidsRepository,
|
|
|
|
TexturesStorage: texturesStorage,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
logger.Info("Mojang's textures queue is successfully initialized")
|
|
|
|
} else {
|
|
|
|
logger.Info("Mojang's textures queue is disabled")
|
|
|
|
mojangTexturesProvider = &mojangtextures.NilProvider{}
|
2019-04-27 04:16:15 +05:30
|
|
|
}
|
|
|
|
|
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-04-07 02:43:04 +05:30
|
|
|
Emitter: dispatcher,
|
|
|
|
SkinsRepo: skinsRepo,
|
|
|
|
CapesRepo: capesRepo,
|
|
|
|
MojangTexturesProvider: mojangTexturesProvider,
|
|
|
|
Authenticator: &http.JwtAuth{
|
|
|
|
Key: []byte(viper.GetString("chrly.secret")),
|
|
|
|
Emitter: dispatcher,
|
|
|
|
},
|
2020-01-06 02:46:38 +05:30
|
|
|
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-04-04 21:44:12 +05:30
|
|
|
logger.Info("Starting the app, HTTP on: :addr", wd.StringParam("addr", address))
|
2020-01-29 04:04:15 +05:30
|
|
|
if err := http.Serve(address, handler); err != nil {
|
2020-04-04 21:44:12 +05:30
|
|
|
logger.Emergency("Error in main(): :err", wd.ErrParam(err))
|
2020-01-05 23:09:17 +05:30
|
|
|
finishChan <- true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
s := waitForExitSignal()
|
2020-04-04 21:44:12 +05:30
|
|
|
logger.Info("Got signal: :signal, exiting", wd.StringParam("signal", s.String()))
|
2020-01-05 23:09:17 +05:30
|
|
|
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")
|
2020-04-06 19:45:40 +05:30
|
|
|
viper.SetDefault("mojang_textures.enabled", true)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|