2020-01-03 03:21:57 +05:30
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-04-04 21:44:12 +05:30
|
|
|
"os"
|
2020-01-03 03:21:57 +05:30
|
|
|
|
|
|
|
"github.com/mono83/slf/wd"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/bootstrap"
|
2020-04-04 21:44:12 +05:30
|
|
|
"github.com/elyby/chrly/eventsubscribers"
|
2020-01-03 03:21:57 +05:30
|
|
|
"github.com/elyby/chrly/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
var workerCmd = &cobra.Command{
|
|
|
|
Use: "worker",
|
|
|
|
Short: "Starts HTTP handler for the Mojang usernames to UUIDs worker",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-04-04 21:44:12 +05:30
|
|
|
dispatcher := bootstrap.CreateEventDispatcher()
|
|
|
|
|
|
|
|
// TODO: need to find a way to unify this initialization with the serve command
|
|
|
|
logger, err := bootstrap.CreateLogger(viper.GetString("sentry.dsn"))
|
2020-01-03 03:21:57 +05:30
|
|
|
if err != nil {
|
2020-04-04 21:44:12 +05:30
|
|
|
log.Fatalf("Cannot initialize logger: %v", err)
|
2020-01-03 03:21:57 +05:30
|
|
|
}
|
|
|
|
logger.Info("Logger successfully initialized")
|
|
|
|
|
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
|
|
|
|
|
|
|
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(dispatcher)
|
2020-01-03 03:21:57 +05:30
|
|
|
if err != nil {
|
|
|
|
logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err))
|
2020-04-04 21:44:12 +05:30
|
|
|
os.Exit(1)
|
2020-01-03 03:21:57 +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.UUIDsWorker{
|
2020-02-08 17:01:47 +05:30
|
|
|
Emitter: dispatcher,
|
2020-01-03 03:21:57 +05:30
|
|
|
UUIDsProvider: uuidsProvider,
|
2020-01-29 04:04:15 +05:30
|
|
|
}).CreateHandler()
|
2020-01-03 03:21:57 +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 worker, 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.Error("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: :code, exiting.", wd.StringParam("code", s.String()))
|
2020-01-05 23:09:17 +05:30
|
|
|
finishChan <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-finishChan
|
2020-01-03 03:21:57 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(workerCmd)
|
|
|
|
}
|