aghhttp

package
v0.107.69 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package aghhttp provides some common methods to work with HTTP.

Index

Constants

View Source
const (
	HdrValApplicationJSON         = "application/json"
	HdrValStrictTransportSecurity = "max-age=31536000; includeSubDomains"
	HdrValTextPlain               = "text/plain"
)

HTTP header value constants.

View Source
const (
	// ErrorCodeTMP000 is the temporary error code used for all errors.
	ErrorCodeTMP000 = ""
)

ErrorCode constants.

TODO(a.garipov): Expand and document codes.

Variables

This section is empty.

Functions

func ErrorAndLog added in v0.107.53

func ErrorAndLog(
	ctx context.Context,
	l *slog.Logger,
	r *http.Request,
	w http.ResponseWriter,
	code int,
	format string,
	args ...any,
)

ErrorAndLog writes a formatted HTTP error response and logs it at slog.LevelError level. l, r, and w must not be nil.

func OK

OK writes "OK\n" to the response. l and w must not be nil.

func UserAgent added in v0.107.14

func UserAgent() (ua string)

UserAgent returns the ID of the service as a User-Agent string. It can also be used as the value of the Server HTTP header.

func WriteJSONResponse added in v0.107.14

func WriteJSONResponse(
	ctx context.Context,
	l *slog.Logger,
	w http.ResponseWriter,
	r *http.Request,
	code int,
	resp any,
)

WriteJSONResponse writes headers with the code, encodes resp into w, and logs any errors it encounters. r is used to get additional information from the request. l, w, and r must not be nil.

func WriteJSONResponseError added in v0.107.37

func WriteJSONResponseError(
	ctx context.Context,
	l *slog.Logger,
	w http.ResponseWriter,
	r *http.Request,
	err error,
)

WriteJSONResponseError encodes err as a JSON error into w, and logs any errors it encounters. r is used to get additional information from the request. l, w, and r must not be nil.

func WriteJSONResponseOK added in v0.107.37

func WriteJSONResponseOK(
	ctx context.Context,
	l *slog.Logger,
	w http.ResponseWriter,
	r *http.Request,
	v any,
)

WriteJSONResponseOK writes headers with the code 200 OK, encodes v into w, and logs any errors it encounters. r is used to get additional information from the request. l, w, and r must not be nil.

func WriteTextPlainDeprecated added in v0.107.14

func WriteTextPlainDeprecated(
	ctx context.Context,
	l *slog.Logger,
	w http.ResponseWriter,
	r *http.Request,
) (isPlainText bool)

WriteTextPlainDeprecated responds to the request with a message about deprecation and removal of a plain-text API if the request is made with the "text/plain" Content-Type. All arguments must not be nil.

Types

type DefaultRegistrar added in v0.107.69

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

DefaultRegistrar is an implementation of Registrar that registers handlers after applying a user-provided wrapper function.

func NewDefaultRegistrar added in v0.107.69

func NewDefaultRegistrar(mux *http.ServeMux, wrap WrapFunc) (r *DefaultRegistrar)

NewDefaultRegistrar returns a new properly initialized *DefaultRegistrar. mux and wrap must not be nil.

func (*DefaultRegistrar) Register added in v0.107.69

func (r *DefaultRegistrar) Register(method, path string, h http.HandlerFunc)

Register implements the Registrar interface.

type EmptyRegistrar added in v0.107.69

type EmptyRegistrar struct{}

EmptyRegistrar is an implementation of Registrar that does nothing.

func (EmptyRegistrar) Register added in v0.107.69

func (EmptyRegistrar) Register(_, _ string, _ http.HandlerFunc)

Register implements the Registrar interface.

type ErrorCode added in v0.107.37

type ErrorCode string

ErrorCode is the error code as used by the HTTP API. See the ErrorCode definition in the OpenAPI specification.

type HTTPAPIErrorResp added in v0.107.37

type HTTPAPIErrorResp struct {
	Code ErrorCode `json:"code"`
	Msg  string    `json:"msg"`
}

HTTPAPIErrorResp is the error response as used by the HTTP API. See the BadRequestResp, InternalServerErrorResp, and similar objects in the OpenAPI specification.

type JSONDuration added in v0.107.37

type JSONDuration time.Duration

JSONDuration is a time.Duration that can be decoded from JSON and encoded into JSON according to our API conventions.

func (JSONDuration) MarshalJSON added in v0.107.37

func (d JSONDuration) MarshalJSON() (b []byte, err error)

MarshalJSON implements the json.Marshaler interface for JSONDuration. err is always nil.

func (*JSONDuration) UnmarshalJSON added in v0.107.37

func (d *JSONDuration) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements the json.Marshaler interface for *JSONDuration.

type JSONTime added in v0.107.37

type JSONTime time.Time

JSONTime is a time.Time that can be decoded from JSON and encoded into JSON according to our API conventions.

func (JSONTime) MarshalJSON added in v0.107.37

func (t JSONTime) MarshalJSON() (b []byte, err error)

MarshalJSON implements the json.Marshaler interface for JSONTime. err is always nil.

func (*JSONTime) UnmarshalJSON added in v0.107.37

func (t *JSONTime) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements the json.Marshaler interface for *JSONTime.

type RegisterFunc added in v0.107.10

type RegisterFunc func(method, url string, handler http.HandlerFunc)

RegisterFunc is the function that sets the handler to handle the URL for the method.

TODO(e.burkov, a.garipov): Get rid of it.

type Registrar added in v0.107.69

type Registrar interface {
	Register(method, path string, h http.HandlerFunc)
}

Registrar registers an HTTP handler for a method and path.

TODO(s.chzhen): Implement [httputil.Router].

type WrapFunc added in v0.107.69

type WrapFunc func(method string, h http.HandlerFunc) (wrapped http.Handler)

WrapFunc is a wrapper function that builds an HTTP handler for a route.

Jump to

Keyboard shortcuts

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