mirror of
https://github.com/dstotijn/hetty.git
synced 2025-07-01 18:47:29 -04:00
Add project settings for intercept
This commit is contained in:
@ -31,10 +31,12 @@ type Service struct {
|
||||
mu *sync.RWMutex
|
||||
requests map[ulid.ULID]Request
|
||||
logger log.Logger
|
||||
enabled bool
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Logger log.Logger
|
||||
Logger log.Logger
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
// RequestIDs implements sort.Interface.
|
||||
@ -45,6 +47,7 @@ func NewService(cfg Config) *Service {
|
||||
mu: &sync.RWMutex{},
|
||||
requests: make(map[ulid.ULID]Request),
|
||||
logger: cfg.Logger,
|
||||
enabled: cfg.Enabled,
|
||||
}
|
||||
|
||||
if s.logger == nil {
|
||||
@ -93,6 +96,12 @@ func (svc *Service) Intercept(ctx context.Context, req *http.Request) (*http.Req
|
||||
return req, nil
|
||||
}
|
||||
|
||||
if !svc.enabled {
|
||||
// If intercept is disabled, return the incoming request as-is.
|
||||
svc.logger.Debugw("Bypassed interception: module disabled.")
|
||||
return req, nil
|
||||
}
|
||||
|
||||
ch := make(chan *http.Request)
|
||||
done := make(chan struct{})
|
||||
|
||||
@ -181,6 +190,15 @@ func (svc *Service) Requests() []*http.Request {
|
||||
return reqs
|
||||
}
|
||||
|
||||
func (svc *Service) UpdateSettings(settings Settings) {
|
||||
// When updating from `enabled` -> `disabled`, clear any pending reqs.
|
||||
if svc.enabled && !settings.Enabled {
|
||||
svc.ClearRequests()
|
||||
}
|
||||
|
||||
svc.enabled = settings.Enabled
|
||||
}
|
||||
|
||||
// Request returns an intercepted request by ID. It's safe for concurrent use.
|
||||
func (svc *Service) RequestByID(id ulid.ULID) (*http.Request, error) {
|
||||
svc.mu.RLock()
|
||||
|
Reference in New Issue
Block a user