http

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 7 Imported by: 5

Documentation

Overview

Package http includes handlers and utilties.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAndReplaceBody added in v0.5.0

func GetAndReplaceBody(req *http.Request, w io.Writer) error

GetAndReplaceBody writes the body of req to w. If needed the body is replaced with a byte buffer for re-use.

func GetAndReplaceBodyBytes added in v0.5.0

func GetAndReplaceBodyBytes(req *http.Request) ([]byte, error)

GetAndReplaceBodyBytes returns the body of req as a byte slice. If needed the body is replaced with a byte buffer for re-use.

Example
buf := bytes.NewBuffer([]byte("Hello, world!"))

req, err := http.NewRequestWithContext(context.Background(), "GET", "", buf)
if err != nil {
	log.Fatal(err)
}

ret, err := GetAndReplaceBodyBytes(req)
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(ret))
Output:

Hello, world!

func NewJSONVersionHandler added in v0.3.0

func NewJSONVersionHandler(version string) http.HandlerFunc

NewJSONVersionHandler responds with JSON from version. The JSON is in the form of `{"version":"v0.0.1"}`. No validation is done on version; it should conform to a JSON string.

func NewSimpleBasicAuthHandler added in v0.3.0

func NewSimpleBasicAuthHandler(next http.Handler, username, password, realm string) http.HandlerFunc

NewSimpleBasicAuthHandler is an HTTP Basic authentication middleware. The returned handler verifies the the correct authentication against username and password before handing off to next, or reponds with an HTTP 401 setting the WWW-Authenticate header using realm.

Types

type MWMux added in v0.4.0

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

MWMux applies middleware when registering HTTP handlers.

func NewMWMux added in v0.4.0

func NewMWMux(mux Mux) *MWMux

NewMWMux creates a new MWMux using mux.

func (*MWMux) Handle added in v0.4.0

func (m *MWMux) Handle(pattern string, handler http.Handler)

Handle layers middlewares around handler and registers them with pattern.

func (*MWMux) HandleFunc added in v0.4.0

func (m *MWMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc layers middlewares around handler and registers them with pattern.

func (*MWMux) ServeHTTP added in v0.4.0

func (m *MWMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is a convenience wrapper to use m itself as an HTTP handler.

func (*MWMux) Use added in v0.4.0

func (m *MWMux) Use(middlewares ...func(http.Handler) http.Handler)

Use adds middlewares to be applied when registering HTTP handlers.

type Mux added in v0.4.0

type Mux interface {
	Handle(pattern string, handler http.Handler)
	HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

Mux represents an HTTP muxer.

Directories

Path Synopsis
Pacakge trace implements a trace logging HTTP request middleware handler.
Pacakge trace implements a trace logging HTTP request middleware handler.

Jump to

Keyboard shortcuts

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