Skip to content

Commit

Permalink
Provide port option as int, not string
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Mar 14, 2024
1 parent e802ab3 commit a22b71a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 52 deletions.
4 changes: 2 additions & 2 deletions documentation/docs/guides/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
Expand All @@ -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),
)
```

Expand Down
2 changes: 1 addition & 1 deletion examples/full-app-gourmet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 5 additions & 13 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log/slog"
"net/http"
"os"
"strings"
"time"

"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -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".
Expand Down
46 changes: 10 additions & 36 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit a22b71a

Please sign in to comment.