42 lines
904 B
Go
42 lines
904 B
Go
package cmd
|
||
|
||
import (
|
||
"fmt"
|
||
"log"
|
||
"github.com/spf13/cobra"
|
||
"remote-task-excutor-cli/pkg/router"
|
||
)
|
||
|
||
var serverCmd = &cobra.Command{
|
||
Use: "server",
|
||
Short: "Start a HTTP server for remote task execution",
|
||
Long: `Start a HTTP server that can receive requests for executing remote tasks`,
|
||
RunE: func(cmd *cobra.Command, args []string) error {
|
||
// 创建路由
|
||
r := router.SetupRouter()
|
||
|
||
// 配置服务器地址(0.0.0.0表示监听所有网络接口)
|
||
addr := fmt.Sprintf("0.0.0.0:%d", port)
|
||
fmt.Printf("Starting HTTP server on %s\n", addr)
|
||
|
||
// 启动服务器
|
||
if err := r.Run(addr); err != nil {
|
||
log.Fatalf("Server failed to start: %v", err)
|
||
}
|
||
return nil
|
||
},
|
||
}
|
||
|
||
|
||
|
||
// 定义命令行参数变量
|
||
var (
|
||
port int
|
||
)
|
||
|
||
func init() {
|
||
rootCmd.AddCommand(serverCmd)
|
||
|
||
// 配置命令行参数
|
||
serverCmd.Flags().IntVarP(&port, "port", "p", 8080, "Port to run the server on")
|
||
} |