Documentation ¶
Overview ¶
Package httpx implements helper function for HTTP requests.
Index ¶
- Constants
- Variables
- func CombineMuxes(muxes ...*bone.Mux) *bone.Mux
- func NewHealthMux(v ...Health) *bone.Mux
- func NewMux() *bone.Mux
- func NewServer(addr string, mux *bone.Mux, muxes ...*bone.Mux) *http.Server
- func NewStatsMux(s stats.Statter) *bone.Mux
- func RealIP(r *http.Request) string
- func WriteJSONResponse(w http.ResponseWriter, code int, v interface{}) error
- type Health
- type StatsHandler
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 ¶
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() }
Output:
func NewHealthMux ¶
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() }
Output:
func NewServer ¶
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 }
Output:
func NewStatsMux ¶
NewStatsMux returns a Mux with a stats endpoint. If no statter implements the StatsHandler interface, and empty Mux is returned.
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 ¶
StatsHandler represents a statter that can expose stats for collection.
Directories ¶
Path | Synopsis |
---|---|
Package middleware implements reusable HTTP middleware.
|
Package middleware implements reusable HTTP middleware. |
Click to show internal directories.
Click to hide internal directories.