mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 03:27:02 -07:00
Added server implementation and serve command
This commit is contained in:
parent
112db14dd4
commit
3905a8a023
4 changed files with 158 additions and 11 deletions
37
cmd/serve.go
Normal file
37
cmd/serve.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
//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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue