42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"git.oblat.lv/alex/triggerssmith/internal/config"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var opts = struct {
|
|
ConfigPath *string
|
|
Debug *bool
|
|
}{}
|
|
|
|
|
|
var serveCmd = &cobra.Command{
|
|
Use: "serve",
|
|
Short: "Start the server",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if *opts.Debug {
|
|
slog.SetDefault(slog.New(slog.NewTextHandler(cmd.OutOrStdout(), &slog.HandlerOptions{Level: slog.LevelDebug})))
|
|
} else {
|
|
slog.SetDefault(slog.New(slog.NewTextHandler(cmd.OutOrStdout(), &slog.HandlerOptions{Level: slog.LevelInfo})))
|
|
}
|
|
slog.Debug("Starting server")
|
|
slog.Debug("Reading configuration", slog.String("path", *opts.ConfigPath))
|
|
config, err := config.LoadConfig(*opts.ConfigPath)
|
|
if err != nil {
|
|
slog.Error("Failed to load configuration", slog.String("path", *opts.ConfigPath), slog.String("error", err.Error()))
|
|
return
|
|
}
|
|
slog.Debug("Configuration loaded", slog.Any("config", config))
|
|
slog.Info("Server started", slog.Int("port", config.Server.Port), slog.String("address", config.Server.Addr))
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
opts.Debug = serveCmd.Flags().BoolP("debug", "d", false, "Enable debug logs")
|
|
opts.ConfigPath = serveCmd.Flags().StringP("config", "c", "config.yaml", "Path to configuration file")
|
|
rootCmd.AddCommand(serveCmd)
|
|
}
|