mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-19 06:59:50 +02:00
refactor: fix lint errors; improve error handling
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
type roundRobin struct {
|
||||
index atomic.Uint32
|
||||
index atomic.Uint64
|
||||
}
|
||||
|
||||
var _ impl = (*roundRobin)(nil)
|
||||
@@ -21,6 +21,6 @@ func (lb *roundRobin) ChooseServer(srvs types.LoadBalancerServers, r *http.Reque
|
||||
if len(srvs) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := (lb.index.Add(1) - 1) % uint32(len(srvs))
|
||||
index := (lb.index.Add(1) - 1) % uint64(len(srvs))
|
||||
return srvs[index]
|
||||
}
|
||||
|
||||
@@ -22,12 +22,14 @@ type HcaptchaProvider struct {
|
||||
Secret string `json:"secret" validate:"required"`
|
||||
}
|
||||
|
||||
// https://docs.hcaptcha.com/#content-security-policy-settings
|
||||
// CSPDirectives returns the CSP directives for the Hcaptcha provider.
|
||||
// See: https://docs.hcaptcha.com/#content-security-policy-settings
|
||||
func (p *HcaptchaProvider) CSPDirectives() []string {
|
||||
return []string{"script-src", "frame-src", "style-src", "connect-src"}
|
||||
}
|
||||
|
||||
// https://docs.hcaptcha.com/#content-security-policy-settings
|
||||
// CSPSources returns the CSP sources for the Hcaptcha provider.
|
||||
// See: https://docs.hcaptcha.com/#content-security-policy-settings
|
||||
func (p *HcaptchaProvider) CSPSources() []string {
|
||||
return []string{
|
||||
"https://hcaptcha.com",
|
||||
|
||||
@@ -34,11 +34,11 @@ type (
|
||||
}
|
||||
|
||||
Middleware struct {
|
||||
commonOptions
|
||||
|
||||
name string
|
||||
construct ImplNewFunc
|
||||
impl any
|
||||
|
||||
commonOptions
|
||||
}
|
||||
ByPriority []*Middleware
|
||||
|
||||
@@ -196,7 +196,12 @@ func (m *Middleware) ServeHTTP(next http.HandlerFunc, w http.ResponseWriter, r *
|
||||
|
||||
if exec, ok := m.impl.(ResponseModifier); ok {
|
||||
lrm := httputils.NewLazyResponseModifier(w, needsBuffering)
|
||||
defer lrm.FlushRelease()
|
||||
defer func() {
|
||||
_, err := lrm.FlushRelease()
|
||||
if err != nil {
|
||||
m.LogError(r).Err(err).Msg("failed to flush response")
|
||||
}
|
||||
}()
|
||||
next(lrm, r)
|
||||
|
||||
// Skip modification if response wasn't buffered (non-HTML content)
|
||||
@@ -225,7 +230,9 @@ func (m *Middleware) ServeHTTP(next http.HandlerFunc, w http.ResponseWriter, r *
|
||||
|
||||
// override the content length and body if changed
|
||||
if currentResp.Body != currentBody {
|
||||
rm.SetBody(currentResp.Body)
|
||||
if err := rm.SetBody(currentResp.Body); err != nil {
|
||||
m.LogError(r).Err(err).Msg("failed to set response body")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
next(w, r)
|
||||
@@ -239,12 +246,14 @@ func needsBuffering(header http.Header) bool {
|
||||
}
|
||||
|
||||
func (m *Middleware) LogWarn(req *http.Request) *zerolog.Event {
|
||||
//nolint:zerologlint
|
||||
return log.Warn().Str("middleware", m.name).
|
||||
Str("host", req.Host).
|
||||
Str("path", req.URL.Path)
|
||||
}
|
||||
|
||||
func (m *Middleware) LogError(req *http.Request) *zerolog.Event {
|
||||
//nolint:zerologlint
|
||||
return log.Error().Str("middleware", m.name).
|
||||
Str("host", req.Host).
|
||||
Str("path", req.URL.Path)
|
||||
|
||||
Reference in New Issue
Block a user