From 87e06ffc673f8e25f26e229de2da7f84b4723dc6 Mon Sep 17 00:00:00 2001 From: legiz-ru Date: Sun, 6 Apr 2025 07:03:37 +0300 Subject: [PATCH] add bind option (#17) * add bind option --- api/server.go | 7 ++++--- cmd/server.go | 8 ++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/api/server.go b/api/server.go index 9580fd7..59681a8 100755 --- a/api/server.go +++ b/api/server.go @@ -15,7 +15,7 @@ import ( //go:embed static var staticFiles embed.FS -func RunServer(port uint16) { +func RunServer(bind string, port uint16) { tpl, err := template.ParseFS(staticFiles, "static/*") if err != nil { println(err.Error()) @@ -41,8 +41,9 @@ func RunServer(port uint16) { r.GET("/convert", handler.Convert) - fmt.Println("Server is running on port", port) - err = r.Run(":" + strconv.Itoa(int(port))) + address := bind + ":" + strconv.Itoa(int(port)) + fmt.Println("Server is running on", address) + err = r.Run(address) if err != nil { fmt.Println("Run server failed: ", err) } diff --git a/cmd/server.go b/cmd/server.go index 7c79770..0275292 100755 --- a/cmd/server.go +++ b/cmd/server.go @@ -6,10 +6,14 @@ import ( "github.com/spf13/cobra" ) -var port uint16 +var ( + port uint16 + bind string +) func init() { runCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "server port") + runCmd.Flags().StringVarP(&bind, "bind", "b", "0.0.0.0", "bind address (e.g., 0.0.0.0, 127.0.0.1, localhost)") RootCmd.AddCommand(runCmd) } @@ -18,6 +22,6 @@ var runCmd = &cobra.Command{ Long: "Run the server", Short: "Run the server", Run: func(cmd *cobra.Command, args []string) { - api.RunServer(port) + api.RunServer(bind, port) }, }