diff --git a/documentation/docs/guides/options.md b/documentation/docs/guides/options.md index 0fc5ffae..9ebf505e 100644 --- a/documentation/docs/guides/options.md +++ b/documentation/docs/guides/options.md @@ -11,7 +11,7 @@ import "github.com/go-fuego/fuego" func main() { s := fuego.NewServer( - fuego.WithPort(":8080"), + fuego.WithPort(8080), fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{ DisableSwagger : true, }), @@ -33,7 +33,7 @@ You can change the port of the server with the `WithPort` option. ```go s := fuego.NewServer( - fuego.WithPort(":8080"), + fuego.WithPort(8080), ) ``` diff --git a/examples/full-app-gourmet/main.go b/examples/full-app-gourmet/main.go index e0cb037e..34c9b942 100644 --- a/examples/full-app-gourmet/main.go +++ b/examples/full-app-gourmet/main.go @@ -26,7 +26,7 @@ func main() { } // Flags - port := flag.String("port", ":8083", "port to listen to") + port := flag.Int("port", 8083, "port to listen to") dbPath := flag.String("db", "./recipe.db", "path to database file") debug := flag.Bool("debug", false, "debug mode") flag.Parse() diff --git a/options.go b/options.go index c251fa46..43bf5d40 100644 --- a/options.go +++ b/options.go @@ -8,7 +8,6 @@ import ( "log/slog" "net/http" "os" - "strings" "time" "github.com/getkin/kin-openapi/openapi3" @@ -217,18 +216,11 @@ func WithDisallowUnknownFields(b bool) func(*Server) { return func(c *Server) { c.DisallowUnknownFields = b } } -// WithPort sets the port of the server. For example, "8080". -// -// Deprecated: Use WithAddr instead as that allows to set the host and the port. -func WithPort(port string) func(*Server) { - var addr string - if strings.HasPrefix(port, ":") { - addr = port - } else { - addr = fmt.Sprintf(":%s", port) - } - - return func(c *Server) { c.Server.Addr = addr } +// WithPort sets the port of the server. For example, 8080. +// If not specified, the default port is 9999. +// If you want to use a different address, use [WithAddr] instead. +func WithPort(port int) func(*Server) { + return func(s *Server) { s.Server.Addr = fmt.Sprintf(":%d", port) } } // WithAddr optionally specifies the TCP address for the server to listen on, in the form "host:port". diff --git a/options_test.go b/options_test.go index 837eaa50..48a88b2c 100644 --- a/options_test.go +++ b/options_test.go @@ -259,41 +259,15 @@ func TestWithAddr(t *testing.T) { } func TestWithPort(t *testing.T) { - tests := []struct { - name string - port string - expectedAddr string - }{ - { - name: "port provided with :", - port: ":8888", - expectedAddr: ":8888", - }, - { - name: "port provided without :", - port: "8888", - expectedAddr: ":8888", - }, - { - name: "no port provided", - port: "", - expectedAddr: ":9999", - }, - } - for _, tt := range tests { - t.Run( - tt.name, func(t *testing.T) { - var opts []func(*Server) - - if tt.port != "" { - opts = append(opts, WithPort(tt.port)) - } - - s := NewServer( - opts..., - ) - require.Equal(t, tt.expectedAddr, s.Server.Addr) - }, + t.Run("with custom port, that port is used", func(t *testing.T) { + s := NewServer( + WithPort(8488), ) - } + require.Equal(t, ":8488", s.Server.Addr) + }) + + t.Run("no port provided, default is used (9999)", func(t *testing.T) { + s := NewServer() + require.Equal(t, ":9999", s.Server.Addr) + }) }