chrly/internal/cmd/serve.go

64 lines
1.2 KiB
Go
Raw Normal View History

package cmd
import (
2024-02-14 05:26:48 +05:30
"context"
"log/slog"
"github.com/spf13/cobra"
2024-02-14 05:26:48 +05:30
"github.com/spf13/viper"
"ely.by/chrly/internal/di"
2024-02-14 05:26:48 +05:30
"ely.by/chrly/internal/http"
"ely.by/chrly/internal/otel"
)
var serveCmd = &cobra.Command{
Use: "serve",
2020-01-03 03:21:57 +05:30
Short: "Starts HTTP handler for the skins system",
2024-02-07 18:59:52 +05:30
RunE: func(cmd *cobra.Command, args []string) error {
return startServer(di.ModuleSkinsystem, di.ModuleProfiles, di.ModuleSigner)
},
}
func init() {
RootCmd.AddCommand(serveCmd)
}
2024-02-14 05:26:48 +05:30
func startServer(modules ...string) error {
container := shouldGetContainer()
var globalCtx context.Context
err := container.Resolve(&globalCtx)
if err != nil {
return err
}
var config *viper.Viper
err = container.Resolve(&config)
if err != nil {
return err
}
if !config.GetBool("otel.sdk.disabled") {
shutdownOtel, err := otel.SetupOTelSDK(globalCtx)
defer func() {
err := shutdownOtel(context.Background())
if err != nil {
slog.Error("Unable to shutdown OpenTelemetry", slog.Any("error", err))
}
}()
if err != nil {
return err
}
}
2024-02-14 05:26:48 +05:30
config.Set("modules", modules)
err = container.Invoke(http.StartServer)
if err != nil {
return err
}
return nil
}