configurator/cmd/serve.go
2024-04-11 10:19:29 -06:00

37 lines
900 B
Go

//go:build server || all
// +build server all
package cmd
import (
"errors"
"fmt"
"net/http"
"os"
"github.com/OpenCHAMI/configurator/internal/server"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start configurator as a server and listen for requests",
Run: func(cmd *cobra.Command, args []string) {
// set up the routes and start the server
server := server.New()
err := server.Start(&config)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("Server closed.")
} else if err != nil {
logrus.Errorf("failed to start server: %v", err)
os.Exit(1)
}
},
}
func init() {
serveCmd.Flags().StringVar(&config.Server.Host, "host", config.Server.Host, "set the server host")
serveCmd.Flags().IntVar(&config.Server.Port, "port", config.Server.Port, "set the server port")
rootCmd.AddCommand(serveCmd)
}