httputils

package
v0.0.0-...-e9c6563 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const ContentTypeHeader = "Content-Type"
View Source
const (
	LogUserIDKey = contextKey("user_id")
)

Variables

View Source
var ErrInvalidJSON = errors.New("invalid JSON")

ErrInvalidJSON is returned when the body is not valid JSON.

Functions

func BadRequestResponse

func BadRequestResponse(w http.ResponseWriter, r *http.Request, err error)

BadRequestResponse sends a JSON-formatted error message with 400 Bad Request status code.

func EditConflictResponse

func EditConflictResponse(w http.ResponseWriter, r *http.Request)

EditConflictResponse method is used to send a 409 Conflict status code. This can occur when we try to create a new record in the database and another user has updated the same record concurrently.

func FailedValidationResponse

func FailedValidationResponse(w http.ResponseWriter, r *http.Request, errors []validation.Error)

FailedValidationResponse sends JSON-formatted error message to client with 400 Bad Request status code.

func GetCORSMiddleware

func GetCORSMiddleware(trustedOrigins []string) func(next http.Handler) http.Handler

func GetURL

func GetURL(baseURL string, queryParams map[string]string) (string, error)

func HandleErrorResponse

func HandleErrorResponse(w http.ResponseWriter, r *http.Request, err error)

HandleErrorResponse method is a utility function that will return the appropriate error from the service layer of our application.

func InitializeSlog

func InitializeSlog(level string) *slog.Logger

func NewSlogErrorWriter

func NewSlogErrorWriter(logger *slog.Logger) *log.Logger

func NotFoundResponse

func NotFoundResponse(w http.ResponseWriter, r *http.Request)

NotFoundResponse method is used to send a 404 Not Found status code.

func RateLimitExceededResponse

func RateLimitExceededResponse(w http.ResponseWriter, r *http.Request)

RateLimitExceededResponse method is used to send a 429 Too Many Requests status code. The rate limit middleware will return this status code if a request exceeds the rate limit.

func RateLimitMiddleware

func RateLimitMiddleware(next http.Handler) http.Handler

func ReadJSON

func ReadJSON[T any](w http.ResponseWriter, r *http.Request) (T, error)

ReadJSON decodes request Body into corresponding Go type. It triages for any potential errors and returns corresponding appropriate errors.

func ServeHTTP

func ServeHTTP(handler http.Handler, logger *slog.Logger) error

func ServerErrorResponse

func ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)

serverErrorResponse method is used when our application encounters an unexpected problem at runtime. it logs the detailed error message and returns a 500 Internal Server Error.

func SetJSONContentTypeRequestHeader

func SetJSONContentTypeRequestHeader(req *http.Request)

func SetJSONContentTypeResponseHeader

func SetJSONContentTypeResponseHeader(w http.ResponseWriter)

func UnauthorizedResponse

func UnauthorizedResponse(w http.ResponseWriter, r *http.Request)

UnauthorizedResponse method is used to send a 401 Unauthorized status code. This can occur if a user tries to access a protected resource without supplying valid credentials. If the request is made to an api endpoint, we will return a JSON response. Otherwise, we will redirect the user to the login page.

func UnprocessableEntityResponse

func UnprocessableEntityResponse(w http.ResponseWriter, r *http.Request, err error)

UnprocessableEntityResponse method is used to send a 422 Unprocessable Entity status code.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data interface{}, headers http.Header) error

WriteJSON marshals data structure to encoded JSON response and writes it to the response body.

Types

type RateLimitConfig

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

type Router

type Router interface {
	Connect(pattern string, h http.HandlerFunc)
	Delete(pattern string, h http.HandlerFunc)
	Get(pattern string, h http.HandlerFunc)
	Head(pattern string, h http.HandlerFunc)
	Options(pattern string, h http.HandlerFunc)
	Patch(pattern string, h http.HandlerFunc)
	Post(pattern string, h http.HandlerFunc)
	Put(pattern string, h http.HandlerFunc)
	Trace(pattern string, h http.HandlerFunc)
}

type SlogLogEntry

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

func (*SlogLogEntry) Panic

func (e *SlogLogEntry) Panic(v interface{}, stack []byte)

Panic logs a panic with its stack trace.

func (*SlogLogEntry) Write

func (e *SlogLogEntry) Write(status, bytes int, _ http.Header, elapsed time.Duration, _ interface{})

Write logs the request completion details.

type SlogLogFormatter

type SlogLogFormatter struct {
	Logger *slog.Logger
}

func NewSlogLogFormatter

func NewSlogLogFormatter(logger *slog.Logger) *SlogLogFormatter

NewSlogLogFormatter creates a new SlogLogFormatter.

func (*SlogLogFormatter) NewLogEntry

func (f *SlogLogFormatter) NewLogEntry(r *http.Request) middleware.LogEntry

NewLogEntry creates a new LogEntry for the request.

Jump to

Keyboard shortcuts

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