Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.

Commit e770691

Browse files
authored
feat: add useragent for requests (#75)
* feature(httpclient): add useragent * build: set buildinfo vars * feat: print build date with build info * fix: imports * feat(lists): set user agent for http queries * fix: imports
1 parent 0ea22a3 commit e770691

File tree

10 files changed

+49
-12
lines changed

10 files changed

+49
-12
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ COPY . ./
1919
#ENV GOOS=linux
2020
ENV CGO_ENABLED=0
2121

22-
RUN go build -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${REVISION} -X main.date=${BUILDTIME}" -o bin/omegabrr cmd/omegabrr/main.go
22+
RUN go build -ldflags "-s -w -X buildinfo.Version=${VERSION} -X buildinfo.Commit=${REVISION} -X buildinfo.Date=${BUILDTIME}" -o bin/omegabrr cmd/omegabrr/main.go
2323

2424
# build runner
2525
FROM alpine:latest

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ GIT_TAG := $(shell git tag --points-at HEAD 2> /dev/null | head -n 1)
55

66
GO ?= go
77
RM ?= rm
8-
GOFLAGS ?= "-X main.commit=$(GIT_COMMIT) -X main.version=$(GIT_TAG)"
8+
GOFLAGS ?= "-X buildinfo.Commit=$(GIT_COMMIT) -X buildinfo.Version=$(GIT_TAG)"
99
PREFIX ?= /usr/local
1010
BINDIR ?= bin
1111

cmd/omegabrr/main.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/autobrr/omegabrr/internal/apitoken"
17+
"github.com/autobrr/omegabrr/internal/buildinfo"
1718
"github.com/autobrr/omegabrr/internal/domain"
1819
"github.com/autobrr/omegabrr/internal/http"
1920
"github.com/autobrr/omegabrr/internal/processor"
@@ -27,11 +28,6 @@ import (
2728
"github.com/spf13/pflag"
2829
)
2930

30-
var (
31-
version = "dev"
32-
commit = ""
33-
)
34-
3531
const usage = `omegabrr - Automagically turn your monitored titles from your arrs and lists into autobrr filters.
3632
3733
Usage:
@@ -98,7 +94,7 @@ func main() {
9894

9995
switch cmd := pflag.Arg(0); cmd {
10096
case "version":
101-
fmt.Printf("Version: %v\nCommit: %v\n", version, commit)
97+
fmt.Printf("Version: %v\nCommit: %v\nBuild date: %v\n", buildinfo.Version, buildinfo.Commit, buildinfo.Date)
10298

10399
// get the latest release tag from brr-api
104100
client := &netHTTP.Client{
@@ -132,7 +128,7 @@ func main() {
132128
fmt.Printf("Latest release: %v\n", rel.TagName)
133129

134130
case "update":
135-
v, err := semver.ParseTolerant(version)
131+
v, err := semver.ParseTolerant(buildinfo.Version)
136132
if err != nil {
137133
log.Error().Err(err).Msg("could not parse version")
138134
return
@@ -146,7 +142,7 @@ func main() {
146142

147143
if latest.Version.Equals(v) {
148144
// latest version is the same as current version. It means current binary is up-to-date.
149-
log.Info().Msgf("Current binary is the latest version: %s", version)
145+
log.Info().Msgf("Current binary is the latest version: %s", buildinfo.Version)
150146
} else {
151147
log.Info().Msgf("Successfully updated to version: %s", latest.Version)
152148
}
@@ -196,7 +192,7 @@ func main() {
196192
case "run":
197193
cfg := domain.NewConfig(configPath)
198194

199-
log.Info().Msgf("starting omegabrr: %s", version)
195+
log.Info().Msgf("starting omegabrr: %s", buildinfo.Version)
200196
log.Info().Msgf("running on schedule: %v", cfg.Schedule)
201197

202198
p := processor.NewService(cfg)

internal/buildinfo/buildinfo.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package buildinfo
2+
3+
var (
4+
Version = "dev"
5+
Commit = ""
6+
Date = ""
7+
)

internal/processor/client.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package processor
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"runtime"
7+
8+
"github.com/autobrr/omegabrr/internal/buildinfo"
9+
)
10+
11+
func setUserAgent(req *http.Request) {
12+
agent := fmt.Sprintf("omegabrr/%s (%s %s)", buildinfo.Version, runtime.GOOS, runtime.GOARCH)
13+
14+
req.Header.Set("User-Agent", agent)
15+
}

internal/processor/mdblist.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func (s Service) mdblist(ctx context.Context, cfg *domain.ListConfig, dryRun boo
3838
req.Header.Set(k, v)
3939
}
4040

41+
setUserAgent(req)
42+
4143
resp, err := s.httpClient.Do(req)
4244
if err != nil {
4345
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)

internal/processor/metacritic.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func (s Service) metacritic(ctx context.Context, cfg *domain.ListConfig, dryRun
3636
req.Header.Set(k, v)
3737
}
3838

39+
setUserAgent(req)
40+
3941
resp, err := s.httpClient.Do(req)
4042
if err != nil {
4143
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)

internal/processor/plaintext.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func (s Service) plaintext(ctx context.Context, cfg *domain.ListConfig, dryRun b
3939
req.Header.Set(k, v)
4040
}
4141

42+
setUserAgent(req)
43+
4244
resp, err := s.httpClient.Do(req)
4345
if err != nil {
4446
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)

internal/processor/trakt.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func (s Service) trakt(ctx context.Context, cfg *domain.ListConfig, dryRun bool,
4141
req.Header.Set(k, v)
4242
}
4343

44+
setUserAgent(req)
45+
4446
resp, err := s.httpClient.Do(req)
4547
if err != nil {
4648
l.Error().Err(err).Msgf("failed to fetch titles from URL: %s", cfg.URL)

pkg/autobrr/client.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import (
55
"context"
66
"crypto/tls"
77
"encoding/json"
8+
"fmt"
89
"io"
910
"net/http"
1011
"net/http/httputil"
12+
"runtime"
1113
"strconv"
1214
"time"
1315

14-
"github.com/rs/zerolog/log"
16+
"github.com/autobrr/omegabrr/internal/buildinfo"
1517

1618
"github.com/pkg/errors"
19+
"github.com/rs/zerolog/log"
1720
)
1821

1922
type Client struct {
@@ -76,6 +79,7 @@ func (c *Client) GetFilters(ctx context.Context) ([]Filter, error) {
7679

7780
req.SetBasicAuth(c.BasicUser, c.BasicPass)
7881
req.Header.Add("X-API-Token", c.APIKey)
82+
c.buildUserAgent(req)
7983

8084
res, err := c.client.Do(req)
8185
if err != nil {
@@ -115,6 +119,7 @@ func (c *Client) UpdateFilterByID(ctx context.Context, filterID int, filter Upda
115119

116120
req.SetBasicAuth(c.BasicUser, c.BasicPass)
117121
req.Header.Add("X-API-Token", c.APIKey)
122+
c.buildUserAgent(req)
118123

119124
res, err := c.client.Do(req)
120125
if err != nil {
@@ -136,6 +141,12 @@ func (c *Client) UpdateFilterByID(ctx context.Context, filterID int, filter Upda
136141
return nil
137142
}
138143

144+
func (c *Client) buildUserAgent(req *http.Request) {
145+
agent := fmt.Sprintf("omegabrr/%s (%s %s)", buildinfo.Version, runtime.GOOS, runtime.GOARCH)
146+
147+
req.Header.Set("User-Agent", agent)
148+
}
149+
139150
type Filter struct {
140151
ID string `json:"id"`
141152
Name string `json:"name"`

0 commit comments

Comments
 (0)