gphttp

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Get  = httpClient.Get
	Post = httpClient.Post
	Head = httpClient.Head
)
View Source
var DefaultDialer = net.Dialer{
	Timeout: 5 * time.Second,
}

Functions

func BadRequest

func BadRequest(w http.ResponseWriter, err string, code ...int)

BadRequest returns a Bad Request response with the given error message.

func ClientError

func ClientError(w http.ResponseWriter, err error, code ...int)

ClientError is for responding to client errors.

It returns http.StatusBadRequest with reason to the client. Status code can be specified as an argument.

For JSON marshallable errors (e.g. gperr.Error), it returns the error details as JSON. Otherwise, it returns the error details as plain text.

func ErrAlreadyExists

func ErrAlreadyExists(k, v string) error

func ErrInvalidKey

func ErrInvalidKey(k string) error

func ErrMissingKey

func ErrMissingKey(k string) error

func ErrNotFound

func ErrNotFound(k, v string) error

func Forbidden

func Forbidden(w http.ResponseWriter, err string)

Forbidden returns a Forbidden response with the given error message.

func IsMethodValid

func IsMethodValid(method string) bool

func IsStatusCodeValid

func IsStatusCodeValid(status int) bool

func IsSuccess

func IsSuccess(status int) bool

func JSONError

func JSONError(w http.ResponseWriter, err gperr.Error, code int)

JSONError returns a JSON response of gperr.Error with the given status code.

func LogDebug

func LogDebug(r *http.Request) *zerolog.Event

func LogError

func LogError(r *http.Request) *zerolog.Event

func LogInfo

func LogInfo(r *http.Request) *zerolog.Event

func LogWarn

func LogWarn(r *http.Request) *zerolog.Event

func NewTransport

func NewTransport() *http.Transport

func NewTransportWithTLSConfig

func NewTransportWithTLSConfig(tlsConfig *tls.Config) *http.Transport

func NotFound

func NotFound(w http.ResponseWriter, err string)

NotFound returns a Not Found response with the given error message.

func RespondJSON

func RespondJSON(w http.ResponseWriter, r *http.Request, data any, code ...int) (canProceed bool)

func ServerError

func ServerError(w http.ResponseWriter, r *http.Request, err error, code ...int)

ServerError is for handling server errors.

It logs the error and returns http.StatusInternalServerError to the client. Status code can be specified as an argument.

func Unauthorized

func Unauthorized(w http.ResponseWriter, err string)

Unauthorized returns an Unauthorized response with the given error message.

func WriteBody

func WriteBody(w http.ResponseWriter, body []byte)

Types

type AcceptContentType

type AcceptContentType []ContentType

func GetAccept

func GetAccept(h http.Header) AcceptContentType

func (AcceptContentType) AcceptHTML

func (act AcceptContentType) AcceptHTML() bool

func (AcceptContentType) AcceptJSON

func (act AcceptContentType) AcceptJSON() bool

func (AcceptContentType) AcceptPlainText

func (act AcceptContentType) AcceptPlainText() bool

func (AcceptContentType) IsEmpty

func (act AcceptContentType) IsEmpty() bool

type ContentType

type ContentType string

func GetContentType

func GetContentType(h http.Header) ContentType

func (ContentType) IsHTML

func (ct ContentType) IsHTML() bool

func (ContentType) IsJSON

func (ct ContentType) IsJSON() bool

func (ContentType) IsPlainText

func (ct ContentType) IsPlainText() bool

type ModifyResponseFunc

type ModifyResponseFunc func(*http.Response) error

type ModifyResponseWriter

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

func (*ModifyResponseWriter) Flush

func (w *ModifyResponseWriter) Flush()

Flush sends any buffered data to the client.

func (*ModifyResponseWriter) Header

func (w *ModifyResponseWriter) Header() http.Header

func (*ModifyResponseWriter) Hijack

Hijack hijacks the connection.

func (*ModifyResponseWriter) Size

func (w *ModifyResponseWriter) Size() int

func (*ModifyResponseWriter) StatusCode

func (w *ModifyResponseWriter) StatusCode() int

func (*ModifyResponseWriter) Unwrap

func (*ModifyResponseWriter) Write

func (w *ModifyResponseWriter) Write(b []byte) (int, error)

func (*ModifyResponseWriter) WriteHeader

func (w *ModifyResponseWriter) WriteHeader(code int)

type ServeMux

type ServeMux struct {
	*http.ServeMux
}

func NewServeMux

func NewServeMux() ServeMux

func (ServeMux) Handle

func (mux ServeMux) Handle(pattern string, handler http.Handler) (err error)

func (ServeMux) HandleFunc

func (mux ServeMux) HandleFunc(pattern string, handler http.HandlerFunc) (err error)

Jump to

Keyboard shortcuts

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