Documentation ¶
Index ¶
- Constants
- Variables
- func BadRequestResponse(w http.ResponseWriter, r *http.Request, err error)
- func EditConflictResponse(w http.ResponseWriter, r *http.Request)
- func FailedValidationResponse(w http.ResponseWriter, r *http.Request, errors []validation.Error)
- func GetCORSMiddleware(trustedOrigins []string) func(next http.Handler) http.Handler
- func GetURL(baseURL string, queryParams map[string]string) (string, error)
- func HandleErrorResponse(w http.ResponseWriter, r *http.Request, err error)
- func InitializeSlog(level string) *slog.Logger
- func NewSlogErrorWriter(logger *slog.Logger) *log.Logger
- func NotFoundResponse(w http.ResponseWriter, r *http.Request)
- func RateLimitExceededResponse(w http.ResponseWriter, r *http.Request)
- func RateLimitMiddleware(next http.Handler) http.Handler
- func ReadJSON[T any](w http.ResponseWriter, r *http.Request) (T, error)
- func ServeHTTP(handler http.Handler, logger *slog.Logger) error
- func ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)
- func SetJSONContentTypeRequestHeader(req *http.Request)
- func SetJSONContentTypeResponseHeader(w http.ResponseWriter)
- func UnauthorizedResponse(w http.ResponseWriter, r *http.Request)
- func UnprocessableEntityResponse(w http.ResponseWriter, r *http.Request, err error)
- func WriteJSON(w http.ResponseWriter, status int, data interface{}, headers http.Header) error
- type RateLimitConfig
- type Router
- type SlogLogEntry
- type SlogLogFormatter
Constants ¶
const ContentTypeHeader = "Content-Type"
const (
LogUserIDKey = contextKey("user_id")
)
Variables ¶
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 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 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 ReadJSON ¶
ReadJSON decodes request Body into corresponding Go type. It triages for any potential errors and returns corresponding appropriate errors.
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 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.
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.
type SlogLogFormatter ¶
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.