http

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package http provides utilities for running a http server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v0.2.0

type Option func(s *Server) error

An Option modifies the behaviour of a `Server`.

func WithBaseContext added in v0.2.0

func WithBaseContext(ctx context.Context) Option

WithBaseContext sets the `context.Context` that will server as base context for incoming requests.

func WithLogger added in v0.2.0

func WithLogger(logger *zap.Logger) Option

WithLogger sets the logger used for the underlying `http.Server`. The supplied logger will be named "http" and log at error level.

func WithShutdownTimeout added in v0.2.0

func WithShutdownTimeout(dur time.Duration) Option

WithShutdownTimeout sets the timeout of the graceful shutdown.

type Server

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

Server implements a http server which handles request using the configured `http.Handler`. It provides graceful shutdown capabilities using `context.Context` propagation.

func NewServer

func NewServer(addr string, handler http.Handler, options ...Option) (*Server, error)

NewServer creates a new http server listening on the configured address and handling requests using the configured `http.Handler`. An error is returned if the address the server should listen on is already in use.

func (*Server) ListenAddr

func (s *Server) ListenAddr() net.Addr

ListenAddr returns the address the http server is listening on.

func (*Server) ListenError

func (s *Server) ListenError() <-chan error

ListenError returns the receive-only channel which signals errors during http server startup.

func (*Server) Run

func (s *Server) Run(ctx context.Context)

Run starts the http server in a separate goroutine and pushes errors into the errors channel. This method is non-blocking. Use the `ListenError()` method to listen for errors which occure during startup and handle them accordingly.

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown stops the http server gracefully.

func (*Server) ShutdownCtx added in v0.2.0

func (s *Server) ShutdownCtx(ctx context.Context) error

ShutdownCtx stops the http server gracefully but respects the context.

Jump to

Keyboard shortcuts

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