httpx

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2019 License: MIT Imports: 6 Imported by: 2

Documentation

Overview

Package httpx implements helper function for HTTP requests.

Index

Examples

Constants

View Source
const (
	// JSONContentType represents MIME type for JSON content.
	JSONContentType = "application/json"
)

Variables

View Source
var DefaultHealthPath = "/health"

DefaultHealthPath is the default HTTP path for checking health.

View Source
var DefaultStatsPath = "/metrics"

DefaultStatsPath is the default HTTP path for collecting metrics.

Functions

func CombineMuxes

func CombineMuxes(muxes ...*bone.Mux) *bone.Mux

CombineMuxes combines the given Muxes into a new Mux.

Example
package main

import (
	"github.com/hamba/pkg/httpx"
)

func main() {
	m1 := httpx.NewMux()
	m2 := httpx.NewMux()

	// Do something with the muxes

	mux := httpx.CombineMuxes(m1, m2)

	_ = httpx.NewServer(":8080", mux).ListenAndServe()
}

func NewHealthMux

func NewHealthMux(v ...Health) *bone.Mux

NewHealthMux returns a Mux with a health endpoint.

Example
package main

import (
	"github.com/hamba/pkg/httpx"
)

func main() {
	mux := httpx.NewHealthMux() // Add your health checker

	_ = httpx.NewServer(":8080", mux).ListenAndServe()
}

func NewMux

func NewMux() *bone.Mux

NewMux wraps bones New for convenience.

func NewServer

func NewServer(addr string, mux *bone.Mux, muxes ...*bone.Mux) *http.Server

NewServer creates a new http Server with the given Muxes.

Example
package main

import (
	"context"

	"github.com/hamba/pkg/httpx"
)

func main() {
	mux := httpx.NewMux()
	srv := httpx.NewServer(":8080", mux)

	if err := srv.ListenAndServe(); err != nil {
		// Handler err
	}

	_ = srv.Shutdown(context.Background()) // Server can be shutdown
}

func NewStatsMux

func NewStatsMux(s stats.Statter) *bone.Mux

NewStatsMux returns a Mux with a stats endpoint. If no statter implements the StatsHandler interface, and empty Mux is returned.

func RealIP

func RealIP(r *http.Request) string

RealIP resolves the real client IP address from the request.

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, code int, v interface{}) error

WriteJSONResponse encodes json content to the ResponseWriter.

Types

type Health

type Health interface {
	IsHealthy() error
}

Health represents an object that can check its health.

type StatsHandler

type StatsHandler interface {
	Handler() http.Handler
}

StatsHandler represents a statter that can expose stats for collection.

Directories

Path Synopsis
Package httptest provides utilities for HTTP testing.
Package httptest provides utilities for HTTP testing.
Package middleware implements reusable HTTP middleware.
Package middleware implements reusable HTTP middleware.

Jump to

Keyboard shortcuts

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