From b58ce4acb35c46dd9d8744106f5cadedaf47ab60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Mon, 20 Feb 2023 13:27:41 +0100 Subject: [PATCH] Set default header for User-Agent --- http.go | 14 ++++++++++++++ main.go | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 http.go diff --git a/http.go b/http.go new file mode 100644 index 0000000..226450c --- /dev/null +++ b/http.go @@ -0,0 +1,14 @@ +package main + +import "net/http" + +// httpClient is a wrapper around the default http.Client +// It is used to add default headers to the requests. +type httpClient struct { + *http.Client +} + +func (c *httpClient) Do(req *http.Request) (*http.Response, error) { + req.Header.Set("User-Agent", "ntfy-alertmanger") + return c.Client.Do(req) +} diff --git a/main.go b/main.go index 7e5f82b..8c2d3f7 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,7 @@ type receiver struct { cfg *config logger *log.Logger cache *cache - client *http.Client + client *httpClient } type payload struct { @@ -391,7 +391,7 @@ func main() { logger.Errorf("config: %v", err) } - client := &http.Client{Timeout: time.Second * 3} + client := &httpClient{&http.Client{Timeout: time.Second * 3}} receiver := &receiver{cfg: cfg, logger: logger, cache: newCache(cfg.cache.Duration), client: client} -- 2.44.0