Skip to content

Commit

Permalink
Set default Serve port
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Feb 1, 2024
1 parent b8432f7 commit 1d513b4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions babyapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,12 @@ func (a *API[T]) AddIDMiddleware(m func(http.Handler) http.Handler) *API[T] {
}

// Serve will serve the API on the given port
func (a *API[T]) Serve(port string) {
a.server = &http.Server{Addr: port, Handler: a.Router()}
func (a *API[T]) Serve(address string) {
if address == "" {
address = ":8080"
}

a.server = &http.Server{Addr: address, Handler: a.Router()}

var serverStopCtx context.CancelFunc
a.serverCtx, serverStopCtx = context.WithCancel(context.Background())
Expand Down Expand Up @@ -287,7 +291,7 @@ func (a *API[T]) Serve(port string) {
serverStopCtx()
}()

slog.Info("starting server", "port", port, "api", a.name)
slog.Info("starting server", "address", address, "api", a.name)
err := a.server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
slog.Error("error starting the server", "error", err)
Expand Down

0 comments on commit 1d513b4

Please sign in to comment.