httpwrapper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: MIT Imports: 6 Imported by: 0

README

httpwrapper

Build Status Coverage Status Go Reference MIT License

This package simplifies the use of http.Server by providing functions and types that implement common patterns.

Usage

To use the package, begin by importing it:

import "github.com/nathan-osman/httpwrapper"

To wrap an http.Server, allowing you to listen without explicitly creating a separate goroutine, simply:

s := http.Server{}
h := httpwrapper.New(&s)

// ...server is listening and awaiting connections...

h.Close()

To have an http.Server listen until a context is cancelled, simply:

s := http.Server{}
ctx, cancel := context.WithCancel(context.Background())
if err := httpwrapper.Run(s, ctx); err != nil {
    panic(err)
}

If you want to watch for signals (SIGINT and SIGTERM), you can use the Signal() function in conjunction with the Run() function:

s := http.Server{}
err := httpwrapper.Run(s, httpwrapper.Signal(context.Background()))
if err != nil && !errors.Is(err, context.Canceled) {
    panic(err)
}

You can also just wait for a signal:

httpwrapper.WaitForSignal()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(s *http.Server, ctx context.Context) error

Run wraps the http.Server and waits until either an error occurs or the provided context is cancelled. The error returned will be from the server (if listening fails) or from the context (if cancelled).

func Signal

func Signal(parentCtx context.Context) context.Context

Signal returns a context that will cancel when a signal is received or the provided parent context is cancelled.

func WaitForSignal

func WaitForSignal()

WaitForSignal returns only when a signal has been received.

Types

type HTTPWrapper

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

HTTPWrapper wraps an http.Server.

func New

func New(s *http.Server) *HTTPWrapper

New creates a new HTTPWrapper from the provided http.Server. Note that the server will begin listening immediately.

func (*HTTPWrapper) Close

func (h *HTTPWrapper) Close()

Close shuts down and waits for the server.

func (*HTTPWrapper) Error

func (h *HTTPWrapper) Error() <-chan error

Error returns a channel that sends when a listening error occurs.

Jump to

Keyboard shortcuts

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