Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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 ¶
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 ¶
ListenFunc is an option for Handle that allows changing how it listens for incoming connections.