middlewares

package
v0.1.7 Latest Latest
Warning

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

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

README

Package middlewares

This package provides various HTTP middleware functions to enhance the functionality and reliability of an HTTP server application.

Functions

3. RecoveryMiddleware

A middleware function that recovers from panics, logs errors with stack traces, sends an alert if needed, and returns a generic internal server error response.

Parameters:
  • next http.Handler: The next handler in the chain to invoke after this middleware.
Returns:
  • An http.Handler that wraps around the original handler and adds recovery functionality for panics.
Example Usage:
http.Handle("/", RecoveryMiddleware(http.HandlerFunc(someHandler)))
4. AuthMiddleware

A middleware function used for basic authentication, ensuring that incoming requests have a valid authorization token in their headers.

Parameters:
  • next http.Handler: The next handler in the chain to invoke after this middleware.
Returns:
  • An http.Handler that wraps around the original handler and adds basic authentication functionality.
Example Usage:
http.Handle("/", AuthMiddleware(http.HandlerFunc(someHandler)))

Notes:

  • RecoveryMiddleware: It catches any panics that occur in subsequent middleware or handlers and logs them along with stack traces. It also triggers an alert using github.com/Miskamyasa/utils/alerts package's Send function and returns an internal server error (HTTP status code 500) to the client.

  • AuthMiddleware: This middleware checks for a specific authorization token in the request headers (auth-token). If the token does not match the configured AUTH_TOKEN, it logs an unauthorized access attempt and sends back an internal server error response.

Requirements:

  • Ensure that you have the necessary environment variables set up, such as the AUTH_TOKEN required by the AuthMiddleware.

  • The middleware functions assume the existence of a caching system (github.com/Miskamyasa/utils/cache) and alerting utility (github.com/Miskamyasa/utils/alerts) packages.


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware(next http.Handler) http.Handler

func CacheMiddleware

func CacheMiddleware(next http.Handler) http.Handler

func GenerateCacheKey

func GenerateCacheKey(req *http.Request) string

func RecoveryMiddleware

func RecoveryMiddleware(next http.Handler) http.Handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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