hako/internal/cli/cli.go

55 lines
1.2 KiB
Go

package cli
import (
"log/slog"
"os"
"git.nakama.town/fmartingr/hako/internal/config"
"git.nakama.town/fmartingr/hako/internal/server"
"github.com/spf13/cobra"
)
var configPath string
var rootCmd = &cobra.Command{
Use: "hako",
Short: "Hako service",
Long: `Hako is a service with HTTP and WebSocket capabilities.`,
}
var serverCmd = &cobra.Command{
Use: "server",
Short: "Start the HTTP/WebSocket server",
Run: func(cmd *cobra.Command, args []string) {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
// Load configuration
cfg, err := config.Load(configPath)
if err != nil {
logger.Error("Failed to load configuration", "error", err)
os.Exit(1)
}
// Create server with configuration
srv, err := server.NewServer(cfg, logger)
if err != nil {
logger.Error("Failed to create server", "error", err)
os.Exit(1)
}
if err := srv.Start(cmd.Context()); err != nil {
logger.Error("Failed to start server", "error", err)
os.Exit(1)
}
},
}
func init() {
serverCmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to configuration file (YAML, JSON, or TOML)")
}
// Execute executes the root command
func Execute() error {
rootCmd.AddCommand(serverCmd)
return rootCmd.Execute()
}