Documentation
¶
Index ¶
- func CORSMiddleware(next http.Handler) http.Handler
- func ExampleUsage()
- func GetClientIP(r *http.Request) string
- func InitDefaultLogger()
- func MaliciousRateLimitMiddleware(limiter *IPRateLimiter) func(http.Handler) http.Handler
- func RecoverPanicMiddleware(h http.Handler) http.Handler
- func RequestLoggerMiddleware(next http.Handler) http.Handler
- func SecurityHeaders(next http.Handler) http.Handler
- func SetLogger(l interface{})
- func SimpleRateLimitMiddleware(next http.Handler) http.Handler
- type IPRateLimiter
- type ReqRespLogStruct
- type RequestStruct
- type ResponseStruct
- type ResponseWriterWrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CORSMiddleware ¶
CORSMiddleware function sets the headers for the response and returns directly if there is an OPTIONS request
func GetClientIP ¶ added in v0.1.12
GetClientIP extracts the client IP address from the request
func InitDefaultLogger ¶
func InitDefaultLogger()
InitDefaultLogger sets the default logger to "github.com/gyozatech/noodlog" is no other logger has been set
func MaliciousRateLimitMiddleware ¶ added in v0.1.12
func MaliciousRateLimitMiddleware(limiter *IPRateLimiter) func(http.Handler) http.Handler
MaliciousRateLimitMiddleware creates a middleware that limits requests based on IP
func RequestLoggerMiddleware ¶
RequestLoggerMiddleware is the middleware layer to log all the HTTP requests
func SecurityHeaders ¶ added in v0.1.12
SecurityHeaders adds security headers to responses
Types ¶
type IPRateLimiter ¶ added in v0.1.12
type IPRateLimiter struct {
// contains filtered or unexported fields
}
IPRateLimiter stores rate limiters for individual IP addresses
func NewIPRateLimiter ¶ added in v0.1.12
NewIPRateLimiter creates a new rate limiter for IP addresses rate: requests per second burst: maximum burst size banDuration: how long to ban IPs that exceed the rate limit
func (*IPRateLimiter) AddIP ¶ added in v0.1.12
func (i *IPRateLimiter) AddIP(ip string) *rate.Limiter
AddIP creates a new rate limiter and adds it to the ips map, using the IP address as the key
func (*IPRateLimiter) BanIP ¶ added in v0.1.12
func (i *IPRateLimiter) BanIP(ip string)
BanIP bans an IP for the configured duration
func (*IPRateLimiter) CleanupJob ¶ added in v0.1.12
func (i *IPRateLimiter) CleanupJob(cleanupInterval time.Duration)
CleanupJob removes old IP entries to prevent memory leaks Should be run periodically, e.g., via a goroutine
func (*IPRateLimiter) GetLimiter ¶ added in v0.1.12
func (i *IPRateLimiter) GetLimiter(ip string) *rate.Limiter
GetLimiter returns the rate limiter for the provided IP address if it exists, otherwise calls AddIP to add a new one
type ReqRespLogStruct ¶
type ReqRespLogStruct struct {
Request RequestStruct
ExecTime time.Duration
Response ResponseStruct
}
ReqRespLogStruct struct represents the schema for the HTTP Request/ExecutionTime/Response log
type RequestStruct ¶
type RequestStruct struct {
Verb string `json:"verb,omitempty"`
Path string `json:"path,omitempty"`
Protocol string `json:"protocol,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body interface{} `json:"body,omitempty"`
}
RequestStruct struct represents the schema for the HTTP Request log
func HTTPRequest ¶
func HTTPRequest(r *http.Request) RequestStruct
HTTPRequest static function flatten pointers of HTTP Request and obscurate passwords to prepare for logging
type ResponseStruct ¶
type ResponseStruct struct {
Status int `json:"status,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body interface{} `json:"body,omitempty"`
}
ResponseStruct struct represents the schema for the HTTP Response log
func HTTPResponse ¶
func HTTPResponse(statusCode int, responseHeaders http.Header, requestURI string, responseBody string) ResponseStruct
HTTPResponse static function flatten pointers of HTTP Response and obscurate passwords to prepare for logging
type ResponseWriterWrapper ¶
type ResponseWriterWrapper struct {
// contains filtered or unexported fields
}
ResponseWriterWrapper struct is used to log the response
func NewResponseWriterWrapper ¶
func NewResponseWriterWrapper(w *http.ResponseWriter, r *http.Request) ResponseWriterWrapper
NewResponseWriterWrapper static function creates a wrapper for the http.ResponseWriter
func (ResponseWriterWrapper) Header ¶
func (rww ResponseWriterWrapper) Header() http.Header
Header function overwrites the http.ResponseWriter Header() function
func (ResponseWriterWrapper) Hijack ¶ added in v0.1.8
func (rww ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack allows the ResponseWriterWrapper to implement the http.Hijacker interface necessary to upgrade the connection to a WebSocket
func (ResponseWriterWrapper) Write ¶
func (rww ResponseWriterWrapper) Write(buf []byte) (int, error)
func (ResponseWriterWrapper) WriteHeader ¶
func (rww ResponseWriterWrapper) WriteHeader(statusCode int)
WriteHeader function overwrites the http.ResponseWriter WriteHeader() function