httpserver

package
v0.0.0-...-02cd31d Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultListenFunc

func DefaultListenFunc(network, address string) (net.Listener, error)

DefaultListenFunc builds a net.Listener with the given network and address. This function is the default value for ListenFunc.

Types

type Handle

type Handle struct {
	// contains filtered or unexported fields
}

Handle is a reference to an HTTP server. It provides clean startup and shutdown for net/http HTTP servers.

func NewHandle

func NewHandle(srv *http.Server, opts ...HandleOption) *Handle

NewHandle builds a Handle to the given HTTP server. You can use the returned Handle to start the server and access information about the running server.

Handle must be used for all server operations from this point onwards. Starting or stopping the http.Server directly will lead to undefined behavior.

Note that Handle is not thread-safe. You must not call procedures on Handle concurrently.

func (*Handle) Addr

func (h *Handle) Addr() net.Addr

Addr returns the address on which the HTTP server is listening. This can be used to determine the address of the server if it was started on an OS-assigned port (":0").

Returns nil if the server hasn't been started yet.

func (*Handle) Shutdown

func (h *Handle) Shutdown(ctx context.Context) error

Shutdown initiates a graceful shutdown of the HTTP server. The provided context controls how long we are willing to wait for the server to shut down. Shutdown will block until the server has shut down completely or until the context finishes.

func (*Handle) Start

func (h *Handle) Start(ctx context.Context) error

Start starts the HTTP server for this Handle in a separate goroutine and blocks until the server is ready to accept requests or the provided context finishes.

The server is started on the address defined on Server.Addr, defaulting to an OS-assigned port (":0") if Server.Addr is empty.

h := httpserver.NewHandle(&http.Server{Handler: myHandler})
err := h.Start(ctx)

Note that because the server is started in a separate goroutine, this method is safe to use as-is inside Fx Lifecycle hooks.

fx.Hook{
  OnStart: handle.Start,
  OnStop: handle.Shutdown,
}

type HandleOption

type HandleOption interface {
	// contains filtered or unexported methods
}

HandleOption customizes the behavior of a Handle.

func ListenFunc

func ListenFunc(f func(string, string) (net.Listener, error)) HandleOption

ListenFunc is an option for Handle that allows changing how it listens for incoming connections.

Jump to

Keyboard shortcuts

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