blob: 088e7ff5011a080a63f354067c91507350a6561e [file] [log] [blame]
package commands
import (
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/iomodo/staff/server"
"github.com/spf13/cobra"
)
// Command is an abstraction of the cobra Command
type Command = cobra.Command
// Run function starts the application
func Run(args []string) error {
rootCmd.SetArgs(args)
return rootCmd.Execute()
}
// rootCmd is a command to run the server.
var rootCmd = &cobra.Command{
Use: "server",
Short: "Runs a server",
Long: `Runs a server. Killing the process will stop the server`,
RunE: serverCmdF,
}
func serverCmdF(_ *cobra.Command, _ []string) error {
srv, err := runServer()
if err != nil {
return err
}
defer srv.Shutdown()
// wait for kill signal before attempting to gracefully shutdown
// the running service
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-interruptChan
return nil
}
func runServer() (*server.Server, error) {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
srv, err := server.NewServer(logger)
if err != nil {
logger.Error(err.Error())
return nil, err
}
serverErr := srv.Start()
if serverErr != nil {
logger.Error(serverErr.Error())
return nil, serverErr
}
return srv, nil
}