2017-06-30 18:40:25 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-08-10 03:14:28 +03:00
|
|
|
"fmt"
|
2017-08-16 15:23:03 +03:00
|
|
|
"log"
|
2017-06-30 18:40:25 +03:00
|
|
|
|
2018-02-16 00:13:57 +03:00
|
|
|
"github.com/elyby/chrly/auth"
|
2018-01-23 18:43:37 +03:00
|
|
|
|
2017-06-30 18:40:25 +03:00
|
|
|
"github.com/spf13/cobra"
|
2017-08-14 21:06:22 +03:00
|
|
|
"github.com/spf13/viper"
|
2017-08-10 03:14:28 +03:00
|
|
|
|
2018-02-16 00:13:57 +03:00
|
|
|
"github.com/elyby/chrly/bootstrap"
|
|
|
|
"github.com/elyby/chrly/db"
|
|
|
|
"github.com/elyby/chrly/http"
|
2017-06-30 18:40:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
2018-02-14 23:49:22 +03:00
|
|
|
Short: "Starts http handler for the skins system",
|
2017-06-30 18:40:25 +03:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2017-08-27 18:10:03 +03:00
|
|
|
logger, err := bootstrap.CreateLogger(viper.GetString("statsd.addr"), viper.GetString("sentry.dsn"))
|
2017-08-16 15:23:03 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(fmt.Printf("Cannot initialize logger: %v", err))
|
|
|
|
}
|
|
|
|
logger.Info("Logger successfully initialized")
|
2017-06-30 18:40:25 +03:00
|
|
|
|
2017-08-14 21:06:22 +03:00
|
|
|
storageFactory := db.StorageFactory{Config: viper.GetViper()}
|
|
|
|
|
|
|
|
logger.Info("Initializing skins repository")
|
|
|
|
skinsRepo, err := storageFactory.CreateFactory("redis").CreateSkinsRepository()
|
2017-06-30 18:40:25 +03:00
|
|
|
if err != nil {
|
2017-08-14 21:06:22 +03:00
|
|
|
logger.Emergency(fmt.Sprintf("Error on creating skins repo: %+v", err))
|
2017-06-30 18:40:25 +03:00
|
|
|
return
|
|
|
|
}
|
2017-08-14 21:06:22 +03:00
|
|
|
logger.Info("Skins repository successfully initialized")
|
2017-06-30 18:40:25 +03:00
|
|
|
|
2017-08-14 21:06:22 +03:00
|
|
|
logger.Info("Initializing capes repository")
|
|
|
|
capesRepo, err := storageFactory.CreateFactory("filesystem").CreateCapesRepository()
|
2017-06-30 18:40:25 +03:00
|
|
|
if err != nil {
|
|
|
|
logger.Emergency(fmt.Sprintf("Error on creating capes repo: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2017-08-14 21:06:22 +03:00
|
|
|
logger.Info("Capes repository successfully initialized")
|
2017-06-30 18:40:25 +03:00
|
|
|
|
2017-08-20 01:22:42 +03:00
|
|
|
cfg := &http.Config{
|
2017-08-14 21:06:22 +03:00
|
|
|
ListenSpec: fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port")),
|
2018-01-23 18:43:37 +03:00
|
|
|
SkinsRepo: skinsRepo,
|
|
|
|
CapesRepo: capesRepo,
|
|
|
|
Logger: logger,
|
2018-02-15 14:20:17 +03:00
|
|
|
Auth: &auth.JwtAuth{Key: []byte(viper.GetString("chrly.secret"))},
|
2017-06-30 18:40:25 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 01:22:42 +03:00
|
|
|
if err := cfg.Run(); err != nil {
|
2017-06-30 18:40:25 +03:00
|
|
|
logger.Error(fmt.Sprintf("Error in main(): %v", err))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(serveCmd)
|
2018-02-15 14:20:17 +03:00
|
|
|
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 18:40:25 +03:00
|
|
|
}
|