Skip to content

Commit

Permalink
Set default user agent for HTTP requests
Browse files Browse the repository at this point in the history
In order to behave like a good bot, set a default user agent to identify
Cupdate requests.
  • Loading branch information
AlexGustafsson committed Dec 31, 2024
1 parent 74728ff commit 8ba6433
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/cupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type Config struct {
Address string `env:"ADDRESS"`
} `envPrefix:"WEB_"`

HTTP struct {
UserAgent string `env:"USER_AGENT" envDefault:"Cupdate/1.0"`
} `envPrefix:"HTTP_"`

Cache struct {
Path string `env:"PATH" envDefault:"cachev1.boltdb"`
MaxAge time.Duration `env:"MAX_AGE" envDefault:"24h"`
Expand Down Expand Up @@ -214,6 +218,7 @@ func main() {

wg.Go(func() error {
httpClient := httputil.NewClient(cache, config.Cache.MaxAge)
httpClient.UserAgent = config.HTTP.UserAgent
prometheus.DefaultRegisterer.MustRegister(httpClient)

worker := worker.New(httpClient, writeStore)
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ done using environment variables.
| `CUPDATE_API_PORT` | The port to expose the API on. | `8080` |
| `CUPDATE_WEB_DISABLED` | Whether or not to disable the web UI. | `false` |
| `CUPDATE_WEB_ADDRESS` | The URL at which the UI is available (such as `https://example.com`). Used for RSS feeds, should generally not be set | Automatically resolved |
| `CUPDATE_HTTP_USER_AGENT` | The User Agent string to use for HTTP requests. | `Cupdate/1.0` |
| `CUPDATE_CACHE_PATH` | A path to the boltdb file in which to store cache. | `cachev1.boltdb` |
| `CUPDATE_CACHE_MAX_AGE` | The maximum age of cache entries. | `24h` |
| `CUPDATE_DB_PATH` | A path to the sqlite file in which to store data. | `dbv1.sqlite` |
Expand Down
9 changes: 9 additions & 0 deletions internal/httputil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var _ prometheus.Collector = (*Client)(nil)

type Client struct {
http.Client

UserAgent string

cache cache.Cache
cacheMaxAge time.Duration

Expand Down Expand Up @@ -58,6 +61,12 @@ func NewClient(cache cache.Cache, maxAge time.Duration) *Client {

// See [http.Client.Do].
func (c *Client) Do(req *http.Request) (*http.Response, error) {
if _, ok := req.Header["User-Agent"]; !ok {
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
}

res, err := c.Client.Do(req)
if err == nil {
c.requestsCounter.WithLabelValues(req.URL.Host, req.Method, strconv.FormatInt(int64(res.StatusCode), 10)).Inc()
Expand Down

0 comments on commit 8ba6433

Please sign in to comment.