server

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, listenAddr string, router http.Handler, logger *slog.Logger, opts ...Option) error

Run creates and runs an HTTP server until ctx is canceled or startup fails.

Parameters:

  • ctx controls the server lifetime; cancellation triggers graceful shutdown.
  • listenAddr is passed to http.Server.Addr, for example ":8080".
  • router is assigned to http.Server.Handler.
  • logger is used for lifecycle logs; if nil, lifecycle logging is disabled.
  • opts override default timeouts via WithOptions.

Run returns an error when startup fails (for example, address already in use) or when graceful shutdown fails.

func RunServer

func RunServer(ctx context.Context, server *http.Server, logger *slog.Logger) error

RunServer starts server with ListenAndServe and manages graceful shutdown.

Parameters:

  • ctx controls the server lifetime; cancellation triggers graceful shutdown.
  • server is the fully configured http.Server instance to run.
  • logger is used for lifecycle logs; if nil, lifecycle logging is disabled.

RunServer uses the default shutdown timeout. It returns an error when startup fails or when graceful shutdown fails.

Types

type Option

type Option func(*Options)

Option mutates Options used by Run.

func WithOptions

func WithOptions(opts Options) Option

WithOptions overwrites the server timeout options used by Run.

type Options

type Options struct {
	// ReadHeaderTimeout limits how long to read request headers.
	ReadHeaderTimeout time.Duration
	// WriteTimeout limits the time spent writing the response.
	WriteTimeout time.Duration
	// IdleTimeout sets how long to keep idle connections open.
	IdleTimeout time.Duration
	// ShutdownTimeout bounds how long to wait for graceful shutdown.
	ShutdownTimeout time.Duration
}

Options configures the HTTP server timeouts applied by Run.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL